A brief introduction to the MQTT protocol of the Internet of Things

The Internet of Things was once considered to be the third wave of the information technology industry after computers and the Internet. With the continuous improvement of basic communication facilities, especially the emergence of 5G, the threshold and cost of the Internet of Everything have been further reduced. The Internet of Things itself is also one of the good landing scenarios for AI and blockchain applications, and major cloud service providers are also launching IoT platforms and services one after another.

IoT communication is a core content of IoT. At present, there is no unified standard for IoT communication protocols. Common ones include MQTT, CoAP, DDS, XMPP, etc. Among them, MQTT (Message Queue Telemetry Transmission Protocol) should It is one of the most widely used standards. At present, MQTT has gradually become the most popular protocol in the IoT field, and it is also the most mainstream transmission protocol for major IoT platforms at home and abroad. Many devices on the Alibaba Cloud IoT platform are connected through MQTT.

1. Introduction to MQTT

MQTT (Message Queuing Telemetry Transport, message queue telemetry transmission) is described in the book "MQTT Protocol Specification Chinese Version":

MQTT is a message transmission protocol based on the publish/subscribe mode of the client server architecture. Its design philosophy is lightweight, open, simple, standardized, and easy to implement. These characteristics make it a good choice for many scenarios, especially for constrained environments such as machine-to-machine communication (M2M) and Internet of Things (IoT) environments. ----MQTT protocol Chinese version

Like the HTTP protocol, the MQTT protocol is also an application layer protocol, working on the top layer (application layer) of the TCP/IP four-layer model, and built on the TCP/IP protocol. The biggest advantage of MQTT is that it can provide real-time and reliable message services for connected remote devices with very little code and limited bandwidth. As a low-overhead, low-bandwidth instant messaging protocol, it has a wide range of applications in the Internet of Things, small devices, and mobile applications.

The IOSI seven-layer or tcp/ip five-layer or tcp/ip four-layer model is shown in the figure below:

Today, MQTT has become the most popular IoT protocol and has been widely used in the fields of Internet of Vehicles, smart home, instant chat applications and industrial Internet. At present, there are more than 100 million devices connected through the MQTT protocol, all of which benefit from the stable, reliable and easy-to-use communication foundation provided by the MQTT protocol for devices.

2. The main features of MQTT

The MQTT protocol is a protocol designed for communication between remote sensors and control devices working on low-bandwidth, unreliable networks. It has the following main characteristics:

①. Use the publish/subscribe message mode to provide one-to-many message publishing and decouple application programs.

②, Provide network connection based on TCP/IP. The mainstream MQTT is based on the TCP connection for data push, but there is also a UDP-based version called MQTT-SN.

③. Support QoS service quality level. Set different quality of service levels according to the importance of the message.

④. Small-scale transmission, small overhead, and minimized protocol exchange to reduce network traffic. This is why it is said in the introduction that it is very suitable for "in the field of Internet of Things, communication between sensors and servers, and information collection". It is necessary to know that the computing power and bandwidth of embedded devices are relatively weak, and using this protocol to transmit messages is more suitable. However, in terms of mobile phone applications, MQTT is a good Android message push solution.

⑤. Use the will will mechanism to notify the client of abnormal disconnection.

⑥. Publish/subscribe messages based on topics, and block message transmission for payload content.

⑦, support heartbeat mechanism.

3. History of MQTT

The original version of the MQTT protocol was established in 1999, and the inventors of the protocol are Andy Stanford-Clark and Arlen Nipper. MQTT was originally used for data transmission between sensors and satellites in oil pipelines. They were developing a project to use satellite communication to monitor oil pipelines. In order to realize the requirements of this project, they needed to develop a communication protocol for embedded devices. This communication protocol must meet the following conditions:

⚫ Easy to implement, the server must realize the access of thousands of clients

⚫ The service quality of data transmission is controllable, and different levels of service quality can be set according to the importance and characteristics of the data

⚫ Occupies a small amount of bandwidth and a small amount of data per time, but it cannot make mistakes

⚫ Must be able to adapt to the risks of unreliable network communication such as high latency, dropped calls, and disconnected networks

⚫ The connection status of the device is known, and the cloud and the device maintain a long connection

From the above conditions, we can see that:

⚫ The MQTT server can connect a large number of remote sensors and control devices, maintain long-term connections with remote clients, and have certain real-time performance.

⚫ The cloud sends a message to the device, and the device can receive and respond within the shortest possible time.

⚫ MQTT is more suitable for occasions that require real-time control, especially for actuators.

⚫ The cloud and the client need to maintain a long-term connection. To be able to obtain the connection status of the device, it is necessary to send heartbeat packets from time to time, which will not save power. Therefore, MQTT is not suitable for low-power situations.

It can be seen that MQTT was designed for low-bandwidth, high-latency or unreliable networks from the beginning. Although after decades of updates and changes, the above features are still the core features of the MQTT protocol. But unlike the original, the MQTT protocol has expanded from embedded system applications to the open Internet of Things (IoT) field.

4. MQTT version

Currently, there are two mainstream versions of MQTT, namely MQTT3.1.1 and MQTT5. MQTT3.1.1 was released in October 2014, and MQTT5 was released in March 2019. Although the time difference between MQTT3.1.1 and MQTT5 is nearly five years, MQTT3.1.1, as a classic version, is still the mainstream version and can meet most actual needs.

MQTT5 is upgraded on the basis of MQTT3.1.1, so MQTT5 is fully compatible with MQTT3.1.1. On the basis of MQTT3.1.1, MQTT5 adds more functions and complements and improves the MQTT protocol.

5. MQTT protocol

MQTT is a message transmission protocol based on client-server architecture (C/S), so in MQTT protocol communication, there are two most important roles, they are server and client.

1) server

The MQTT server is usually a server (broker), which is the hub of MQTT information transmission and is responsible for passing the information sent by the MQTT client to the MQTT client; the MQTT server is also responsible for managing the MQTT client to ensure that the client The smooth communication among them ensures that the MQTT information can be received and delivered correctly.

2) client

The MQTT client can publish information to the server, and can also receive information from the server; we call the behavior of the client sending information as "publishing" information. If the client wants to receive information from the server, it must first "subscribe" to the server. The operation of "subscribing" information is very similar to "following" an official account when we use WeChat. When the author of the official account publishes a new article, the official WeChat will send a message to all users who have followed the official account, telling They are updated with new articles for users to check.

3) MQTT topics

As we mentioned above, if the client wants to obtain information from the server, it first needs to subscribe to the information, so how does the client subscribe to the information? Here we want to introduce the concept of "topic (Topic)", "topic" is a very important concept in MQTT communication, the client publishes information and subscribes information around the "topic", and the MQTT server is managing When sending MQTT information, it is also controlled by "topic".

When a client publishes a message, it needs to specify a "topic" for the message, which means publishing the message to the topic; and for the client who subscribes to the message, it can subscribe to the message by subscribing to the "topic", so that when other clients or themselves ( When the current client) publishes a message to the topic, the MQTT server will send the information of the topic to the subscribers (clients) of the topic.

How the server controls the information communication between the clients through the "topic", see the example in the figure below:

In the above illustration, there are three MQTT clients, which are development board, mobile phone and computer. The MQTT server uses "topics" to manage information when managing MQTT communication.

For example, as shown in the figure above, assuming that we need to use the mobile phone and computer to obtain the temperature of the SoC chip of the development board during operation, then first the two clients, the computer and the mobile phone, need to subscribe to the topic "chip temperature" from the MQTT server; next, when After the development board client publishes information to the "chip temperature" topic of the server (assuming that the content of the information is the current temperature value), the server will first check which clients have subscribed to the "chip temperature" topic information, And when it finds that the client that has subscribed to the topic has a mobile phone and a computer, the server will forward the "chip temperature" information just received to the mobile phone and computer clients that have subscribed to the topic.

Through the above example, the mobile phone and computer can obtain the temperature value of the SoC chip when the development board is running.

In the above example, the development board is the publisher of the "chip temperature" topic, while the mobile phone and computer are the subscribers of the topic.

It is worth noting that the role of the MQTT client is often not single when communicating. A client can be both an information publisher and an information subscriber at the same time. As shown below:

All the clients in the picture above communicate around the topic of "LED control". At this time, for the topic of "LED control", the mobile phone and computer clients become the publishers of the MQTT information and the development board becomes the subscriber (receiver) of the MQTT information.

所以由此可知,针对不同的主题,MQTT 客户端可以切换自己的角色,它们可能对主题 A 来说是信息发布者,但是对于主题 B 就成了信息订阅者,所以一个 MQTT 客户端它的角色并不是固定的,所以大家一定要理解“主题”这个概念。

4)MQTT 发布/订阅特性

从以上实例我们可以看到,MQTT 通信的核心枢纽是 MQTT 服务端,它负责将 MQTT 客户端发送来的信息传递给 MQTT 客户端,还负责管理 MQTT 客户端,以确保客户端之间的通讯顺畅,保证 MQTT 信息得以正确接收和准确投递。

正是因为有了服务端对 MQTT 信息的接收、储存、处理和发送,客户端在发布和订阅信息时,可以相 互独立、且在空间上可以分离、时间上可以异步,这就是 MQTT 发布/订阅的特性:客户端相互独立、空间上可分离、时间上可异步,具体介绍如下:

⚫ 客户端相互独立:MQTT 客户端是一个个独立的个体,它们无需了解彼此的存在,依然可以实现信息交流。

⚫ 空间上分离:空间上分离相对容易理解,MQTT 客户端以及 MQTT 服务端它们在通信时是处于同一个通信网络中的,这个网络可以是互联网或者局域网;只要客户端联网,无论他们远在天边还是近在眼前,都可以实现彼此间的通讯交流;其实网络通信本就是如此,所以并不是 MQTT 通信所特有的。

⚫ 时间上可异步:MQTT 客户端在发送和接收信息时无需同步。这一特点对物联网设备尤为重要,前面我们也介绍了,MQTT 从诞生之初就是专为低带宽、高延迟或不可靠的网络而设计的,高延迟和不可靠网络必然就会导致时间上的异步;物联网设备在运行过程中发生意外掉线是非常正常的情况。

6、总结

向大家介绍了 MQTT 通信的基本原理,在 MQTT 通信中,1 个服务端、多个客户端之间围绕“主题”进行了通信,所以重要在于大家需要理解各个客户端的相互关系以及服务端在其中所起的作用,并且理解“主题”这个概念以及 MQTT 发布/订阅模式的特性,后面向大家介绍具体的通信过程时,要迅速的反应过来。

注意:对于 MQTT 发布/订阅模式的特性,我们总结的几个特点中都有一个“可”字。这 个“可”字意味着客户端彼此之间可以独立,空间可以分离,时间可以异步。在我们实际应用中,客户端之间的关系既可以独立也可以相互依存。在空间上,既可以相距甚远,也可以彼此相邻。在时间上,既可以异步也可以同步。这个“可”字所体现的是 MQTT 通讯的灵活性。

Guess you like

Origin blog.csdn.net/qq_36564503/article/details/129217398