This tutorial shows how to install the Mosquitto Broker for MQTT communication on a Raspberry Pi board.
Updated 15 December 2021
You can also run Mosquitto MQTT broker in the cloud. Running the MQTT Mosquitto Broker in the cloud allows you to connect several ESP32/ESP8266 boards and other IoT devices from anywhere using different networks as long as they have an Internet connection. Check the tutorial below:
What is an MQTT Broker?
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.
MQTT communication works as a publish and subscribe system. Devices publish messages on a specific topic. All devices that are subscribed to that topic receive the message.
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).
For a more detailed explanation about MQTT communication check out this article: What is MQTT and How It Works
Before continuing with this tutorial
- You should be familiar with the Raspberry Pi board – read Getting Started with Raspberry Pi;
- You should have the Raspberry Pi OS installed in your Raspberry Pi – Install Raspberry Pi OS, Set Up Wi-Fi, Enable and Connect with SSH;
- You also need the following hardware:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
If you like home automation and you want to build a complete home automation system, I recommend downloading my home automation course.
Installing Mosquitto Broker on Raspberry Pi OS
After having your Raspberry Pi board prepared with Raspberry Pi OS, you can continue with this tutorial.
Let’s install the Mosquitto Broker.
1) Open a new Raspberry Pi terminal window. If you’re running your Raspberry Pi headless, check this tutorial to learn how to establish an SSH connection between your computer and the Raspberry Pi.
2) Run the following command to upgrade and update your system:
[email protected]:~ $ sudo apt update && sudo apt upgrade
3) Press Y and Enter. It will take some time to update and upgrade (in my case, it took approximately 10 minutes).
4) To install the Mosquitto Broker enter these next commands:
[email protected]:~ $ sudo apt install -y mosquitto mosquitto-clients
5) To make Mosquitto auto start when the Raspberry Pi boots, you need to run the following command (this means that the Mosquitto broker will automatically start when the Raspberry Pi starts):
[email protected]:~ $ sudo systemctl enable mosquitto.service
6) Now, test the installation by running the following command:
[email protected]:~ $ mosquitto -v
This returns the Mosquitto version that is currently running in your Raspberry Pi. It will be 2.0.11 or above.
It will prompt 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.”
This is only applicable for Mosquitto version 2. More information about this topic on the Mosquitto documentation.
To enable remote access so that we can communicate with other IoT devices, we need to edit/create a configuration file.
7) Run the following command to open the mosquitto.conf file.
[email protected]:~$ sudo nano /etc/mosquitto/mosquitto.conf
8) Move to the end of the file using the arrow keys and paste the following two lines:
9) Then, press CTRL-X to exit and save the file. Press Y and Enter.
10) Reboot your Raspberry Pi with the following command for the changes to take effect.
[email protected]:~$ sudo reboot
Raspberry Pi IP Address
To use Mosquitto broker later in your projects, you’ll need to know the Raspberry Pi IP address. To retrieve your Raspberry Pi IP address, type the next command in your Pi Terminal window:
[email protected]:~ $ hostname -I
In our case, the Raspberry Pi IP address is 192.168.1.144. Save your Raspberry Pi IP address because you’ll need it in future projects.
Testing Mosquitto Broker and MQTT Client
After installing the Mosquitto broker, you should test your installation. You can follow the next tutorial:
An MQTT broker is essential if you want to use the MQTT protocol in IoT projects. The MQTT broker receives all MQTT messages and forwards them to all subscribed clients. In this tutorial, you’ve learned how to install the Mosquitto broker on a Raspberry Pi.
Like home automation? Learn more about Node-RED, Raspberry Pi, ESP8266, and Arduino with my course: Build a Home Automation System for $100.
Do you have any questions? Leave a comment down below!
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing to my blog.
Updated December 15, 2021