Raspberry Pi

What is MQTT and how to use it with Raspberry Pi

The Raspberry Pi is a multi-purpose small size device which has the power to become people’s ideal choice in every aspect. It has started to set its mark into the Internet of Things (IoT) world and has quickly become an excellent source to bring people into the whole new world. When we talk about IoT, the only thing that comes to our mind is the communication of devices over the internet but on the other side some people will also need an answer about how a Raspberry device will be used for communication purposes.

If you are the one among those people who don’t have any knowledge then you should follow this guide which will teach you how you may utilize your Raspberry Pi device for communication purposes with the help of a Message Queuing Telemetry Transport (MQTT) protocol. Before proceeding towards its use with Raspberry Pi, it is important for you to acquire the basic understanding of MQTT.

What is MQTT

The MQTT is a machine-to-machine communication protocol that has the capability to transmit the messages between the two devices. It is created expressly to work in an embedded atmosphere so that it would provide users dependable and effective path communication. It is built on TCP/IP, but any network protocol that enables lossless, bidirectional and ordered communications can serve MQTT.

How to use it with Raspberry Pi

MQTT protocol relies on the publish and subscribe principle which means a topic will be required to publish somewhere else so that subscribers will see the published topic. Take an example of LinuxHint website where the website will act as a broker that is it contains the information of different blogs posted by the publisher (writer). The subscribers are the one who see those blogs (topics) and can exchange messages with the publisher which is routed through the LinuxHint (broker).

Here, we will make the Raspberry Pi device as a source to deliver the message from one device to another device. It means we will need to construct a broker on a Raspberry Pi device and we will use Mosquitto broker in that case which truly operates on the idea of MQTT protocol.

Before beginning the installation, it is must for us to check the package updates so that we won’t encounter an error while installing the broker on Raspberry Pi. To update the packages, the following command needs to be executed.

$ sudo apt update

$ sudo apt upgrade

Once ensuring all packages are successfully updated, you will then move ahead and perform the mosquito installation. As mosquitto broker is available in the Raspberry Pi repository so the installation command is pretty simple which is given below.

$ sudo apt install mosquitto mosquitto-clients

The mosquitto-clients will ensure that our MQTT broker is running perfectly fine and if you want to check the status, you can use the following command.

$ sudo systemctl status mosquitto

The above command will return with active status ensuring the service is running fine.

Now, we will need to test our mosquitto service and to do that we will need to start up a subscriber which will listen to our MQTT broker.

$ mosquitto_sub -h localhost -t "mqtt/LinuxHint"

From the above command, the “h” tag defines the localhost which you will need to specify. In our scenario, the hostname is our Raspberry Pi device so we won’t require an IP address in that case. The use of “t” tag informs mosquitto about the topic “LinuxHint” that you will listen to from MQTT broker.

Now after loading the client up on your device, you are now good to publish a message so that the subscriber can see it. This can be possible when you execute the below mentioned command in another terminal.

$ mosquitto_pub -h localhost -t "mqtt/LinuxHint" -m "Welcome LinuxHint users"

Once the above command is executed, the Raspberry Pi who is acting as a broker as well will publish the message on the terminal window.


MQTT has become a prominent protocol for communication between different IoT devices and having a Raspberry Pi device with you will open up the opportunity of creating a reliable and effective path for exchanging different forms of information or data. The message exchanging method is not difficult once you install mosquitto on your Raspberry Pi using the steps provided in this guide.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.