MQTT – The Basics
MQTT (MQ Telemetry Transport) is a lightweight, Machine to Machine (M2M) messaging protocol, primarily used in low power sensors and mobile devices. Because of being lightweight and secure, the protocol is well known within the IoT community. In this article we will be an introduction to MQTT. Later we will be implementing the protocol using NodeMCU.
MQTT is based on the publish/subscribe architecture, where the sender (publisher) sends the messages to a central communication point known as the broker. The publisher classifies the messages into various classes (topics), without the knowledge of the receivers (subscribers). The subscribers interested to a specific topic subscribe to that topic, and the broker pushes all the messages with the matching topics to the interested subscribers.
As mentioned earlier, messages are published on topics. A topic is a string treated as a hierarchical structure, with slash ‘/’ used as a separator. This allows for a clean classification of messages. For instance, multiple clients may all publish their weather information on the following topic, with their name and weather parameter value being replaced as appropriate
Special characters, known as the wildcard characters, can be employed to filter out and subscribe to multiple topics at once. MQTT provides two such wildcard. One is the multi-level wildcard ‘#’, which filters the topics against all the remaining levels of the hierarchy. This wildcard must be used at the end of the topic. For example, to get all the temperature values, following topic can be used
and to get all the weather parameters for a client
Another such wildcard, the single-level wildcard ‘+’, is used for a single level of hierarchy, and can be used at any level. For example, to get the temperature values for all the clients, topic can be structured like this
Quality of Service (QoS) is an agreement between a sender and a receiver regarding delivery of a message. MQTT defines three levels of QoS
- QoS 0 – At most once: This is the fastest, and consequently the most unreliable one. The message is delivered only once or not at all. Once the message is sent it is deleted from the outbound queue, with no acknowledgment from the subscriber.
- QoS 1 – At least once: Ensures that the message arrives at the receiver at least once. Upon receiving acknowledgement (PUBACK Packet), the sender notifies the client app and deletes the message from the outbound queue. Else the message is dispatched again with a duplicate flag (DUP) is set.
- QoS 2 – Exactly once: Guarantees that the message is delivered only once, with an increased overhead. It is used when neither lost or duplication of messages is acceptable. Sender will keep resending the message as long as no acknowledgement (PUBREC) is received. On receiving acknowledgement it then sends a message release message (PUBREL). Upon no acknowledgement on PUBREL, it will resend the PUBREC message. On receiving acknowledgement (PUBCOMB), the sender can now delete the message from the outbound queue, otherwise it will resend the PUBREL message.
That’s enough to get you started with MQTT. In the next lesson we will finally be implementing MQTT in a simple communication scenario.