In this article, we’re going to introduce you to the MQTT protocol. MQTT stands for Message Queuing Telemetry Transport.
It is a lightweight publish and subscribe system where you can publish and receive messages as a client.
MQTT is a simple messaging protocol, designed for constrained devices with low-bandwidth. So, it’s the perfect solution for Internet of Things applications. MQTT allows you to send commands to control outputs, read and publish data from sensor nodes and much more.
Therefore, it makes it really easy to establish a communication between multiple devices.
Watch the video tutorial below
High Level Overview
Here’s a quick high level overview of what MQTT allows you to do.
You can send a command with a client (like Node-RED) to control an output:
Or you can read data from a sensor and publish it to a client (like Node-RED):
MQTT Basic Concepts
In MQTT there are a few basic concepts that you need to understand:
MQTT – Publish/Subscribe
The first concept is the publish and subscribe system. In a publish and subscribe system, a device can publish a message on a topic, or it can be subscribed to a particular topic to receive messages
- For example Device 1 publishes on a topic.
- Device 2 is subscribed to the same topic as device 1 is publishing in.
- So, device 2 receives the message.
MQTT – Messages
Messages are the information that you want to exchange between your devices. Whether it’s a command or data.
MQTT – Topics
Another important concept are the topics. Topics are the way you register interest for incoming messages or how you specify where you want to publish the message.
Topics are represented with strings separated by a forward slash. Each forward slash indicates a topic level. Here’s an example on how you would create a topic for a lamp in your home office:
Note: topics are case-sensitive, which makes these two topics different:
If you would like to turn on a lamp in your home office using MQTT you can imagine the following scenario:
- You have a device that publishes “on” and “off” messages on the home/office/lamp topic.
- You have a device that controls a lamp (it can be an ESP32, ESP8266, or any other board). The ESP32 that controls your lamp, is subscribed to that topic: home/office/lamp.
- So, when a new message is published on that topic, the ESP32 receives the “on” or “off” message and turns the lamp on or off.
This first device, can be an ESP32, an ESP8266, or an Home Automation controller platform like Node-RED, Home Assistant, Domoticz, or OpenHAB, for example.
MQTT – Broker
At last, you also need to be aware of the term broker.
The broker is primarily responsible for receiving all messages, filtering the messages, decide who is interested in them and then publishing the message to all subscribed clients.
There are several brokers you can use. In our home automation projects we use the Mosquitto broker which can be installed in the Raspberry Pi. Alternatively, you can use a cloud MQTT broker.
To install Mosquitto broker on the Raspberry Pi follow our tutorial:
How to Use MQTT in Home Automation and IoT Projects
As we’ve seen previously, MQTT is great for home automation and internet of things projects. If you want to start making your own projects using MQTT here’s an example of what you can do.
Here’s the steps you should follow:
1) Set up your Raspberry Pi. Follow our Getting Started Guide with Raspberry Pi.
2) Enable and Connect your Raspberry Pi with SSH.
3) You need Node-RED installed on your Pi and Node-RED Dashboard.
4) Install the Mosquitto broker on the Raspberry Pi.
5) Add the ESP8266 or the ESP32 to this system. You can follow the next MQTT tutorials:
- ESP32 and Node-RED with MQTT – Publish and Subscribe
- ESP8266 and Node-RED with MQTT – Publish and Subscribe
If you want to learn more about these subjects, we have a dedicated course on how to create your own Home Automation System using Raspberry Pi, ESP8266, Arduino, and Node-RED. Just click the following link.
MQTT is a communication protocol based on a publish and subscribe system. It is simple to use and it is great for Internet of Things and Home Automation projects. We hope you’ve found this tutorial useful and you now understand what is MQTT and how it works.
10 thoughts on “What is MQTT and How It Works”