Professional Documents
Culture Documents
What Is MQTT and How It Works - Random Nerd Tutorials
What Is MQTT and How It Works - Random Nerd Tutorials
Menu
This article is an introduction to the MQTT protocol. MQTT stands for Message
Queuing Telemetry Transport, a simple messaging protocol suitable for
communication between IoT devices.
What is MQTT?
MQTT stands for Message Queuing Telemetry Transport. MQTT is a simple
messaging protocol, designed for constrained devices with low bandwidth. So, it’s the
perfect solution to exchange data between multiple IoT devices.
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 1/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Its main applications include sending messages to control outputs, read and publish
data from sensor nodes and much more.
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
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 2/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
MQTT – Messages
Messages are the information that you want to exchange between your devices. It
can be a message like a command or data like sensor readings, for example.
MQTT – Topics
Another important concept is 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 of 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:
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 3/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 4/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
MQTT – Broker
Finally, another important concept is the broker.
The MQTT broker is responsible for receiving all messages, filtering the
messages, deciding who is interested in them, and then publishing the message to
all subscribed clients.
There are several brokers you can use. In home automation projects, we use
the Mosquitto Broker installed on a Raspberry Pi. You can also install the Mosquitto
broker on your PC (which is not as convenient as using a Raspberry Pi board,
because you have to keep your computer running all the time to keep the MQTT
connection between your devices).
Having the Mosquitto broker installed on a Raspberry Pi on your local network allows
you to exchange data between your IoT devices that are also connected to that same
network.
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 5/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
You can also run Mosquitto MQTT broker in the cloud. Running the MQTT Mosquitto
Broker in the cloud allows you to connect several IoT devices from anywhere using
different networks as long as they have an Internet connection. Check the tutorial
below:
Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital
Ocean)
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 6/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Node-RED also allows you to build an User Interface with buttons to control
outputs and charts to display sensor readings.
The Arduino, the ESP32 and ESP8266 can act as MQTT clients that publish
and subscribe to topics.
These boards are connected to actuators like LEDs or relays, and sensors like
temperature, humidity, smoke sensors, etc..
These boars can publish data about the sensor’s state on a specific topic, that
Node-RED is also subscribed to. This way, Node-RED receives the sensor
readings and can display them on the user interface.
On the other side, Node-RED can publish data on a specific topic to control
outputs when you use the buttons on the interface. The other boards are also
subscribed to that topic and control the outputs accordingly.
The following image shows an example of a Node-RED UI that allows you to control
one output and displays temperature and humidity readings:
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 7/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Here’s a quick summary of the steps you should follow to build something as
described previously:
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:
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 8/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd 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.
Wrapping Up
MQTT is a communication protocol based on a publish and subscribe system.
Devices can subscribe to a topic or publish data on a topic. Devices receive
messages that are published on topics they are subscribed to.
MQTT is simple to use and it is great for Internet of Things and Home Automation
projects. You can check all our MQTT-related tutorials here.
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 to
my blog.
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 9/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 10/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
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 »
Recommended Resources
Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266,
Arduino, and Node-RED.
Home Automation using ESP8266 eBook and video course » Build IoT and
home automation projects.
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 11/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even
with no prior experience!
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 12/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
SUBSCRIBE
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 13/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Ciro
January 12, 2017 at 9:27 am
Reply
Rui Santos
January 15, 2017 at 11:55 pm
You’re welcome!
Thanks for reading,
Rui
Reply
John
January 12, 2017 at 2:57 pm
Great simplified instructions, as usual. Can you add info on use of passwords
in next blog.
Reply
Rui Santos
January 15, 2017 at 11:59 pm
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 14/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Hi John!
Thanks for the suggestion,
Rui
Reply
rick
January 12, 2017 at 4:07 pm
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.
Reply
Rui Santos
January 16, 2017 at 12:43 am
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
Reply
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 15/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
moccafloat
January 13, 2017 at 2:54 pm
Is MQTT has maximum string for a topic? Because I’ve some problem with
publishing topic.
Reply
Rui Santos
January 15, 2017 at 11:47 pm
How many characters do you have? But I don’t think that’s your issue…
Reply
Enrique Gongora
January 16, 2017 at 5:12 am
Reply
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 16/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Rui Santos
January 24, 2017 at 7:16 pm
No, the Arduino can’t run an MQTT broker, but with a Ethernet/WiFi shield
the Arduino can be an MQTT client
Reply
David Adams
June 27, 2020 at 10:11 am
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/).
Reply
Sara Santos
June 27, 2020 at 2:46 pm
Great!
Thanks for sharing
Reply
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 17/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
cfb
January 16, 2017 at 3:07 pm
Reply
Rui Santos
January 24, 2017 at 7:15 pm
Reply
Sidaray
January 20, 2017 at 10:11 am
Great work!
Reply
Rui Santos
January 24, 2017 at 7:09 pm
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 18/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Rui
Reply
Olof Scholten
January 27, 2017 at 7:15 am
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
Reply
Huong
February 19, 2017 at 6:01 pm
Reply
Rui Santos
February 19, 2017 at 10:32 pm
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 19/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Reply
giulio
March 8, 2017 at 5:35 am
do the node red works only with raspberry? what about Arduino?
Reply
Rui Santos
March 20, 2017 at 7:53 pm
Reply
Messaoud chaima
June 27, 2018 at 8:42 am
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 20/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Reply
Andre
October 18, 2018 at 11:37 am
Reply
Sara Santos
October 19, 2018 at 4:12 pm
Hi Andre.
You can find the pubsubclient in the following link:
github.com/knolleary/pubsubclient
If you want to use MQTT with the ESP32, you can take a look at the
following tutorial:
https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/
I hope this helps.
Regards,
Sara
Reply
Bruce Calder
December 14, 2018 at 6:05 pm
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 21/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
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.
Reply
Martin
April 30, 2019 at 5:18 pm
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
Reply
Rui Santos
May 1, 2019 at 10:50 am
Reply
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 22/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Hradesh Kumar
July 27, 2019 at 9:00 pm
Reply
june veeranondh
November 27, 2020 at 3:29 am
Reply
Paolo
December 16, 2021 at 9:58 pm
Hi Rui, great explanation of this protocol. I have a question, what are the
advantages for preferring mqtt instead of a classical http+mysql database
(that are quite lightweight for sending data from few sensors). Actually using
mqtt I can’t store the data, but I may at most download after the visualization.
So is it more suitable for multiple iot devices? What’s your opinion?
Thanks
Paolo
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 23/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Reply
Bill
December 17, 2021 at 1:50 pm
I think I’m beginning to get this understood. Please correct me if I’m wrong.
Node Red isn’t required unless you want to add an interface that you can
interact with or that displays data.
Have I got this right? I was wondering about the difference between Node
Red and MQTT.
Thank you
Bill
Reply
Sara Santos
December 17, 2021 at 2:54 pm
Hi Bill.
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 24/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
network. You can also install MQTT on your computer. Alternatively, you can
also install it on the cloud.
Regards,
Sara
Reply
Bill
December 17, 2021 at 8:20 pm
One more question: Can the broker and clients all work if none are
connected to the internet? I noticed you mentioned “local network” and I’m
wondering if this separate system of broker and clients can all work
together and without any internet connection.
Reply
Sara Santos
December 18, 2021 at 4:21 pm
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 25/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Hi.
Regarding your previous comments, all instructions were tested before
publishing and they were working (two days ago). So, I think everything
should be fine now.
All devices need to be connected to the internet.
Regards,
Sara
Reply
Abhi Rajput
September 27, 2022 at 5:40 am
regards,
Abhi
Reply
ISHAN
December 9, 2022 at 2:48 pm
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 26/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Reply
Sara Santos
December 9, 2022 at 5:45 pm
Great!
Reply
Charitha
February 16, 2023 at 9:58 am
Thank you for this informative article on MQTT! Your explanations are very
clear.
Reply
Paul Keenan
February 22, 2023 at 10:19 am
From the 16th February all my MQTT devices failed to connect . Any idea of
reason ?
Reply
Sara Santos
February 23, 2023 at 10:22 am
Hi.
Check your network connection and if your MQTT broker is running. You
might need to restart your Raspberry Pi.
Regards,
Sara
Reply
Paul Keenan
February 23, 2023 at 5:47 pm
Reply
Sara Santos
February 24, 2023 at 1:51 pm
Yes. You’ll have to do that to point the ESP32 to the right MQTT server.
For future projects, you may want to add OTA updates so that you can
update the sketches on your boards wirelessly.
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 28/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Regards,
Sara
Reply
Paul Keenan
February 23, 2023 at 5:57 pm
Discovered that the address of the MQTT server had changed. I will have
to change the programs for a lot of esp 32’s.
Is this a common thing ?
Reply
Givi
March 23, 2023 at 1:50 am
Hi Sara,
Issue resolved. There is a confusion in where to put/locate the edited version
of “mosquitto.conf”. The file that is in “/etc/mosquitto/” folder, after install has
the following entry and when I put my final version of “mosquitto.conf”
in “/etc/mosquitto/conf.d/” it conflict with the file that comes with the install in
the “/etc/mosquitto” directory. I moved the file to that folder and it worked OK
and came up active.::
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 29/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
/usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
include_dir /etc/mosquitto/conf.d
=========================
Thank you.
Givi
Reply
Sara Santos
March 25, 2023 at 11:12 am
Great!
Thanks for the follow-up.
Regards,
Sara
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 30/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
Reply
Leave a Comment
Name *
Email *
Website
Post Comment
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 31/32
9/9/23, 11:09 AM What is MQTT and How It Works | Random Nerd Tutorials
About Support Terms and Conditions Privacy Policy Refunds Complaints’ Book
https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/ 32/32