MQTT Protocol – How it Works

MQTT is one of the most commonly used protocols in IoT projects. It stands for Message Queuing Telemetry Transport.

In addition, it is designed as a lightweight messaging protocol that uses publish/subscribe operations to exchange data between clients and the server. Furthermore, its small size, low power usage, minimized data packets and ease of implementation make the protocol ideal of the “machine-to-machine” or “Internet of Things” world.


Firstly, I will go through the reason why using MQTT, how it practically works with a real-life IoT example. Then, I will go through the broker; the backbone of the MQTT and what best broker to use.

Finally, I will get into a hands-on tutorial that demonstrates how you can use a smartphone to control devices or get their data, using the MQTT protocol.


First of all, if this is your first time in the IoT world, I would recommend you go through this IoT blog first.



MQTT has unique features you can hardly find in other protocols, like:

  • It’s a lightweight protocol. So, it’s easy to implement in software and fast in data transmission.
  • It’s based on a messaging technique. Of course, you know how fast your messenger/WhatsApp message delivery is. Likewise, the MQTT protocol.
  • Minimized data packets. Hence, low network usage.
  • Low power usage. As a result, it saves the connected device’s battery.
  • It’s real time! That’s is specifically what makes it perfect for IoT applications.

How MQTT works

Like any other internet protocol, MQTT is based on clients and a server. Likewise, the server is the guy who is responsible for handling the client’s requests of receiving or sending data between each other.

MQTT server is called a broker and the clients are simply the connected devices.

  • When a device (a client) wants to send data to the broker, we call this operation a “publish”.
  • When a device (a client) wants to receive data from the broker, we call this operation a “subscribe”.


In addition, These clients are publishing and subscribing to topics. So, the broker here is the one that handles the publishing/subscribing actions to the target topics.


Let’s say there is a device that has a temperature sensor. Certainly, it wants to send his readings to the broker. On the other side, a phone/desktop application wants to receive this temperature value. Therefore, 2 things will happen:

  • The device defines the topic it wants to publish on, ex: “temp”. Then, it publishes the message “temperature value”.
  • The phone/desktop application subscribes to the topic “temp”. Then, it receives the message that the device has published, which is the temperature value.

Again, the broker role here is to take the message “temperature value” and deliver it to phone/desktop application.

MQTT Components:

That takes us to the MQTT components, which are 5 as follows:

  • Broker, which is the server that handles the data transmission between the clients.
  • A topic, which is the place a device want to put or retrieve a message to/from.
  • The message, which is the data that a device receives “when subscribing” from a topic or send “when publishing” to a topic.
  • Publish, is the process a device does to send its message to the broker.
  • Subscribe, where a device does to retrieve a message from the broker.

How many devices you can connect to a broker

The number of connected devices “clients” to the broker depends on the broker service provider.

In fact, it can reach a massive number of clients those are publishing and subscribing all the time.

but the amazing part of this isn’t only the huge number of these connected devices but also any the fact that any device can get any other device’s data at any time. As a result, the applications based on these quickly shared data are limitless.

But one question here, what if you used HTTP protocol to share these data between devices? I would say that’s really a bad idea  😎


Why not HTTP

HTTP is slower, more overhead and power consuming protocol than MQTT. So, let’s get into each one separately:

  • Slower: because it uses bigger data packets to communicate with the server.
  • Overhead: HTTP request opens and closes the connection at each request, while MQTT stays online to make the channel always open between the broker “server” and clients.
  • Power consuming: since it takes a longer time and more data packets, therefore it uses much power.

Broker in the IoT system components

In the previous IoT blog, we discussed the IoT system in details and its 4 components. I would recommend you go through it if this is your first time reading about IoT.


One of these components was the IoT platform. It’s responsible for connecting the devices together, indeed.

Though, the IoT platform is just a cloud-based software that implements a broker server and some software with GUI to allow user control and monitor his connected devices.

Therefore, MQTT broker is included implicitly inside the IoT platform.

But we won’t use an IoT platform in our upcoming IoT blogs and tutorials.

That’s because we will only connect a few devices for small projects. So, IoT platform isn’t needed here. But it was necessary to get you a bit knowledge about it.


Which broker to use?

There are many brokers that implement the MQTT protocol. One of the most popular and commonly used is the mosquito broker.


Mosquitto broker

Mosquitto is an open source message broker that implements the MQTT protocol. It’s lightweight and suitable for use on all devices from a low power single board like Arduino, ESP8266 to full computers and servers.


But rather than using the Mosquitto on a local PC, you will need to use a cloud-based server that implements the Mosquitto broker. That’s necessary to make your IoT projects controllable over the internet.


Cloud-based Mosquitto brokers are many, like:

  • ThingMQ
  • ThingStudio
  • Heroku
  • CloudMQTT

And I will focus on the last one in this and the upcoming blogs.


CloudMQTT broker

CloudMQTT is one of the best and easiest cloud-based Mosquitto broker.

CloudMQTT has a free plan that allows you to set up your own CloudMQTT broker instance that will run on their hardware servers. Hence, you can have an online broker that’s ready to use in your IoT project.

It also has a well designed GUI to monitor the publishing and subscribing processes and topics through an easy to use WebSocket UI.


How to use CloudMqtt

Let’s give CloudMQTT a try to figure out how it works:

1- Go to the cloudmqtt and register an account.

2- Then press on the “+ create new instance” green button to get an instance of the MQTT based mosquito cloud broker that cloudMqtt offer.

3- Just write the broker instance name, like: “My_MQTT_Broker_Instance” and press “create new instance”

4- I have created a broker instance before and named it “try”, but on your account, you will find only the one you have created. Press on its name to see the details.

5- Here you can find the authentication details that any device will need to connect to your broker. We are interested only in the first 4 parameters.

6- cloudMqtt offers a pretty feature which is the WebSocket UI that allows you to monitor and publish and subscribe happens within this broker.

Now you have a free broker that you can connect any device to it.


Moreover, let’s get practice by using your smartphone as a device that will connect to the broker. So, make sure your phone is already connected to the Internet via wifi or data.


CloudMQTT Tutorial

I will use an application to connect the phone to the CloudMQTT broker.

There are many Apps there for connecting the smartphone to a broker and using it to publish and subscribe for topics. I have chosen a beautiful, simple and easy to use one. It’s called “IoT MQTT Panel Pro”.

In addition, it has many UI components to use as a visual indicator for the subscribed and published topics.

You can download it from here for the Android phones.



1- After downloaded, open the App.

2- Then press on the “+” button to enter the broker authentication data.

3- Enter the broker data as follows “these data will differ from yours of course”.

You have to enter choose the password as it is, capital or small letters aren’t the same.

4- Then on the “Device list” press the “+” button and add a device that will connect to this broker. Let’s call it “Home”. Then press the “create” button.

5- You will see the broker that you have just made is ready, press it.

6- Let’s add some a button to use it to visualize publishing and a bar to visualize subscribing. Press “ADD PANEL” button.

7- You will see a list of many buttons and figures. Let’s select the second element, the “switch” to use as the event that’s responsible for publishing.

8- Then fill in the fields with the Panel name, the Topic which is “switch”, the message to be sent when the switch is on “called here Payload on” and the message to be sent when the switch is off “called here Payload off”

8- Press on “Use icon switch” then choose a color for the “on” icon to make it distinguishable.

9- Once finished, press “create”.

10- Now, press the “+” button to add the subscriber. Let’s choose the “Gauge”.

11- Then fill in the Panel name, the Topic that this Gauge will visualize its messages and the min, max values of the gauge.

12- Press the “create” button.

13- Then press on the top right arrows icon to connect this device to the broker.

14- As a result of the succeeded connection, the icon will be colored with orange to indicate that the device is now connected to the broker.

Run it

Now, let’s back to the WebSocket UI in the browser and monitor what’s going on.

When you press the switch to make it on, the device will publish a message which is “on” to a topic which is “switcher”. You will see that clearly in the WebSocket UI.

And when you publish a message “20” or any other number from the WebSocket UI on the topic “sensor”, the device subscriber “gauge” will get this message and updates the value to that number.


Furthermore, you have to draw your IoT structure like this image. That’s to make it easy for you to get a clear idea about your project elements and how each element will communicate with the others.


Check this video for a demo:

After all, you have just completed your first IoT project using MQTT protocol with a connected device!


So, what’s next!

I think you are ready to move to another tutorial to know how you can implement what you have just learned here into a real-life IoT project.

surely, a real IoT-based project means connecting a hardware, like the popular Arduino Uno board to the MQTT broker. And use it to publish and subscribe for topics.

Of course, Arduino alone can’t connect to the Internet, so we will use a shield that can connect the Arduino to the Internet and the broker. That shield is the IoT Shield from 1Sheeld+ which will facilitate the coding for our upcoming IoT tutorial. Furthermore, you would like to check this IoT Shield Function Explainer to get more familiar with the shield functions and how to use them.


Also, don’t hesitate to leave your comments below with any question you want 🙂

And stay tuned for the upcoming MQTT tutorials  😉

Disclaimer: I gathered lots of information from these sources: readitquick, mosquitto, mqtt, istsos, spindance.

Related Blogs

Arduino IoT

Arduino IoT Shield Functions Explainer

Arduino makers …. you can connect your Arduino to MQTT brokers(servers) and launch your IoT with Arduino IoT Shield! It’s the 1Sheeld IoT shield that will do the job for you. It’s one of the most important but recent Arduino shields in 1Sheeld. Simply, it turns your Arduino into MQTT client where you can publish and …

Arduino Temperature Humidity System

IoT Arduino Temperature and Humidity System

Monitor my home environmental parameters remotely is one of the most desirable things I have ever wanted to make. It’s not about purchasing such a system that can get the job done for me. But, passion to create it myself. So, today, I am going to connect the popular DHT sensor to an online MQTT …

Arduino Door Lock

IoT Arduino Door Lock using 1Sheeld and Smartphone

  “Oh God! … It seems like I have forgotten the door lock open, again” … the normal saying whenever I am in hurry to go out! Have you encountered this problem before? It always confuses me since I may even think that I have left the door lock opened, and once I go back, …

IoT Fan Control

Arduino IoT Fan Control using 1Sheeld and Smartphone

Here I am with another IoT-based home automation project; Arduino IoT Fan Control. Every day, I get back from work tired and feeling hot. No air conditioner here and the fan takes much time to start cooling the room, sadly! So, I was thinking why not controlling the fan from outside my home with my …

Arduino Light Control

IoT Arduino Light Control using 1Sheeld and Smartphone

Have you ever forgotten to turn your home lights off when you got outside? Sadly, it happens to me all the time. And this causes confusion once I get my bills! Furthermore, here is one secret about me; I am a lazy man. I always want to control my room lamp from my sweet bed. …


IoT: Understanding the Concepts

Actually, that’s not a phrase from the Terminator movies, it’s just the reality of nowadays! Statistics show that the number of connected devices in 2018 are 23+ billion. This is almost 3 times the population density of the Earth, which is 7.6 billion at the time of writing this blog. Furthermore, this number is increasing …