MQTT (Message Queuing Telemetry Transport), in human terms, means Message Queuing Telemetry Transport. Some years ago, when the PC side of the prevalence of many engineers simply did not hear a roundabout term, but with the gradual development of the Internet of Things (IoT) technology, this protocol appears more and more frequently in the eyes of major engineers. This has caused many engineers only know the name but not the meaning, and many people even thought that this is a kind of protocol developed with the development of IoT. In fact, the MQTT protocol was first invented more than 20 years ago, and in 1999 Andy Stanford Clark of IBM and Alan Nippe of Cirrus Link wrote the first version of the protocol. The protocol has since been internationally standardized as a publish/subscribe-based messaging protocol under the ISO standard (ISO/IEC PRF 20922.) IBM submitted the MQTT version 3.1 specification to the Structured Information Standards Facilitation Organization in 2013, along with a charter to ensure that only a small number of changes could be made to the specification, and since then the MQTT protocol has been used in a number of niches since then. Once the technical infrastructure of the IoT was finalized, this ancient protocol began to have its first spring.
Transport and Application Layers of the Network
As we all know, the rapid development of the Internet of Things so far can not leave the communication network infrastructure, you can now control any corner of the world in the home of a room light switch, or to do industrial control, you can also remote control of a robot's movement, the maturity of this technology are based on network communication as the basis. The main technology of the current network technology is the OSI seven-layer model, of course, the actual application is actually using the TCP/IP four-layer network model.
TCP / IP four-layer network model of the third transport layer is the famous TCP / IP protocol, this layer of the main purpose of the protocol is used to send a computer on the network communication data transmission to the specified IP address of the other machine above, for example, an IP address "192.168.137.19 For example, if a machine with IP address "192.168.137.19" wants to send a 16-byte binary packet to a machine with IP address "192.168.137.10", then it is possible to use the TCP/IP protocol to transmit it. Instead, when transferring data using TCP, we commonly use sockets.
But when the IP address of "192.168.137.19" machine to send data to the "192.168.137.10" machine, this packet of TCP packets inside the data is actually on behalf of what the meaning of the receiving end of the IP address of the receiving end of the "192.168.137.10" machine how to parse this packet of data, this problem is left to the transport layer above the layer of protocols to solve, which is the application layer protocols. Of course, if your protocols do not want to give the ordinary network of computers on the resolution, you can also go to develop some of their own application layer protocols, it does not matter, the purpose of the transport layer is just to pass the data to the target machine on top of it.
Our daily work, entertainment will often encounter a variety of application layer protocols, such as when you open a web page, the picture is displayed in that position, the button to point down is to achieve what function, this is by the HTML HyperText Transfer Protocol (English: HyperTextTransferProtocol, abbreviation: HTTP) agreed. This ensures that when a page in your website is requested by any device, that device can display it properly. Apart from HTTP, there are many other application layer protocols such as DNS, FTP, etc., and the MQTT protocol, which is our protagonist today, is one of them.
Why IoT favors MQTT
With all the great application layer protocols available for our existing applications, why does MQTT shine in the IoT space. The choice of the MQTT protocol is not unfounded; MQTT is a lightweight, flexible networking protocol that strives to strike the right balance for IoT developers:
This lightweight protocol can be implemented on severely constrained device hardware and high latency/bandwidth limited networks.
Its flexibility makes it possible to support diverse application scenarios for IoT devices and services.
Most developers are already familiar with HTTP web services. So why not let IoT devices connect to Web services? Devices can send their data in the form of HTTP requests and receive updates from the system in the form of HTTP responses. This request and response model has some serious limitations:
HTTP is a synchronization protocol. The client needs to wait for the server to respond. web browsers have this requirement, but at the cost of scalability. In the IoT space, a large number of devices and a network that is likely to be unreliable or have high latency make synchronous communication problematic. Asynchronous messaging protocols are better suited for IoT applications. Sensors send readings and let the network determine the best route and time to deliver them to target devices and services.
HTTP is unidirectional. The client must initiate the connection. In IoT applications, the device or sensor is usually the client, which means they cannot passively receive commands from the network.
HTTP is a one-to-one protocol. The client makes a request and the server responds. Delivering messages to all devices on the network is not only difficult but also costly, which is a common use case in IoT applications.
HTTP is a heavyweight protocol with many headers and rules. It is not suitable for constrained networks.
For these reasons, most high-performance, scalable systems use asynchronous message buses for internal data exchange instead of Web services.
The subscribe/publish model
Interestingly, this MQTT protocol server is actually a much simpler design than a web server, as it seeks to be an efficient service. the mechanism by which MQTT primarily sends and receives messages is somewhat similar to the relationship between our public website and you, the readers.
In the real world, I and you are similar to a have a MQTT device hooked up to a unified server, you subscribe to us out of interest or some kind of affection for our public number, and when every day I send out a text push, you will appear in the cell phone I pushed the message, this process, you get my information in a way known as the In this process, the way you get my information is called "subscription", and the behavior of my posting to this public number is "publishing". And everyone can to my article, you can feel free to leave a message to me, this behavior is everyone's "publish" behavior, and I always keep in front of a push to see everyone's message, this is a kind of "subscription" behavior. In this process, all the external information has nothing to do with us, we simply communicate with the information flow in two directions.The messaging mechanism in MQTT is also based on the Publish - Subscribe model. MQTT message delivery mechanism is also based on the "Publish" - "Subscribe" model.
MQTT specific steps are:
Step 1: Use the first to obtain an MQTT server, and then create a new MQTT communication product.
Step 2: Then go to connect to this server, two important parameters to connect to the server is the host number (domain name or IP address) and port number.
Step 3: If you are using a third-party cloud server platform, it may require you to log in to this device using the product ID and authentication information, both of which can be found in the backend of Device Cloud.
Once these three steps are done, you can subscribe or post messages to the corresponding topics.
I'll put together a document to show you how to "whore out" a China Mobile device cloud open access platform.
These three steps apply to both application software development and microcontroller development. In microcontroller development, if you use AT commands and external WIFI module communication, then the general module can come with AT + MQTT commands, which is the best way to greatly reduce the pressure on the microcontroller. Or you can directly access the TCP/IP transport layer data, and then parse the MQTT, which requires the user to have a deep understanding of the MQTT protocol to also parse their own Json data, so in general when doing embedded devices, it is generally recommended that we directly use the ready-made module with the MQTT protocol, directly parsing the AT command is more convenient.
Case Study:
Remote control of lights and getting the current room temperature.
Regarding this case, it is actually one of the simplest applications of MQTT. First of all, the embedded control board in the room is mainly connected to the server through WIFI, which can control the light switch and also collect the temperature. The end device far away is a cell phone.
To keep the communication working, first they need to be connected to the same MQTT server.
The temperature information on the device side is collected by the device, so it needs to publish the collected data to the "Temperature" topic, while the cell phone gets the temperature information, so it needs to subscribe to the "Temperature" topic. Once the device sends the temperature information to the "temperature topic", the topic will be received by the cell phone.
The light control on the device side is executed by the device, so it needs to subscribe to the "light switch" topic, while the cell phone controls the light switch, so it needs to publish control information to this "light switch" topic. Once the cell phone sends a light on message to the "light on" topic, the topic will be received by the terminal, and then the light on command will be executed.




