Install Mosquitto Broker on Raspberry Pi

Mosquitto is a message broker that implements the MQTT protocol. Mosquitto is an open source project developed by Eclipse. MQTT protocol uses a publish/subscribe model. Client can publish message to a broker and other clients can subscribe to the topic of that message. A broker is a central component that receives all messages from the clients and then publishes the messages to all subscribed clients.

This tutorial shows how to install Mosquitto broker on Raspberry Pi.

Connect to Raspberry Pi via SSH and execute the following commands to install Mosquitto broker:

sudo apt update
sudo apt install -y mosquitto

After installation we can check Mosquitto version.

mosquitto -v

Mosquitto broker is installed as a service and we can check status.

sudo service mosquitto status

We can stop, start and restart the Mosquitto service by using these commands:

sudo service mosquitto stop
sudo service mosquitto start
sudo service mosquitto restart

To make Mosquitto auto start after reboot we can use the following command:

sudo systemctl enable mosquitto

If we want completely remove any package with a name that starts with mosquitto and anything related to it we can execute this command:

sudo apt purge -y mosquitto.*

In order to test Mosquitto broker, we need a client. First we need to determine IP address of the Raspberry Pi.

hostname -I

MQTT Explorer is a cross-platform MQTT client which can be installed as Desktop application. It can be downloaded from official website.

Once MQTT Explorer was opened we need to give details about MQTT connection. Enter the connection name you want, then provide IP address of the Raspberry Pi and click “SAVE”. By default, Mosquitto broker is running on 1883 port. Finally, click “CONNECT”.

Connect to Mosquitto Broker with MQTT Explorer

If connection was successfully established, then we should see a window like this:

MQTT Explorer Main Window

Leave a Comment

Your email address will not be published. Required fields are marked *