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.

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:

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 – 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:

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.

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

29 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!

      • Great article, Rui!

        A Raspberry Pi Zero at under $20 runs an MQTT broker just fine, and it’s a great way to get started with these computers. Or, you can run MQTT on Windows if Linux isn’t your cup of tea (steves-internet-guide.com/install-mosquitto-broker/).

  2. 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.

  3. Hi,
    nice project, but my dht22 doesn’t work ! So I switched to Webserver with same Pin D1 connected to DHT22 and this sample works fine but not reliable ! Pullup on D1pin 4,7kOhm, could I do anything to get a more reliable solution for this topic .
    Mosquitto and node red works fine !!
    Thanks Martin


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.