Implementation of IoT Functions Under MQTT Protocol

Basic Definition

The full name of the MQTT protocol is (Message Queuing Telemetry Transport), which is a lightweight communication protocol based on the publish/subscribe model. Many textual descriptions of the MQTT protocol can be found online: it is a lightweight communication protocol based on the Publish/Subscribe model and built on top of the TCP/IP protocol. The TCP protocol itself has high reliability characteristics, so the MQTT protocol, which is based on it, also has the characteristics of high reliability and low overhead.

Protocol Characteristics

This description is very official. Below is an explanation of the basic framework of the MQTT protocol based on my personal understanding and usage experience. In the framework of the MQTT protocol, there is a very clear and important concept known as the topic. Data does not directly point to a specific client address but is broadcasted according to a configured topic. All clients can subscribe to a topic to receive data broadcasted to that topic. Devices are divided into clients and servers. Clients are electronic devices that need to send or receive data to/from a specific topic through the MQTT protocol. Clients do not connect directly to each other; all clients are only connected to the server. The subscribed topic determines what data is received, and the sending topic determines to whom the data is broadcasted. Traditional TCP protocols are like WeChat friends, requiring one-to-one data interaction, while MQTT is more like Douyin, where everyone who follows (subscribes) can receive broadcast data from that topic.

Usage Scenarios

The data interaction method based on publish and subscribe makes MQTT-based communication very flexible, and since MQTT is implemented based on the TCP/IP protocol, this ensures the stability of the transmission. Of course, MQTT also has its limitations and cannot meet the requirements for long-term transmission of large data volumes. However, MQTT is designed to meet the needs of small data volume flexible communication in IoT environments, where there is no need to consider long-term transmission of large data volumes.

Below is an example of remote control based on the MQTT protocol:

Implementation of IoT Functions Under MQTT Protocol

As shown in the figure, this is an MQTT client that requires filling in the MQTT server’s account and password. Clicking connect will feedback success. control@dn is the topic I published control data to, and feedback@dn is the topic I subscribed to for feedback data. As can be seen from the figure,

Send to Topic: control@dn QoS: 2

{

“type”:”event”,

“key”:1

}

The response is sent from the device to

Topic: feedback@dn QoS: 0

{“mac”:”de4f22a0cd5a”,”type”:”Socket-mini-base-02″,”version”:”2.0.2″,”key”:1,”wifiLock”:0,”keyLock”:0,”ip”:”192.168.8.240″,”ssid”:”SHoffice”}

Thus, the communication function of MQTT data is realized (the sent key represents the action of opening and closing, and the received key represents that it has been closed).

Protocol Summary

The MQTT protocol is very flexible. The server can be understood as a data routing agent, determining which client data received is sent to all devices subscribed to that topic through the subscribed and published topics. Each client can subscribe to multiple topics and can also publish data to multiple topics, only clarifying the topic for sending data and the topic for receiving data. In other words, client data interaction is only oriented towards topics, without directly facing devices, eliminating the need to consider the detailed configuration of communication with other devices, simplifying the communication process while allowing flexible configuration of powerful communication functions.

Implementation of IoT Functions Under MQTT Protocol

Scan to Follow

Shenzhen Youni Creative Technology Co., Ltd.

“Source Factory” for R&D Services

36 years of physical enterprise, providing long-term stable R&D services

Leave a Comment