How to Install Mosquitto MQTT Broker on Raspberry Pi

Mosquitto is an open-source message broker that implements the MQTT protocol. In this tutorial, TechSparks will provide a comprehensive guide on installing the Mosquitto MQTT service on Raspberry Pi, making the entire process easier for you.

Table of Contents

MQTT and Mosquitto

MQTT, short for Message Queuing Telemetry Transport, is a lightweight, open-source message publish/subscribe protocol designed to address data exchange between devices in conditions of low bandwidth, high latency, or unstable network connections. By adopting the publish/subscribe model, devices can freely publish and subscribe to messages, supporting features such as retained messages and last will messages when a device disconnects. This design enhances the flexibility of managing device online and offline states. MQTT offers three levels of message delivery services, ranging from the lowest level of at most once delivery to the highest level of exactly once delivery.

MQTT communication mode

Mosquitto, on the other hand, is an open-source MQTT message broker supporting both MQTT protocol versions 3.1 and 3.1.1. Despite the availability of other brokers, Mosquitto is designed with a focus on lightweightness, making it highly suitable for resource-constrained devices and network environments. Additionally, Mosquitto provides support for TLS/SSL-based encrypted communication, adding an extra layer of security to the communication. Its straightforward installation process and flexible configuration options allow users to customize it based on specific requirements.

Mosquitto logo

Installing Mosquitto

First, you need to log in to your Raspberry Pi and ensure that it is connected to the network using the following command:

					pi@raspberrypi:~ $ ping

In this command, “ping” is used to test the network connection to the target host, and here I’m using Baidu, but you can replace it with another host.

If you see the following interface during runtime, it means the network is not accessible:

Interface with no network connection

You need to refresh and reconfirm in the WLAN interface. After retesting, if you see the following interface, it means the network communication is normal:

Interface with normal network connection

To ensure that the package information you are using is up-to-date, you can use the following command to update the apt package resources:

					sudo apt-get update

Then, you need to enter the following command to search for installable MQTT packages:

					apt search mqtt

In this Raspberry Pi project, we are using Mosquitto. MQTT has both server and client components, so execute the following commands to install both parts:

					sudo apt-get install mosquitto mosquitto-clients
Install mosquitto

Finally, execute the following command to check if the installed Mosquitto package is correct:

					dpkg -l mosquitto
Check the mosquitto installation package

Configuring Mosquitto

Before using Mosquitto, it’s necessary to configure the file for personalized settings. The configuration file is located at /etc/mosquitto.

First, you can use the following command to view the configuration of the Mosquitto service and understand its parameter settings and operation:

					cat /etc/mosquitto/mosquitto.conf
Mosquitto file details

In the comments, it mentions placing local configurations in the /etc/mosquitto/conf.d/ directory.

Check the /etc/mosquitto/conf.d/ directory:

					ls /etc/mosquitto/conf.d/

There is a README file in the directory. Open it to see more details:

					cat /etc/mosquitto/conf.d/README

Any files with the .conf extension placed in this directory will be loaded by the broker as local configuration files.

The complete configuration file documentation is available at: /usr/share/doc/mosquitto/examples/

Copy the mosquitto.conf.gz configuration file to the /etc/mosquitto/conf.d/ directory:

					sudo cp /usr/share/doc/mosquitto/examples/mosquitto.conf.gz /etc/mosquitto/conf.d/

Navigate to the /etc/mosquitto/conf.d/ directory:

					cd /etc/mosquitto/conf.d/

Decompress the mosquitto.conf.gz file:

					sudo gzip -d mosquitto.conf.gz

Edit the mosquitto.conf file:

					sudo nano /etc/mosquitto/conf.d/mosquitto.conf

This is a relatively large file with over 800 lines, and all lines are commented out with # symbols.

Make the following modifications by removing the comment symbols and adjusting the default values:

					user mosquitto
max_queued_messages 200
message_size_limit 0
allow_zero_length_clientid true
allow_duplicate_messages false
port 1883
autosave_interval 900
autosave_on_changes false
persistence true
persistence_file mosquitto.db
allow_anonymous false
Password_file /etc/mosquitto/passwd.conf

Save and exit the editor.

Return to the home directory:

					cd ~

Testing Mosquitto

When testing Mosquitto, we need to open three terminals to simulate the scenarios of Mosquitto service, publisher, and subscriber in MQTT communication.

Start the Mosquitto MQTT broker:

					mosquitto -c /etc/mosquitto/mosquitto.conf -v

Use the -v option to log all types of messages, which is useful for debugging. When using Mosquitto in a production environment, it’s generally not necessary to log all messages to avoid consuming storage space.

If you encounter a permission issue, check the permissions of the mosquitto.log file:

					ls -l /var/log/mosquitto/mosquitto.log

If you need to modify the log file’s permissions:

					sudo chmod 666 /var/log/mosquitto/mosquitto.log

Then execute the command again:

					mosquitto -c /etc/mosquitto/mosquitto.conf -v

The service is running, but the terminal window is occupied.

Test Publish and Subscribe

Open two additional terminals:

In one terminal, execute the Subscribe command:

					mosquitto_sub -p 1883 -u ct -P xxxxxx -t "test"

This subscribes to the “test” topic, waiting to receive messages.

In another terminal, execute the Publish command:

					mosquitto_pub -p 1883 -u ct -P xxxxxx -t test -m "Hello!"

This publishes a message with the topic “test” and the content “Hello!”.

The Subscribe window should display the received message.

Check the log:

					sudo cat /var/log/mosquitto/mosquitto.log

Finally, restart the Mosquitto service to ensure the configuration takes effect:

					sudo reboot

Check the running Mosquitto processes:

					ps -ef | grep mosquitto

Now, Mosquitto is running in the background, ready to receive and process MQTT messages.

You Might Be Interested

raspberry pi autostart
How to Auto Start Programs on Raspberry Pi

Automating program startup on the Raspberry Pi can be achieved through various methods. Editing the “/etc/rc.local” file or using desktop applications, while simpler, may not

Scroll to Top