What is MQTT?
MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks.The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery.
These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.
Other notable features of MQTT are:
- It’s open source, royalty free and therefore easy to adopt and adapt
- It follows a publish/subscribe model for one-to-many distribution
- Small message headers
- Multiple Quality of Service levels
- Simple command messages
- Data type agnostic
- Retained messages
- Clean sessions and durable connections
- Last Will and Testament (LWT)
As described above MQTT is a message based protocol that uses publisher-subscriber pattern. The key component in MQTT is the MQTT broker. The main task of MQTT broker is dispatching messages to the clients (“subscribers”). In other words, it receives messages from publisher and dispatches these messages to the subscribers. While it dispatches messages, the MQTT broker uses the topic to filter the clients that will receive the message. The topic is a string and it is possible to combine the topics creating topic levels.
A topic is like a virtual channel that connects a publisher to its subscribers. This topic is managed by the MQTT broker. Through this virtual channel, the publisher is decoupled from the subscribers and the clients (publishers or subscribers) does not have to know each other. This makes this protocol highly scalable without a direct dependency from the message producer (“publisher”) and the message consumer (“subscriber”).
The schema below describes the MQTT architecture:
Image Credit: DZone
How does MQTT work?
Sure MQTT is simple and awesome, but it took clever engineering to make it that way. Here’re some of the nitty-gritties about the critical components of MQTT.
This is… well… the message to be sent. Typically called the “Message Payload,” it is, by default, formatted as plain-text, giving users the flexibility to structure the message payload to any required format.
Every MQTT communication relies on the concept of “The Topic.” A single unique topic defines a unique pipeline, or connection between publishers and subscribers. Essentially, if the topic of the message published matches the topic subscribed, the subscriber gets the message. As simple as that!
The Quality of Service functionality exists to provide the flexibility to have or not have guaranteed delivery of messages. MQTT QoS can be laid out within only six bits – 0, 1, and 2.
QoS-0 is the most unreliable. Any message published with QoS 0 will be sent by the publisher, and then forgotten, not checking whether the message has been successfully delivered. It is often called “fire and forget.”
QoS-1 holds the concept of delivering the message “at least” once. Any message is guaranteed to be delivered at least once, but may also be sent more than once.
QoS-2 is the most reliable – “exactly once.” The broker guarantees that the message reaches its destination only once. QoS reliability is directly proportional to bandwidth consumption: higher reliability, higher bandwidth consumption.
In this tutorial I am going to use a popular web-based dashboard called Cayenne which allows you to easily manage your IoT devices. To get started, you can register for a free Cayenne account here.
For sending data to Cayenne dashboard we will be using a free Chrome extension called MQTT Lens developed by Sandro. Follow the steps below to send your first MQTT data to Cayenne dashboard.
- Download MQTT Lens (an extension of Chrome)
- Create a free account at Cayenne. Once you have signed up you will be presented with a “First Visit” dashboard. Click on “Bring Your Own Thing” option and note down the details. We will be needing these later for MQTT Lens.
- Open MQTT Lens chrome app and copy paste the details from Cayenne dashboard. If done correctly you should see a new device in Cayenne dashboard.
- Setup a sensor in Cayenne dashboard and assign channel “1”.
- Next we need to send a message from the MQTT Lens using the following API:
v1/MQTT_USERNAME/things/CLIENT_ID/data/1Replace MQTT_USERNAME & CLIENT_ID with the values on your dashboard. Here “1” refers to the channel where we will be sending the sensor data. Likewise we can have “n” number of unique channels to send “n” types of sensor data.
- Send a message from the publisher and watch the message magically appear on Cayenne dashboard. Checkout the video below for more details.
For sending actual sensor data from a physical sensor using Raspberry Pi refer to my previous article: https://www.raspinews.com/dht11-raspberry-pi-cayenne/