What is MQTT and How It Works

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.

If you like home automation and you want to build a complete home automation system, I recommend downloading my home automation course.

Learn Raspberry Pi, ESP8266, Arduino and Node-RED. This is a a step-by-step course to get you building a real world home automation system using open-source tools DOWNLOAD »

Learn Raspberry Pi, ESP8266, Arduino and Node-RED. This is a a step-by-step course to get you building a real world home automation system using open-source tools DOWNLOAD »

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:

  • Publish/Subscribe
  • Messages
  • Topics
  • Broker

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:

Download our Free eBooks and Resources

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:

  1. You have a device that publishes “on” and “off” messages on the home/office/lamp topic.
  2. 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.
  3. 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-device

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.

mqtt-broker

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:

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.

>> Enroll in Build a Home Automation System for $100 <<

Wrapping up

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.

Thanks for reading. If you like this article, please support our work by subscribing my blog.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

23 thoughts on “What is MQTT and How It Works”

  1. This is good for many because of the visual aspect. IMHO it’s not quite technical enough, as there are a couple key points such as NOT using a leading “/” in the topic that are omitted, which many folks assume is required due to typical file structure convention, but not for MQTT, so a little embellishment in those lines would make it even better.

    • Hi Rick, I totally agree that I could explore in depth those conventions and other aspects. I’ve tried to keep it as simple as possible, because MQTT is still a bit confusing to a lot of people and there are other resources that go in more depth. Thanks for your feedback!
      Rui

  2. Hello RUI,
    could you explain, in a future lesson, how a REST.API works? Also how to use the REST.API with a site like IFTTT.COM? This based on this chapter MQTT?
    I Love all the tutors

  3. Hi. I have an mqtt server at home and one at work but I find it difficult to keep them in sync. Is there a service out “there” that provides free mqtt broker service? Thanks.

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.