Communication Protocols for the Internet of Things

Communication Protocols for the Internet of Things



The purpose of communication is to convey information

In order to achieve data transmission between devices, we must consider finding the most suitable connection method between objects. These communication methods have their own advantages and disadvantages, and are suitable for different application scenarios.

The communication methods of the Internet of Things can be roughly divided into the following categories

Please add a picture description

The wired communication of the Internet of Things is the communication method with physical lines such as Ethernet and optical cable. The IEEE802.3 standard formulates the standard of Ethernet. Ethernet uses twisted pair as the medium, and can cover 200 range of meters. Wired communication can guarantee the quality, delay and cost of communication, and can also operate normally in harsh environments, but the problems of small communication range and inconvenient portability cannot be solved.

There are many wireless communication technologies for the Internet of Things, which are mainly divided into two categories: one is short-distance communication technologies such as Zigbee, WiFi, Bluetooth, and Z-wave; the other is LPWAN (low-power Wide-Area Network, low-power wide-area network ), that is, wide area network communication technology. LPWA can be divided into two categories: one is LoRa, SigFox and other technologies that work in unlicensed spectrum; the other is 2/3/4G cellular communication technology that works in licensed spectrum and supported by 3GPP, such as EC-GSM, LTE Cat-m, NB-IoT, etc. High-speed services mainly use 3G and 4G technologies; medium-speed services mainly use GPRS technology. Low-rate services are currently not satisfied by good cellular technology, but it has a variety of application scenarios. In many cases, it can only be supported by GPRS technology.

Please add a picture description
Here we can see that Lora and NB-IOT are quite similar. The main difference is that Lora spectrum is free in some segments (can be self-built), while NB-IOT needs to run on the operator. For long-distance transmission requirements that require video streaming, only 4G+WIFI is suitable for networking.

Wireless communication can also be divided into unlicensed spectrum wireless connection and licensed spectrum wireless connection, that is, paying to rent the network.
Licensed spectrum wireless connection is our common cellular communication, which is the operator's network
1G: AMPS
2G: TDMA, CDMA, GSM...
3G: UMTS, HSDPA, WCDMA...
4G: LTE, EC-GSM, eMTC...

Unlicensed spectrum wireless connection can be further divided into short-range communication and wide- area
communication
. )

Wide area communication
Lora

1. UART serial communication

The English abbreviation of serial communication is UART (Universal Asynchronous Receiver Transmitter) and its full name is Universal Asynchronous Receiver Transmitter.

1.1 Serial communication

Please add a picture description

The two devices are connected in series with a wire. The sender converts the data into a binary sequence at one end of the line, sends the 01 signal sequentially with high and low levels, and the receiver reads the high and low levels on the signal line at the other end of the line. The signal corresponds to the 01 sequence converted into binary. This is the most basic concept of serial communication,

1.2 Asynchronous transceiver

insert image description here
If you add a line to the two devices in the above picture, so that the left device can also become the receiver, and the right device can also become the sender, then for the left and right devices, sending and receiving can be done in two Online at the same time, at this time, sending and receiving are asynchronous.

1.3 Baud rate

The baud rate is the binary number of 0 and 1 transmitted per second. The baud rate of the receiver and the sender must be the same, otherwise garbled characters will occur.Please add a picture description

Because it is inconvenient to ensure that the baud rate of the transceiver is consistent every time, some clock-based communication protocols, such as I2C bus protocol and SPI bus protocol, use the clock line to ensure the synchronization of information transmission and reception.

1.4 Data frame of serial communication protocol

In serial communication, the most basic frame of data includes at least start bit + data bit + stop bit (check frame is optional).
The direction of data transmission is generally the most significant bit MSB.
In the UART serial port, RX represents the information receiving end, and TX represents the information sending end.

1.5 Advantages and disadvantages

1.5.1 Advantages

(1) Simple hardware
(2) Low latency
(3) Direct connection

1.5.2 Disadvantages

(1) Point-to-point communication, not suitable for complex network communication
(2) The communication distance and rate are limited by the length and quality of the serial cable
(3) Lack of agreement, the communication parties need to define their own message format and analysis rules

Two , I2C

2.1 I 2 C

I²C (Inter-integrated Circuit) is a bus protocol developed and designed by Philips in 1982. The I²C bus supports short-distance communication between devices and is used for the interface between the processor and some peripheral devices. It only needs two signal lines to complete the information exchange.

2.2 I 2 C

Please add a picture description

I²C requires at least two lines, similar to asynchronous serial ports, but can support multiple slave devices. Unlike SPI, I²C can support multi-master (mul-master) systems, allowing multiple masters and each The master can communicate with all slaves (masters cannot communicate through I²C, and each master can only use the I²C bus in turn). The master refers to the device that initiates data transmission and generates a clock signal on the bus to drive the transmission, while the addressed communication devices are all slaves.

I²C communication requires only two bidirectional buses: serial data line SDA and serial clock line SCL

2.3 Data Validity

The data on the SDA line must remain stable during the high period of the clock, and the high or low state of SDA can only be changed when the clock signal on the SCL line is low.

2.4 Start condition S and stop condition P

Start condition S: When SCL is high, SDA transitions from high to low;

Stop condition P: When SCL is high, SDA transitions from low to high.

Please add a picture description

Start and stop conditions are typically generated by the host. The bus is in the busy state after the start condition, and the bus is in the idle state again after a certain period of time after the stop condition.

2.5 Data format

The data transmitted by I2C is in bytes, and each byte must be 8 bits, and any number of bytes can be transmitted. The data format of I2C has the following characteristics:

(1) Each byte must be followed by a response bit ACK ( Please add a picture description
ACK on SCL), so it takes 9 bits to actually transmit one byte (8 bits) of data.

(2) The highest bit of the byte is first transmitted on SDA. From the above figure, we can see that the order of sending the number of digits is Bit7-Bit0 from left to right

After receiving one byte of data transmitted, the data receiver needs to give a response. At this time, at the ninth clock, the sender releases the SDA line control right, pulls the SDA level high, and is controlled by the receiver. The receiver indicates that it has successfully received the data of 8 bits and one byte, and then pulls SDA to a low level, that is, the ACK signal, indicating a response

2.6 Protocol Data Unit PDU

Please add a picture description

2.7 Advantages and disadvantages

2.7.1 Advantages

(1) The I 2 C bus can connect multiple devices, using a unique address for addressing
(2) Suitable for medium-speed communication requirements
(3) Less hardware resource occupation

2.7.2 Disadvantages

(1) I 2 C communication is limited by distance and is suitable for short-distance communication
(2) I2C address of the device needs to be configured
(3) Real-time performance is poor

3. SPI

3.1 SPI

SPI is the abbreviation of Serial Peripheral Interface (Serial Peripheral Interface). It is a synchronous serial interface technology introduced by Motorola, which is a high-speed, full-duplex, synchronous communication bus. The SPI protocol is mainly used in short-distance communication systems, especially embedded systems, and many chip peripherals, such as LED display drivers, I/O interface chips, UART transceivers, etc., widely use the SPI bus protocol.

3.2 Communication principle

The communication principle of SPI is very simple. It works in a master-slave mode. This mode usually has a master device and one or more
slave devices. In English, the master device is usually called Master, and the slave device is called Slave.
Please add a picture description
SPI theoretically requires 4 wires for bidirectional data transmission, and 3 wires for unidirectional transmission: The
theoretical 4 wires of SPI are as follows Four kinds:
Please add a picture description

3.3 Data Validity

Please add a picture description

3.4 Advantages and disadvantages

3.4.1 Advantages

(1) Support full-duplex communication, sending data and receiving data can be carried out at the same time.
(2) Simple communication
(3) Fast data transmission rate

3.4.2 Disadvantages

(1) The wiring is complicated, requiring at least four wires
(2) In the case of multiple slaves, each slave needs to be connected to a CS chip selection signal line, which wastes the IO resources of the chip.

4. Socket communication

4.1 Socket

Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the TCP/IP layer into several simple interfaces for the application layer to call to realize the communication of the process in the network. Socket originated in UNIX. Under the idea that everything in UNIX is a file, inter-process communication is named as a file descriptor (file descriptor). Socket is an implementation of the "open-read/write-close" mode. Servers and clients Each end maintains a "file". After the connection is established and opened, it can write content to the file for the other party to read or read the content of the other party, and close the file when the communication ends.

In addition, the location of the Socket we often talk about is as follows:
Please add a picture description

4.2 TCP and UDP

There are many kinds of sockets in the world, such as DARPA Internet address (Internet socket), path name of local node (Unix socket), CCITT X.25 address (X.25 socket), etc. We only introduce the first type of socket - Internet socket, which is the most representative, classic and most commonly used. When we refer to sockets in the future, we refer to Internet sockets. Depending on how the data is transferred, Internet sockets can be classified into two types.

4.2.1 Stream format socket SOCK_STREAM

Stream Sockets (Stream Sockets), also known as "connection-oriented sockets", is a reliable, two-way communication data stream, the data can arrive at another computer without error, and if damaged or lost, it can be restarted. send. Its characteristics:
(1) Data will not disappear during transmission;
(2) Data is transmitted in order;
(3) Data sending and receiving are not synchronous (some tutorials also say "there is no data boundary").
It uses the TCP protocol (The Transmission Control Protocol, Transmission Control Protocol), the TCP protocol will control your data to arrive in order and there is no error.
The http protocol used by browsers is based on connection-oriented sockets, because the data must be guaranteed to be correct, otherwise the HTML loaded will not be parsed.

4.2.2 Datagram format socket SOCK_DGRAM

Datagram Sockets (Datagram Sockets) are also called "connectionless sockets". The computer only transmits the data and does not check the data. If the data is damaged during transmission or does not reach another computer, there is no way to remedy it. In other words, if the data is wrong, it is wrong and cannot be retransmitted. Because datagram sockets do less verification work, they are more efficient than stream format sockets in terms of transmission efficiency. It has the following characteristics:
(1) Emphasizes fast transmission rather than transmission order;
(2) The transmitted data may be lost or damaged;
(3) Limit the size of each data transmission;
(4) Data sending and receiving are synchronous
data The packet socket also uses the IP protocol for routing, but it does not use the TCP protocol, but uses the UDP protocol (User Datagram Protocol, User Datagram Protocol).
QQ video chat and voice chat use SOCK_DGRAM to transmit data, because the efficiency of communication must be ensured first, and the delay should be minimized, while the correctness of data is secondary. Even if a small part of data is lost, video and audio can be normal Analysis, at most noise or noise will not have a substantial impact on the communication quality.

4.3 Socket communication process

Socket guarantees communication between different computers, that is, network communication. For websites, the communication model is between server and client. A Socket object is established at both ends, and then data is transmitted through the Socket object. Usually the server is in an infinite loop, waiting for a connection from a client. The following is a connection-oriented TCP sequence diagram:
Please add a picture description

4.4 Advantages and disadvantages

4.4.1 Advantages

(1) It is suitable for network communication and can communicate between different devices
(2) TCP socket provides reliable data transmission to ensure data accuracy and sequence
(3) Supports multiple programming languages

4.4.2 Disadvantages

(1) The programming is more complicated, and needs to deal with connection and disconnection, etc.
(2) It involves the configuration of IP addresses and ports, and it needs to be configured correctly to establish a communication connection
(3) It needs to be used in a network environment and is not suitable for direct connection to physical hardware superior

5. MQTT

MQTT is an application layer protocol, which is at the same level as Hypertext Transfer Protocol (HTTP).

5.1 Message Queue Telemetry Transport MQTT

The original socket communication cannot guarantee that information can reach the receiver, and the reliability of data including real-time performance will have a certain impact. Therefore, a network communication protocol Protocol is needed at this time to ensure the transmission of information and the quality of service. The basic network protocol of the Internet is TCP/IP. MQTT (Message Queuing Telemetry Transport) is built on top of the TCP/IP protocol stack and has become the standard for IoT communication.

5.2 MQTT publish and subscribe model

There are several roles in the IOT network based on the MQTT protocol: Please add a picture description
(1) Publisher is responsible for publishing messages, such as sensors collecting data, and then sending current sensor information

(2) Subscriber Subscriber subscribes to the message and makes corresponding actions according to the obtained sensor data.

(3) *Server Server * Information transfer station, responsible for delivering information from publishers to subscribers.

The implementation of MQTT on the client side is called MQTT Client, and the implementation on the server side is called MQTT Broker.

Each single-chip microcomputer (Client) only maintains a long connection with the PC (Server), and tells the Server if there is any data. If there are other single-chip microcomputers or PCs communicating with this single-chip microcomputer, it can only be obtained through this Server. At the same time, it should also be noted that this The process of data acquisition is passive, the single-chip microcomputer does not actively poll, the whole process is asynchronous, the data is transmitted, and the callback function is automatically called, so the Server becomes the only channel for the single-chip microcomputer to communicate with the peripheral device. This mechanism makes the whole The process is more lightweight and efficient.

5.3 Communication protocol flow

Please add a picture description
The data frame Data Frame used for communication is mainly composed of two parts: Topic ID and Message.

There is no way for publishers and subscribers to directly perceive the existence of each other. Subscribers and publishers can obtain the data they want through the topic number Topic ID in the data frame Data Frame.

Give an example of remotely controlling the LED light to turn on and off:

(1) Connect the ESP32 and the PC to the same LAN, and obtain the IP address of the PC

(2) Start the MQTT Broker on the PC, and turn on the Server mode

(3) ESP32 passes in the IP address and port number of the PC, and creates an MQTT_Client

(4) MQTT_Client of ESP32 creates a long connection with MQTT_Server on PC

(5) MQTT_Client of ESP32 subscribes Topic LED_CONTROL

(6) Create a CLIENT on the PC, pass in the local IP and the port number of the MQTT Broker service in the Client, and establish a long connection with the Server on the PC

(7) Client on PC, send data frame Topic ID + instruction, Topic ID is LED Control

(8) The data is sent to the Server, and the Server finds that the ESP32 development board has subscribed to the LED_CONTROL topic, and then sends the data frame through the connection created between the ESP32 and the Server.

(9) ESP32 receives this data frame and finds TOPIC_ID: LED_CONTROL, so it knows that this is an instruction related to LED control, reads the MESSAGE is LED_ON, ESP32 executes the instruction led.on(), and the LED turns on.

5.3 Advantages and disadvantages

5.3.1 Advantages

(1) Asynchronous message protocol
(2) Persistent connection-oriented
(3) Two-way data transmission
(4) Lightweight protocol
(5) Passive data acquisition

5.3.2 Disadvantages

(1) It is necessary to build an MQTT server, which increases the complexity of the system
(2) It is not suitable for large-scale networks
(3) Communication has a certain overhead, which may affect applications with high real-time requirements

6. Summary

6.1 Wired and wireless

If you can use wired, use wired, data quality is the first guarantee.

6.2 Authorization and non-authorization

If the effect of the two wireless communication methods is not much different and can meet the requirements, it can be done without spending money, which can reduce the later cost.

6.3 Bluetooth and Zigbee

Why are basic mobile phones and other devices now equipped with Bluetooth as standard instead of zigbee? One is the cost, and the other is the ecosystem built by zigbee's big brother Philips. But Zigbee has to respond faster. If you have this demand, you can consider Zigbee.

6.4 RFID and NFC

RFID is radio frequency identification technology, which is identification; NFC is near field communication technology, which is communication.

6.5 MQTT and COAP

MQTT and HTTP are TCP-based protocols, and CoAP (Constrained Application Protocol) is a UDP-based protocol.
MQTT is a persistent connection, and the CoAP protocol is connectionless.
MQTT is a many-to-many protocol that transmits messages through intermediate agents, and CoAP protocol is a one-to-one protocol for message transmission between Server and Client.
MQTT does not support messages with type or other tags to help clients understand. CoAP has built-in content negotiation and discovery support, allowing devices to spy on each other to find ways to exchange data.

Reference articles: http://wiki.1zlab.deepsenserobot.com/wiki/micropython-esp32/mqtt/, https://www.cnblogs.com/myitnews/p/13790067.html
https://zhuanlan.zhihu.com /p/510549726
https://club.gizwits.com/thread-7555-1-1.html

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/131957016