First of all, thank you very much for the guidance of some of the predecessors in the previous article, some of which have benefited me a lot!
About the Internet of things, I think the soul is still in the software, the front-end of all kinds of hardware networking standards once the formation of nothing, more still is the data storage, analysis. In fact, the internet of Things is a kind of data service one-stop. From to data transfer and data storage and processing. Data transmission is needless to say, belongs to the category of communications, is the domain of broadband and three operators, the is an embedded category, the storage of data may be general programmer and DBA can be done, the most core part is the analysis of data, but unfortunately do data mining and large processing, Most of them are PhD and master level! So as an average of two graduates, I am ready to target in the embedded direction, accurately speaking is embedded software development engineer.
has always been the mouth of the internet of things, closed intelligent furniture, finally in the last time, paid the practice, made an embedded Linux based Intelligent Furniture Remote control system, of course, the system is constantly improving, has realized the remote control and environmental information real-time upload function. Hope that the road Daniel, more decision bricks, more guidance advice.
The main objective of this paper is to display the server of the system, and to write the building/driver of the embedded environment without detailed description.
Before the realization of the intelligent home control system, we need to build a human-computer interface, where I mainly use the HTML page, that is, the ultimate goal is that people can be in the network where the home of the embedded Web server, and then through these web pages to use electrical control, while the You can also transfer the home real-time environment value to the HTML page, you can remotely view the environment information in the room, remote for the family to open the air purifier or air conditioning.
STEP 0: Preparation for development
Host: Ubuntu operating system
Development Board: Friendlyarm mini2440
Driver: Led driver, temperature sensor drive
Related technology preparation: Socket Programming/http Protocol/javascript
STEP 1: Implementing a Web server
Although the embedded small, but there is a batch of open source server is available to choose, such as the famous Boa, small and powerful. I was at the beginning of the choice to transplant the BOA server as a smart furniture server, but later in the addition of furniture control module and temperature transmission module feel very inconvenient, so call decided to manually build a server, although and boa difference 100,000 miles, but learned more things, And the mechanism of the web has a deeper understanding of the perception as a learner, value.
Before the bare-hand development of the server, you have to know, the web bottom of the working mechanism, said here is a joke, once I went to interview, Post is Java Web,boss asked me what you know about the web, I casually said: "In fact, the web is the socket and HTTP", Later, at a dinner party boss told me: "When you hear a student who has not graduated to the web can have such a knowledge, it is very surprising, so even if you do not have experience I will still hire." "It seems that sometimes bragging works very well, haha."
Although it is bragging force, but the essence of the web is really a socket and HTTP protocol, browser-side I do not know the words, but I wipe the browser there must be a socket to communicate with our server. Next in detail on our server side, the server wants to communicate with the browser, the first need to establish a socket, and then the traditional network programming, but note that the server and browser for "dialogue", using the "HTTP" language. This means that the web is essentially a html+socket+ browser. However, the socket is also the implementation layer of TCP/IP protocol, so the essence of the Web is html+tcp/ip+ browser. However, the essence of HTML is a file, so the essence of the Web is the file +tcp/ip+ browser (file translator (special file translator)). About the browser I am not a lot of research, dare not talk nonsense.
So, summed up in a sentence is: "The web is to a computer called a server to request files, the server sends the specified file to the browser, and then the browser parses the file and show a process."
Server_sockfd=socket (Af_inet,sock_stream,0); if(server_sockfd<0) {printf ("Create socket failed!\n"); Exit (Exit_failure); } memset (&SERVER_ADDR,0,sizeof(SERVER_ADDR)); Server_addr.sin_family=af_inet; Server_addr.sin_port=htons (PORT); Server_addr.sin_addr.s_addr=Inaddr_any; if(Bind (SERVER_SOCKFD, (structsockaddr*) &server_addr,sizeof(SERVER_ADDR))! =0) {printf ("bind error!\n"); Exit (Exit_failure); }
After the above operation, has created a socket for the server, but also for the socket binding IP address and port, in this way, for our server in the vast network of the ocean has an accurate positioning.
About sockets and sockaddr_in, I always understand that a socket is equivalent to a phone, if you want someone else to get through your phone, first you have a SIM card, and this sockaddr_in is the equivalent of a SIM card, once tied to your phone, Someone (the client) can initiate a call to you.
Next is the general steps of socket programming, listen and Accpect, which is equivalent to your phone loaded with a SIM card and opened the phone, waiting for someone else's call.
Client_sockfd=accept (SERVER_SOCKFD, (struct sockaddr*) &client_addr,&addr_len);
The Accpect function implements the continuation of the blocking program until a client initiates the request, and when a client initiates the request, the Accpect function returns a descriptive symbol for the link. The returned result represents the link description of the client and the server, which can be used directly to represent the linked relationship that has been established.
At this point, like a person who dialed your phone and you answered the phone, your link is now set up. You can use your ear to stick to the microphone to listen to what the person said, in our program you can also use recv to answer the client has sent those requests.
int Recv_len=recv (client_sockfd,buffer,sizeof (buffer), 0);
Where buffer represents the client request information and information about the client. Like the words that the man said to you.
about what the buffer will be, I believe that people who have not been contacted are curious, and so do I, so I printed it out (provided I entered http:127.0.0.1:8000/main.html in the browser address bar):
Get/main.html http/1.1host:127.0.0.1:8000user-agent:mozilla/5.0 (X11; Ubuntu; Linux i686; rv:34.0) gecko/20100101 firefox/34.0accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q= 0.8accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3accept-encoding:gzip, Deflateconnection:keep-alive
Then I found these interesting things (don't laugh at me, I'm really small white), it was really amazing to see this thing for the first time. See here I instantly understand a lot of things:
1 if I have another server, I can know, those IP access to my server, access to how many times, geographical location.
2 I know what browser he's using.
3 I know what his host system is, and the language/encoding/link type used for this link ...
In fact, our most concern is the first sentence, the first sentence is that it (the client) want to get main.html, meaning that the browser wants to request main.html to the server.
Well, if you want to successfully show the main.html in the browser, you need to send main.html the entire file to the client, but notice the first sentence "get/main.html http/1.1", have found that they have a special form, this is the language of the Web, Call it that for the moment. If we call the Write function directly to send the main.html to the client, the client will feel too confused, it will feel a bit of something, so here the server also in a similar form to the browser first send a similar text and then send the entire main.html file in the past:
http/1.1 ok\r\n
conten-type:html\r\n
The completion code for sending main.html is as follows:
1sprintf (Buffer,"http/1.1 ok\r\n"2 "conten-type:%s\r\n"3 "\ r \ n", mine_type);4 intFile_length=strlen (buffer);5 Do{6 if(Write (Socket_fd,buffer,file_length) <0){7 Close (FD);8 Break;9 }Ten} while(File_length=read (Fd,buffer,sizeof(buffer))) >0);
Where Mine_type is the type of file, such as jif,png,html and so on.
At this point, a complete session is complete, and a super-easy Web browser will be done.
STEP2: Intelligent Furniture Control Module
Strictly speaking, this is the Web server extension module, that is, the server can not only provide the file resources of the Web page, but also call the driver for the operation of the hardware.
For example, to light control, in the HTML page has a light bulb picture button, when the button click, with JavaScript want to send the background of the requested content, such as sending the request content: "myled.cgi", note that the suffix here does not represent the request for a CGI file, Because the server is their own interpretation of the write, want to represent what they decide, TM so capricious and so cool! Here I specify that it represents the light control section.
1 intRECV_LEN=RECV (Client_sockfd,buffer,sizeof(buffer),0);2printf"*****%d bytes recved!***********\n", Recv_len);3P_buffer=buffer;4 if(0==STRNCMP ("get/myled.cgi", Buffer, -))5 {6printf"hello,heat nan!\n");7 Send_light_cmd (P_BUFFER,CLIENT_SOCKFD);8 9}
Above the code, I first determine whether the operation it requested is a light operation, and if so, pass this information as a parameter to the light module for processing.
About the light module is divided into two parts, one is to determine the operation of the lamp, the second part is the implementation of hardware operations, if the switch with the appliance, the main is to set the IO port level with the use of electrical binding.
STEP3: Real-time environment information module
About the real-time environment information module and intelligent Furniture Control module is the same, first when the user into the environment information interface, AJAX actively to the server every 10s to send a temperature request, the server received instructions, immediately call the temperature sensor drive to collect the temperature, and then pass the temperature to the front-end HTML page, So that users every 10ms to see the temperature of the home at once, also calculate real-time it!
At this point, a home remote control system even if completed, can not be called system it! Feel so small at this time, call a small demo!
At this moment, the home appliance is indeed connected to the Internet, it should be called the "Internet of Things!" ”
If the collected temperature values are constantly and set preferences temperature value for comparison so that the program automatically control the air conditioning and underfloor heating switch, this should be called "Smart home!" ”。
However, this should be the reality of the internet of things;
The ideal Internet of things should look like this:
Your air conditioning will be supported by a huge data platform, which connects the most authoritative health institutions, which can provide the healthiest temperature at different times/different time periods, your air-conditioning will communicate with your mobile phone, the opportunity to tell the air conditioning, the child master is 10 kilometers away from home, please work immediately. Air conditioning should have memory function, remember the host in different times, different seasons favorite temperature, your air conditioning should be connected with your hand ring, when you are in the left hand with a slow motion, the hand ring will send instructions to the air conditioning, the child master heart rate, body temperature rise, speed, and will not make you sweat.
If this naïve coming, please call him the real age of the Internet of Things!
IoT practice--step-by-step "bare-handed" establishment of intelligent furniture remote control system