Install Mosquitto MQTT Broker on Raspberry Pi

In this tutorial, we will learn to install Mosquitto MQTT broker on Raspberry Pi. Mosquitto broker will be used for MQTT communication on Raspberry Pi. 

Install Mosquitto MQTT broker Raspberry Pi

Although in this tutorial, we will install mosquitto broker on raspberry pi, but you can also install it in the cloud by buying a hosting. By using a mosquito broker in the cloud, we will be able to connect many MQTT clients running on various boards such as ESP32 and ESP8266 with mosquitto broker. Moreover, IoT devices will be able to communicate with MQTT broker from anywhere globally by having an internet connection. 

In this tutorial, we will focus on installing mosquitto broker on Raspberry Pi only. 

You can also install mosquitto on your Windows and Ubuntu machine. But it is not recommended to do so. Because you need to keep your machine up and running all the time in order for IoT devices to remain connected with the broker.

What is MQTT

MQTT is a lightweight messaging protocol for IoT device communication.

MQTT example block diagram
  • MQTT is known as Message Queuing Telemetry Transport protocol.
  • It is a lightweight messaging protocol and helps resource-constrained network clients with a simple communication mechanism.
  • Unlike, most messaging systems, we don’t have to assign addresses to MQTT clients.
  • MQTT uses simple publish/subscribe communication-based on a topic.
  • This protocol runs on top of TCP / IP in order to provide reliable data delivery.
MQTT Protocol Introdution

In order to understand MQTT protocol, you should grasp these four main components conpents

MQTT Publisher/Subscriber

The first main concept in MQTT is a Publisher/ Subscriber system.  A publisher device publishes a message on a specific topic and a Subscriber device subscribes to the topic which will be sent by a publisher. On top of that, more than one device can subscribe to the topic.

MQTT Publisher Subscriber Example

For example, according to above figure:

  • Device1 is a publisher and Devices(2-3) are subscribers.
  • Let’s suppose, device1 publishes on a topic “ESP32/Temperature” and Devices(2-3) subscribe to the same topic.
  • Hence, Devices(2-3) are receivers and Device1 is a sender.

Installing Mosquitto Broker on Raspberry Pi

First, you need to set up Raspberry Pi with the latest Raspian image. You can follow this guide to setup: 

After you have set up your Raspberry Pi and you have Raspbian OS running. Now open the terminal of your Raspberry P and start installing Mosquitto Broker.

Open raspberry pi terminal

Now, we should update and upgrade Raspberry packages with the following command to fix if any packages are outdated or missing:

sudo apt update && sudo apt upgrade

This command may ask you for the option of Y/N which means either you want to update and upgrade packages or not. Select Y and press enter. It will take some time to update and upgrade packages depending on your internet package.

update and upgrade raspberry pi

Now we are ready to install Mosquitto Broker on Raspberry Pi. To install it execute the following commands on the terminal:

sudo apt install -y mosquitto mosquitto-clients

The above command will install mosquitto broker and mosquitto client applications. Mosquitto client consists of mosquitto_pub and mosquitto_sub applications that we can use to subscribe and publish to/from MQTT topics. 

The Mosquitto package creates a systemd service that we can use to start and stop mosquitto broker on Raspberry Pi. 

The mosquitto.service file enables the mosquitto broker on Raspberry Pi on bootup by default. In other words, the Mosquitto broker will start automatically when Raspberry Pi starts. But we can start and stop it using this command. 

sudo systemctl enable mosquitto.service
mosquitto broker running on raspberry pi

Now let’s test the Mosquitto broker installation by running mosquitto with the following command:

mosquitto -v

this command will return the mosquitto broker version that we just installed and running on our raspberry pi. 

One important point to note in the output of the above command is the following message:

“Starting in local-only mode. Connections will only be possible from clients running on this machine. Create a configuration file which defines a listener to allow remote access.”

That means we can only access mosquitto broker within a local machine and we need to create a listener port that we can use to listen to requests from other machines. 

In order to enable remote access outside the localhost of Raspberry pi, we need to edit mosquitto.conf file. By doing so, we will allow other IoT devices to communicate with Raspberry Mosquitto broker. 

To edit mosquitto.conf file, run the following command. It will open .conf file with a nano editor. 

sudo nano /etc/mosquitto/mosquitto.conf

Now, scroll to the end of this file and paste the following two lines:

After that save this file by entering CTRL+X followed by Y and Enter. 

edit mosquitto conf file

Finally, for changes to take effect reboot your raspberry pi with the following command and your Mosquitto broker is ready to use. 

pi@raspberry:~$ sudo reboot

Test Mosquitto Broker with MQTT.fx

Now let’s see if we can access our mosquitto broker from another machine or not. To test it, let’s install a mosquito client application on one of our Windows machine. There are many Desktop mosquitto client applications available but in this tutorial, we will use a popular one that is MQTT fx. 

Go to this link, download, and install MQTT fx on your Windows, Linux, or Mac machine. 

After installing MQTT.fx, find the IP address of your raspberry pi with this command and also check mosquitto broker should be running on your Raspberry pi:

raspberry pi ip address

Now open the MQTT.fx desktop client application and click on the Settings button to connect with Mosquitto brother which is running on Raspberry Pi. 

open mqtt fx mosquitto client

MQTT client profile settings page will open as shown below. Now give any descriptive name to your broker like we have given “pi” in this example. In the second option write the IP address of your Raspberry Pi and in the third line write the port number which is usually 1883. After that click on Ok. 

mosquitto broker settings in mqtt fx

Now click on connect button and you will see that the indicator LED on the top right side of MQTT.fx will turn green from black which indicated that MQTT.fx is successfully connected with your Rasperry Mosquitto broker. 

mqtt fx connection with raspberry pi

Now let’s see an example to publish a message from MQTT.fx to an MQTT client which we will run on Raspberry. 

In order to subscribe to an MQTT topic with Mosquitto Client on Raspberry Pi, open the Rpi terminal and enter the following command: 

pi@raspberry:~$ mosquitto_sub -d -t test_topic
raspberry pi subscribe to topic

When we install a mosquitto broker on Raspberry Pi, it also installs mosquitto_sub and mosquitto_pub client applications which we can use to publish and subscribe to topics Hence, the above command will subscribe to a topic with the name of  test_topic.  Therefore, whenever, any publisher will publish a message on this topic, we will receive it on this Raspberry terminal. 

Now let’s publish a message on test_topic by using the MQTT.fx application. In the client application, click on “Publish” in the second menu bar, and write the topic name and message in the message window as shown with an arrow. Now click on Publish button. It will publish a message on “test_topic”.

publish message with mqtt fx

Now if you see the Raspberry Pi terminal, you will see a message received by the Raspberry client:

raspberry pi mqtt message receive

Similarly, we can subscribe to topics with MQTT.fx and receive message notification whenever any client client publish a message like this:

subscribe to a topic with mqtt fx

Now, on the raspberry terminal type the following command to publish a message:

pi@raspberry:~$ mosquitto_pub -d -t test_topic -m "Hello world!"

As you can see the message published with Rpi is received on MQTT.fx.

mosquitto pub sub example with mqtt fx and raspberry

If you don’t want to use MQTT.fx, you can test Mosquitto broker by using mosquitto_sub and mosquitto_pub application. Open multiple Raspberry Terminals and publish/subscribe to messages from these terminals as shown below:

mosquitto multiple pub sub with Raspberry pi

In the above example, Window#1 and Window#3 have subscribed to a topic “Hello_topic” and Window#2 publishes a message on “Hello_topic”. Hence, two clients will receive a message from “Hello_topic” MQTT topic. As you can also see, both clients have received a message “Hello World!”.

Video demo:

Summary

An MQTT broker is the main component of an MQTT protocol. It receives MQTT messages from all MQTT clients and forwards them to all clients which subscribed to those topics.

Leave a Comment