IoT network programming and web programming

Source: Internet
Author: User

This article is based on the embedded internet of things Research and Development Project division of Visual network programming and Web programming to illustrate.

For the students who focus on the development of the Java backend service, this article may be slightly simpler. However, network programming and web programming are a vacuum neighborhood for most embedded IoT project architects.

Do. IoT research and development should be done in a team collaboration, so there are embedded device end, gateway, web front end, APP, backend development and other exclusive posts. As a system architect. Nature needs to master the key technologies of various positions.

As an embedded project master. Master network programming, Web programming. Can greatly expand their horizons and architectural thinking, you can proactively to the various protocols and application scenarios of the system to optimize the insight, and not only to accept the assignment, at least, can not rely on the backend project division. An Internet of things demo system can be built at high speed.

So. Mastering some of the major network programming and web programming skills is important for improving the development capabilities of the IoT development project.


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

The OSI seven-layer model is a theoretical 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 diagram between the two is as follows:



The key to measuring the usefulness of an internet of things platform or protocol is the ability to deliver messaging that directly impacts IoT application development.

So, we analyze the fact Standard model of TCP/IP from the message's ability to reach. We envision the scenario below and analyze it.


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. And also. Multiple devices share channels, and sending data at the same time can create conflicts. How it's solved. This is also the content of the network interface layer. In fact, IoT project architects 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. That is, the IP protocol. The most basic understanding is that each IP corresponds to a device, a 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 finally sent to the corresponding device on the target IP via the router.

WiFi speakers and other home appliances to increase the home area network. In fact, each obtains a LAN IP. 192.168.*.*, which includes Router 1, also has a LAN address. But Router 1 is another Internet IP. Only the router's Internet IP skills are known to the outside world. The outside world is not actively aware of the LAN IP details of which device. Only Router 1 knows, so all outgoing packets of the source IP are Router 1 of the Internet IP, the outside world sent to the device's destination IP is also the router's Internet IP.

As we all know, it is necessary to inform the IoT Platform server of its status when the device is online 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). Suppose it is a LAN IP. The equipment is impossible to access.

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 about Router 1 's public IP, it cannot deliver the message to the internal device. In addition, the server must continue the routing information that the device has to reach the last hop of the IoT platform, since the route information is returned by the process that has Router 1 record which device is emitting information. Assuming that this information is not recorded, the IoT platform is unable to reach the detailed device by the Internet IP of Router 1 alone.

Assuming that 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 direct response of the IoT platform should be that the packet (IP source address and destination address swap location) is capable of touching the device. The assumption is that the server does not need to record this routing information if it satisfies the one-to-one answer, assuming it needs to satisfy the scenario where the server is actively sending messages to the device. Then the server is required to record this information.

Other than that. We know. The network device is physically represented as a real NIC. The MAC address of the network card is 6 bytes. 48 bits. In a LAN communication, network programming is based on IP address, router or switch how to find the corresponding MAC address by IP address. is the ARP Address Resolution protocol. This is also the responsibility of the network layer, but as a developer. We know, we can.

3. Transport Layer. That is, the TCP/UDP protocol.

For the transport layer, we need to understand that. There may be a lot of network usage on a PC or mobile phone, but they all correspond to the same IP. How does the operating system distribute a packet to the appropriate network application? This relies on the port defined by the transport layer to differentiate. The Common Network Application layer protocol will be the default transport layer port number, such as FTP corresponding 21,http corresponding 80. SMTP corresponds 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-party call process, all the packets are going the same route path.

Because the connection-oriented protocol will reserve resources in the bandwidth to protect. Therefore, the connection-oriented protocol can guarantee the quality, so suitable for some of the data quality requirements of the network application, such as e-mail applications, assuming that quality is not guaranteed, mail content is not guaranteed correct, who will use this mail system? However, for some audio video classes, losing one or two frames does not affect the user experience at all, the UDP protocol is used, which is not connection-oriented. The routing information before the end of the send can not be saved, it is using the 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 the primary request connection, response, and transfer of data. As an embedded IoT application, it should define the format of the application protocol itself, which can be easily defined by itself, and can be used in mature standard formats such as HTML, XML, JSON, and so on. Because firewalls generally only release HTTP packets with port 80, IoT applications are generally built on HTTP.

Therefore, we want to differentiate the Network Application Layer protocol HTTP and apply its own definition protocol. The latter uses the former for transmission communication. Whichever format you use to apply your own definition protocol. Both parties need to use the same time communication.

Both the IoT device and the IoT platform can use a simple XML format or JSON format when communicating back-office. And the IoT platform has to be visited by a PC browser, so. Because the browser only supports HTML formatting. It requires an HTML-formatted content service in the background of the IoT platform, which, in the same vein, can be used in XML or JSON for communication between the IoT platform and the mobile app.

Even, we are able to define simple commands ourselves to implement functionality. However, the use of XML or JSON format to help the data is good readability, and also has a mature 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. Respectively for example the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

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

And UDP programming is very easy, after the binding can immediately start to transfer data.

In addition to mastering the main socket programming, you need to know the following:

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

2) multiplexing. As a server, there may be multiple client connections. Let's say that polling every clientsocket has no data, that's a lot of efficiency.

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 traffic 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 the server to serve efficiently and stably. But the impact of such a pattern on IoT applications is deadly. Because the server cannot proactively 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 sending of an HTML page has changed. You need to send a request again before Ajax to refresh the entire page. Ajax, however, is the only request to the server to send the changed data. The former will see the page flicker when requested, while the latter is not. We were able to use Ajax to periodically make requests to the server to ask if the server had updated data. Suppose the frequency of the inquiry is high. The real-time effect is good, but it can add to the server burden. Essentially, it is a question-and-answer form, not a two-way communication. Ajax requires browser technical support.

2) WebSocket technology. The websocket is intended to solve the two-way communication problem with HTML, which, after the first HTTP request, will allow the server to update the protocol to WebSocket for communication.

WebSocket need to tomcat7.0 above the implementation of container technical support.

IoT application development is capable of simulating the HTTP protocol via socket programming on the device side, the same HTML, XML, or websocket that can emulate HTTP.

2. C/S

C/sclient and server programming before the advent of the smart machine in the PC desktop field was once felt to be gradually replaced by B/S. But it was reborn in the smart device side. Although HTML5 is developing rapidly. But a person thinks the browser in the mobile phone and other smart device side experience is still less than the native app. The bigger advantage of HTML5 is that it has a very strong transplant.

C/S programming can directly use the socket communication to communicate, there is no natural two-party communication problems. Assume that C/s programming uses the HTTP class library for programmatic communication. It also has two-way communication problems.

For now, a lot of people want 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 narrative presentation patterns, while using dynamic languages 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 through the browser access to the corresponding Server Web page, the page's asp/php/jsp and other content will go through the server's interpretation engine into detailed data, and finally with other HTML, CSS data returned to the browser to show. So. The browser always gets a definite HTML, CSS, and data, and there are no asp/php/jsp statements.

2.javascript script.

Scripts are the syntax supported by browser technology. Instead of server technical support. such as a login interface. To ensure that user input is correct, such as irregular characters, too long, and so on, it is common to use JavaScript scripts to detect, without having to send a request to the server. The Ajax techniques described above are also scripting techniques supported by browsers.

3.jQuery. It is a package of JavaScript technology that allows for better front-end programming control.

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

5. Back-end development will naturally involve database access, business logic. And it needs 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 for control. The MVC model allows for good separation of presentation and database. Helps with application development.

6. As an overall implementation 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 side to support the use of B/s network. The servlet supports the use of C/s by mapping classes, such as Bluetooth access and back-end technology for WiFi access, which is supported by the servlet, using the Xml/json format at the top level.























IoT network programming and web programming

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.