In the article "IoT core protocol-message push technology evolution" has been introduced to the reader of a variety of message push technology, including HTTP one-way communication, Ajax polling, Websocket, Mqtt, COAP, etc., wherein the MQTT protocol is developed and pushed by IBM, it has an open, simple, Lightweight and easy-to-implement, it is also widely used in remote sensing, smart home, energy monitoring, and medical applications, even in resource-constrained environments, such as in resource-scarce embedded systems or in very expensive network bandwidth. is an important part of the Internet of things and may become the de facto standard of the internet of things in the future.
This article will help readers to understand the MQTT protocol and the Open source implementation of MQTT in a Linux environment Mosquitto experience Verification, in the subsequent article will expand the analysis of its source framework for the construction of the IoT platform to pave the groundwork.
I. MQTT protocol analysis
1.1. Introduction to the MQTT protocol
The full name of the MQTT (messagequeueing Telemetry Transport Protocol) is the abbreviation for the Message Queuing Remote sensing transport protocol, a message transfer protocol based on the lightweight agent's publish/subscribe model introduced by IBM. Runs on top of the TCP protocol stack, providing an orderly, reliable, two-way connection to the network connection guarantee. Because it is open, simple and easy to implement, it can be used in resource-constrained environments and is a good choice for both the and Internet of things applications.
1.2. Publishers/Subscribers
MQTT employs the agent's publish/subscribe model for decoupling Publishers and Subscribers (decouple), so there are three roles in the MQTT protocol: Proxy Server, publisher client, and Subscriber client, where publishers and subscribers do not interfere with each other. That is, publishers and subscribers do not know each other's existence, they only know the proxy server, the proxy server is responsible for the message from the Publisher to store processing and send these messages to the correct subscribers. This decoupling is reflected in the following 3 areas:
• Spatial decoupling: Publishers and Subscribers do not need to know the other person's presence, such as the other's IP address or port;
• Time decoupling: Publishers and Subscribers do not have to establish a connection;
• Synchronous decoupling: Publishers and Subscribers do not need to synchronize when they publish or receive messages;
1.3. MQTT Message types
The three roles of MQTT interact with 14 different message types:
The 1.CONNECT control message is used for client requests to establish a connection with the server, the application layer connection instead of the TCP/IP connection, and the transmission of the Connect control message after the TCP/IP connection is established;
The 2.CONNACK control message is used for the server to send back the connection acknowledgement to the requesting connection;
3.PUBLISH control messages are used to publish application information for the specified subject name;
4.puback/pubrec/pubrel/pubcomp control messages are used to respond to application information of different service quality;
The 5.SUBSCRIBE control message is used by subscribers to send a topic filter list to the server that represents the topic the client wants to subscribe to;
The 6.SUBACK control message is used to subscribe the response of the control message;
The 7.UNSUBSCRIBE control message is used to send a topic filter list to the server that indicates the topic the client wants to unsubscribe from;
The 8.UNSUBACK control message is used to unsubscribe the response of the control message;
9.PINGREQ/PINGRESP Control message as the heartbeat packet between client and server;
The 10.DISCONNECT control message is used by the client to tell the server that it will disconnect before disconnecting;
1.4. Theme (topic)
Using themes is a way for the server to manage application information for each client, a theme name can consist of multiple topic levels (topic level), each layer is separated by a '/' slash, and when the client-specified theme filter contains wildcards, A topic-level separator is useful when a client wants to subscribe to multiple topics with similar structure at once.
The theme wildcard is available only in the theme filter, and the subject name when you publish the app message does not allow wildcards, and there are two themes for the wildcard character:
Single-layer wildcard: ' + ', which matches all topics in a topic hierarchy;
Multilayer wildcard: ' # ', which matches themes at multiple theme levels;
For example, if the topic Filter for a client subscription is "grade/subject/math/#", it receives a message that is published using the following topic name:
"Grade/subject/math"
"Grade/subject/math/student1"
"Grade/subject/math/student1/lastterm"
1.5, service quality level (Qoslevels)
MQTT delivers the quality of messaging for three types of QoS:
A. At most one time (Atmost once delivery): Qos=0, the Protocol applies information to this level does not require a response acknowledgement, and there is no re-sending mechanism, such information may occur message loss or duplication, depending on TCP/IP provides the best effort to interact with the packet service.
B. At least once (Atleast once delivery): Qos=1, ensure that the information arrives, but the message duplication may occur, if the sender does not receive the Puback control message within the specified time, the application information will be re-sent, and the control message in the DUP flag position 1.
C. Only once (exactlyonce delivery): qos=2, the highest level of quality of service, message loss and duplication are unacceptable.
1.6. Servers and clients that support the MQTT protocol
The current popular open source proxy server (MQTT broker) implementations are: Mosquitto, HIVEMQ, Apache ActiveMQ, RabbitMQ, Mosca, etc., MQTT clients also have implementations under different operating systems and programming languages, popular client libraries ( MQTT clientlibraries) are: Eclipse Paho (Support c,c++, Java, Javascript, Python, Go, C #), M2mqtt (C #), Fusesource mqttclient (Java), Mqtt.js (JavaScript), Libmosquitto (c + +), and so on, the MQTT client applications developed through the above libraries are: Mosquitto_pub/mosquitto_sub (can run on Linux, Windows , MacOSX OS), Hivemqwebsocket Client (Web browser), Mymqtt (Android), Mqttlens (Google Chrome), and more.
Second, Mosquitto deployment verification
2.1. Install Mosquitto under Ubuntu
1. Source code package Download: http://mosquitto.org/files/source/
Version: mosquitto-1.4.tar.gz
Decompression: TAR-ZXVF mosquitto-1.4.tar.gz
Access directory: CD mosquitto-1.4
2. Compiling the installation
Open the configuration file and remove the unwanted features:
VI config.mk
such as: With_tls,with_tls_psk, With_srv, With_websockets, With_socks, With_uuid, etc.
Save exit: Wq
Installing Mosquitto
Make
Make install
2.2. Start Mosquitto Server
Mosquitto is started by default with Mosquitto users, so create Mosquitto users first:
AddUser Mosquitto
Start the proxy server: mosquitto-c/etc/mosquitto/mosquitto.conf.example
You may encounter the following error during startup of the server: the Libmosquitto.so.1 file cannot be found, and the workaround is to add the link library path:
Vi/etc/ld.so.conf.d/liblocal.conf
/usr/local/lib64
/usr/local/lib
Refresh after exiting the file: Ldconfig
Re-execution: Mosquitto-c/etc/mosquitto/mosquitto.conf.example
2.3. Start Subscriber client Mosquitto_sub, subscribe to test topic
Mosquitto_sub client application is located in the/mosquitto-1.4/client directory, executes./MOSQUITTO_SUB-T test,-T represents the theme Filter property, followed by the specified theme filter.
2.4. Launch the Publisher client Mosquitto_pub, and publish the topic information more than once
./mosquitto_pub-t Test-l
Where-T represents the topic name of the publication, the theme name is not allowed to include wildcards,-l refers to the standard input to enter the application information to be published, more properties can be obtained through:./mosquiito_pub--help, also available on the official website to understand:
Http://mosquitto.org/man/mosquitto_pub-1.html.
Publisher publishes app Information:
2.5. The publisher sends the Disconnect control message to the server by typing "ctrl+d" and then exits normally
If you terminate the client program through "CTRL + C" (Abnormal network disconnection), the server will prompt: Scoket error
This article initially described the MQTT protocol and one of the implementation as an example of the deployment and experience, the detailed details of the protocol, such as control message format, readers can go to the official website to download documents, you can also focus on the embedded Penguin Circle to obtain shared resources.
Next, Mosquitto's mosquito_pub publisher's client-side source framework is thoroughly analyzed to understand the implementation and programming of MQTT.
the original team of embedded Penguin Circle consists of senior engineers such as Ali, Meizu, nvidia, godson, Ju Li, and so on. Hundreds of original, two articles a week, sharing embedded, Linux, Internet of Things, GPU, Android, autonomous driving and other technologies. Welcome to sweep code attention to the public number: embedded Penguin ring, real-time push the original article!
IoT MQTT Protocol analysis and open source Mosquitto deployment validation