An overview of Internet programming and web programming for IoT

Source: Internet
Author: User

This article is based on the embedded IoT Development Engineer's vision of network programming and web programming. This article may be a little easier for children's shoes that focus on the development of the Java back-end service. But network programming and web programming are a vacuum for most embedded IoT engineers.

Indeed, IoT research and development should be done in a team-based collaborative way, so there are embedded devices, gateways, web front-end, apps, backend development and other dedicated posts. As a system architect, Nature needs to master the key technologies of various positions. As an embedded engineer, master network programming, Web programming, can greatly expand their horizons and architectural thinking, be able to proactively to the system's various protocols and application scenarios to put forward the optimization of ideas, rather than just accept the task to be assessed. At the very least, you can quickly build an Internet of things demo system without relying on back-end engineers. Therefore, mastering some basic network programming and web programming skills is very important for improving the development ability of IoT research engineers.

This article can be considered as the first original technology sharing of the embedded penguin ring released WiFi access solution. WiFi Access Program series technology sharing will be open after the Spring Festival in 2016, please pay attention. This article provides an overview of the knowledge involved in the Internet of things, followed by detailed guidance in the development of the article.

I. OSI seven-layer model and TCP/IP four-layer model

The OSI seven-layer model is a theoretical research model of network protocols, or it can be called an ideal model, and the TCP/IP four layer model is the standard of fact and a widely used model. The correlation between the two is illustrated below:

For the interpretation of the two models, I would like to say that as developers do not have to force to understand the meaning of each layer, such as what the conversation layer is responsible for, the presentation layer is responsible for what. It is difficult to understand and remember when you are not involved in the development process to solve the problems of these levels. However, when you have a problem and need to solve it, you will certainly be very clear on these levels of responsibility.

The key factor in measuring the utility of an Internet of things platform or protocol is its ability to deliver messaging that directly affects the development of IoT applications. So, we analyze the fact Standard model of TCP/IP from the message's ability to reach. We envision the following scenarios and analyze them.

1. Network interface layer. Router 1 and WiFi speakers, air conditioning, water heaters constitute a home area network, which uses the WiFi (802.11) protocol to communicate. The protocol defines the physical signal, data frame format, packet loss mechanism, flow control and so on. These are the tasks of the network interface layer. Also, multiple devices share the channel, while sending data will conflict, it is how to solve, this is the content of the network interface layer. In fact, IoT engineers don't have to worry about these things. Because the WiFi physical signal content is the WiFi chip manufacturer is responsible, and the WiFi single chip (WIFI+SOC) will provide the SDK package and provide socket programming interface. Therefore, our responsibility is to focus on the network layer above the programming development knowledge.

2. Network layer, IP protocol, the most basic understanding is that each IP corresponds to a device, mobile phone or a rear server. In principle, a network card corresponding to an IP, WiFi speakers, WiFi water heaters have a separate IP. Communication between networks is based on IP, and network packets are eventually sent to the device on the target IP via the router.

Wi-Fi speakers and other home devices to join the home LAN, in fact, each get a LAN ip,192.168.*.*, including Router 1 also has a LAN address, but Router 1 also has an Internet IP. Only the router's Internet IP can be learned, the outside world is not actively aware of the local area network IP specifically corresponding to which device, only Router 1 know, so all outgoing packets of the source IP is Router 1 of the Internet IP, the outside world sent to the device's target IP is the router's Internet IP.

As we all know, when the device is on-line, it needs to tell its status to the IoT platform server for user control. Therefore, the IP of the IoT platform server must be an Internet IP, or a domain name (the DNS protocol can resolve the domain name to IP). If it is a LAN IP, that device is not accessible.

Here, we also have to remember that the first communication between the device and the IoT platform should always be initiated by the device, because even if the IoT platform knows the Router 1 's public IP, it cannot deliver the message to the internal device. In addition, the server must keep the routing information for the last hop of the device to reach the IoT platform, because the route information is returned in the process of having Router 1 record which device is emitting information. If this information is not recorded, the IoT platform alone can not reach the specific device by the Internet IP of Router 1.

If you do not understand the above paragraph, remember that the IoT platform through the Internet IP Router 1 to actively send a message to the device is not successful, but when the device sends a message to the IoT platform, the IoT platform directly respond to the packet (IP source address and destination address swap location) can reach the device. If the answer is to satisfy the way, then the server does not need to record this routing information, if you need to meet the server to actively send messages to the device scene, then the server needs to record this information.

In addition, we know that the network device is physically represented as a real NIC, the MAC address of the NIC is 6 bytes, 48 bits. In a LAN communication, network programming is based on IP address, router or switch how IP address to find the corresponding MAC address, that is, the ARP Address Resolution Protocol, this is the responsibility of the network layer, but as a developer, we understand.

3. Transport layer, which is the TCP/UDP protocol. For the transport layer, we need to understand that a PC or mobile phone running on the network can be used a lot, but they all correspond to the same IP. How does the operating system distribute a packet to the corresponding network application? This is differentiated by the ports defined by the transport layer. The Common Network Application layer protocol will be the default transport layer port number, such as FTP corresponding to 21,http corresponding to 80,smtp corresponding to 25 and so on. In addition to defining the port number, the transport layer has two very important protocols, the TCP connection-oriented protocol and the UDP packet protocol. The former can be understood as a virtual telephone connection protocol, once the two parties call to establish a connection after the two sides of the process of communication, all the packets are going the same route path. Because the connection-oriented protocol can be guaranteed by reserving resources in the bandwidth, the connection-oriented protocol guarantees quality, so it is suitable for some network applications with strict data quality requirements, such as e-mail application, if the quality is not guaranteed, the mail content is not guaranteed to be correct, who will use this mail system? However, for some audio and video classes, the loss of one or two frames does not affect the user experience at all, it will use the UDP protocol, which is not a connection-oriented, after the end of the routing information can not be saved, it is the use of maximum effort to deliver (ie trymy best).

4. Application layer. Common network application protocols include HTTP, FTP, SMTP, Pop, and so on. Embedded IoT applications are built on the basis of these network application protocols. These protocols govern the format of Basic request connectivity, response, and data transfer. As an embedded IoT application, it should define the format of the application protocol itself, which can be easily customized or used in mature standard formats such as HTML, XML, JSON, and so on. Since firewalls typically only open HTTP packets with port 80, IoT applications are generally built on HTTP.

So, we want to differentiate between the Network Application Layer protocol HTTP and the application custom protocol. The latter uses the former for transmission communication. Regardless of the format used to apply the custom protocol, both sides of the communication need to be used simultaneously. When the IoT device and the IoT platform communicate in the background, you can use simple XML format or JSON format, and the IoT platform is also accessible by the PC browser, then, because the browser only supports HTML format, then the IoT platform is required to provide HTML format content services, in the same vein, Communication between the Internet of things platform and mobile app can be either XML or JSON. Even, we can customize simple commands to implement functionality, but using XML or JSON formats helps the data be readable and has a well-established class library to explain.

These are based on the HTTP Network application protocol.

Second, socket programming

Socket programming is divided into TCP and UDP two ways. The following are the differences:

As can be seen, the TCP/UDP socket socket requires a bind (BIND) IP and port address before communication. For TCP, the server needs to listen for listen first, and after the client initiates the connect request, the server can accept, then establishes a connection-oriented communication environment and communicates through the SEND/RECV function.

UDP programming is simple, and the data transfer can begin immediately after binding.

In addition to mastering basic socket programming, you need to be aware of the following:

1) blocking and non-blocking. Network sockets have blocking and non-blocking points. If the socket created is blocked, then its recv function must wait until the other side transmits the data to come back, otherwise it will always be in a blocking state. The non-blocking state is to immediately see if the buffer has no data, and if there is a return data, no error is returned, not always death. Blocking mode can be simply understood as synchronous mode of operation, and non-blocking mode can be understood as asynchronous mode of operation.

2) multiplexing. As a server, there may be multiple client connections, and if polling each client socket has no data, it is more efficient. The select and poll interfaces for socket programming are used to solve this kind of multi-io multiplexing problem, which can detect multiple connected data at the same time.

c/S and C/s

1.B/S is a browser and client mode that uses HTML syntax formatting. It uses a question and answer, that is, the server is stateless, it does not know whether the client has been visited before. Stateless helps servers to serve efficiently and stably. But the impact of this pattern on IoT applications is deadly. Because the server cannot actively send messages to the IoT device. So, how to improve it?

1) Ajax technology. Ajax technology is the latest in order to solve the page local refresh frequent efficiency problems. That is, the local data send of an HTML page changes, before the Ajax need to resend a request to refresh the entire page. Ajax is simply a request to the server to send the changed data. The former will see the page flicker when requested, while the latter is not. We can use Ajax to make a request to the server periodically, asking the server if it has updated data. If the query frequency is high, then the real-time effect is good, but will aggravate the server burden. Essentially, it is a question-and-answer form, not a two-way communication. Ajax requires browser technical support.

2) WebSocket technology. WebSocket is a solution to the two-way communication problem of HTML, which after the first HTTP request will allow the server to update the subsequent protocol to WebSocket for communication. WebSocket requires more than tomcat7.0 to run the container technical support.

IoT application development on the device side, you can simulate the HTTP protocol via socket programming, and you can also simulate HTML, XML, or websocket on top of HTTP.

2. C/S

c/S client and server programming before the advent of the smart machine in the PC desktop field was once thought to be gradually replaced by B/s, but in the intelligent machine device side it is rejuvenated. Despite the rapid development of the HTML5, individuals feel that the browser's experience with smartphones and other smart devices is still less than native apps. The bigger advantage of HTML5 is its strong portability.

C/S programming can use the socket communication directly to communicate, there is no problem of communication between the two sides naturally. If C/S programming uses the HTTP class library for programmatic communication, it also has the problem of bidirectional communication.

At present, many people would like to use the Java EE Business architecture to support the Internet of things, but the IoT device is limited resources, some terminals may be simple microcontroller, its run complete TCP/UDP protocol is more difficult, so someone proposed a lite version of the TCP/IP protocol, such as coap (synonymous with Restricted application Protocol (CONSTRAINEDAPPLICATIONPROTOCOL)), Ucip, lwIP, and so on. From the Business application protocol, MQTT developed by IBM may become the standard for IoT application protocols.

Iv. Web Programming

The first thing that Web programming refers to is the syntax programming of browser presentation content. The Web static language is HTML.

1.HTML does not support dynamic changes in data. Therefore, a dynamic language based on the interpretation engine is generated, such as ASP, PHP, JSP and so on. Such languages use HTML/CSS to describe presentation styles, while dynamic languages are used to control the presentation of data, such as accessing databases for new data, and so on.

Need to know, asp,php,jsp these languages are the server programming language, when the user accesses the server corresponding Web page through the browser, the content of the webpage asp/php/jsp and so on will go through the server's explanation engine to translate to the concrete data, finally and other HTML, The CSS data is returned to the browser for presentation. As a result, browsers always get deterministic HTML, CSS, and data, and there are no asp/php/jsp statements.

2.JavaScript script. Scripts are the syntax supported by the browser, not the server's technical support. For example, a login interface, to ensure that user input correctness, such as irregular characters, length is too long, and so on, usually using JavaScript script to detect, and do not need to send a request to the server. The Ajax techniques described above are also scripting techniques supported by browsers.

3.jQuery, it is the encapsulation of JavaScript technology, can better control the front-end programming.

4.html/css/js scripts, called Web front-end programming development, and asp/jsp/php for backend development.

5. Back-end development naturally involves database access, business logic, and the need to interact with the front end. Therefore, the Web application programming architecture generally uses the MVC programming model, that is, M is the data model, is responsible for database access, V is the view, is responsible for the display, C is control. The MVC model is good at separating the presentation from the database and is helpful to the application development.

6. As a whole operating architecture, the server needs to include databases (such as MySQL), Web applications and interpretation engines, and Web services such as Apache and Tomcat. Apache provides HTTP services.

The 7.JSP is based on Java syntax, and the EE architecture provides servlet technology to support network usage. JSP is in fact a high-level package of servlets and as a separate technology, JSP focus on supporting the use of B/s network, and servlet through the way of mapping classes to support the C/S mode of network application, such as Bluetooth access and WiFi access back-end technology to use the servlet support , the top level uses the Xml/json format.

Preview: The embedded Penguin ring will be released after the Spring Festival WiFi access solution. Embedded Penguin Ring Bluetooth Development Board (Support AirSync protocol and general app serial port transmission, can be developed two times, without external MCU) has been released.

An overview of Internet programming and web programming for IoT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.