A Lightweight Message Transport Protocol – MQTT

Recently, I used MQTT in my project and found it to be an excellent protocol, so I would like to share some related knowledge in hopes of helping everyone.

1

What is MQTT?

MQTT is a lightweight message transport protocol used for communication between IoT devices and applications. Originally developed by IBM, it has now become an open OASIS standard.

The protocol is based on the Publish/Subscribe Pattern and supports multiple Quality of Service (QoS) levels, enabling reliable message transmission and post-delivery storage.

Its main features are as follows:

1. Lightweight and Low Bandwidth: By using binary encoding and variable-length encoding, the size of the message header is kept as small as possible, allowing efficient data transmission in low-bandwidth environments.

2. Flexible and Scalable: It supports various message publishing/subscription modes, adapting flexibly to different application scenarios. Additionally, the MQTT protocol supports multiple QoS levels, allowing the reliability and efficiency of message transmission to be adjusted according to application needs.

3. Reliability and Security: It supports message persistence and storage, ensuring reliable message transmission and post-delivery storage. Furthermore, it supports security mechanisms such as encryption and authentication, ensuring secure message transmission and processing. 4. Easy to Use and Implement: The protocol specification is straightforward and easy to understand and implement. It also provides various client libraries and development tools for quick usage and integration of the protocol.

2

Open Source Libraries for MQTT

1. Paho MQTT C is an open-source MQTT protocol stack library, and Paho MQTT is a client based on the MQTT protocol implemented by Eclipse.

2. uMQTT, the uMQTT package is independently developed by RT-Thread and is a client implementation based on the MQTT 3.1.1 protocol.

3

Design of the MQTT Protocol

The core design pattern of MQTT is the Publish/Subscribe Pattern, which is a messaging pattern used to decouple the relationship between message senders and receivers.

A Lightweight Message Transport Protocol - MQTT

In MQTT, the implementation of the Publish/Subscribe pattern includes the following core concepts: 1. Topic: A topic is the identifier for messages in MQTT, used to specify the content and recipients of messages. Topics consist of one or more topic levels, separated by slashes (/).

For example, topic/a/b/c is a topic composed of three topic levels. 2. Client: In MQTT, a client refers to a device or application connected to the MQTT broker, which can be a publisher or subscriber. 3. Broker: The broker in MQTT is middleware responsible for receiving, routing, and forwarding messages. The broker maintains one or more topics, and clients can publish messages to or subscribe to topics from the broker. 4. Publisher: The publisher in MQTT is the client that publishes messages. The publisher sends messages to the broker, which routes them to subscribers who are subscribed to the corresponding topic. 5. Subscriber: The subscriber in MQTT is the client that subscribes to topics. Subscribers subscribe to specific topics from the broker, which keeps track of the subscription list. When new messages are published to the topics a subscriber is subscribed to, the broker sends these messages to the subscriber. In the Publish/Subscribe model of MQTT, publishers and subscribers are decoupled, meaning they do not need to know about each other’s existence or identity, only the relevant topics.

This design pattern gives MQTT a high degree of flexibility and scalability, adapting to different application scenarios and needs. At the same time, MQTT supports multiple QoS levels, allowing different levels of reliability and efficiency in message transmission based on application requirements.

4

Several Application Modes

MQTT supports various message publishing/subscription modes, and common application modes include: 1. Point-to-Point Mode: In point-to-point mode, MQTT clients connect directly to the MQTT broker and transmit messages using the MQTT protocol.

This mode is suitable for communication between directly connected IoT devices and applications, such as communication between sensors and controllers. 2. Publish/Subscribe Mode: In publish/subscribe mode, MQTT clients receive messages of interest by subscribing to topics and send messages by publishing to topics.

This mode is suitable for application scenarios where decoupling the relationship between message senders and receivers is necessary, such as real-time data transmission, remote control, and status monitoring in IoT. 3. Request/Response Mode: In request/response mode, MQTT clients send request messages to the MQTT broker via request topics, and the broker routes the request messages to the appropriate handler, returning response messages.

This mode is suitable for application scenarios requiring request and response interactions, such as remote management and control in IoT.

4. Point-to-Multipoint Mode: In point-to-multipoint mode, a single MQTT client can connect to multiple MQTT brokers simultaneously, transmitting messages using the MQTT protocol.

This mode is suitable for application scenarios requiring connections to multiple MQTT brokers, such as distributed processing and data storage in IoT.

5

Simple Application

Here, we will illustrate the Publish/Subscribe mode of MQTT using an IoT temperature monitoring system as an example:

Assuming the IoT temperature monitoring system includes multiple temperature sensors and a temperature data center, each temperature sensor can collect temperature data in real-time and publish it to a specified topic, while the temperature data center subscribes to multiple topics to receive and process temperature data in real-time.

This process can be achieved through the following steps: 1. The temperature sensors connect to the MQTT broker and publish temperature data to a specified topic, such as topic/temperature. 2. The temperature data center connects to the MQTT broker and subscribes to the temperature data topic, such as topic/temperature. 3. When the temperature sensor publishes new temperature data to the topic topic/temperature, the MQTT broker routes this message to the subscribed temperature data center. 4. Upon receiving the new temperature data, the temperature data center can process and store the data, such as calculating the average temperature or generating reports. Through this method, the Publish/Subscribe mode of MQTT achieves decoupling between the temperature sensors and the temperature data center, allowing for real-time data transmission and processing without needing to know each other’s existence or identity, only the relevant topics.

A Lightweight Message Transport Protocol - MQTT

END

Source: The Last Bug
Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
How High is the Cost of Embedded Development?
[Very C Structure] Simple Yet Powerful Tables
State Machine – The Universal Language of Microcontrollers (With Code)

→ Follow to Stay Updated ←

Leave a Comment