How to Install Mosquitto on The Raspberry Pi

Mosquitto is a very lightweight broker and a Raspberry Pi can easily cope with MQTT traffic on clients on a smart home networks..

Mosquitto doesn’t come installed by default so you will need to install it.



Install Steps

First get the repo key

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key

Then make the repository available :

cd /etc/apt/sources.list.d/

Then , depending on which version of debian you are using:

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list

Then update apt information:

sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

For windows and linux see this tutorial on my other site.

Starting and Configuring Mosquitto

The install installs mosquitto as a service which starts automatically and tuns on port 1883.

If you need to change the configuration then the configuration file is called mosquitto.conf and is located in the /etc/mosquitto folder.

On the Pi mosquitto can be controlled using the
systemctl command. You can sopt,start and restart the service using.

sudo systemctl stop mosquitto.service
sudo systemctl start mosquitto.service
sudo systemctl restart mosquitto.service

You can find more details, like how to run multiple instances, change port etc on my other site here.

Running Mosquitto Manually

When testing you will find it very useful to run mosquitto manually from the command line.

The first step is to stop mosquitto using:

sudo systemctl stop mosquitto.service

Then you can start it using:

mosquitto -v   #start in verbose mode

the -v option displays verbose logging information other command line options are:

  • -p port_number
  • -c configuration file

Examples
To start mosquitto using a test configuration file use

mosquitto -c test.conf

To start mosquitto using port 1884 use

mosquitto -p 1884

Testing the Install

If you followed the install instructions you will have also installed the mosquitto test clients mosquitto_pub and mosquitto_sub.

You can use these clients to test your mosquitto install.

The first step is to subscribe to a topic and then to publish a message to that topic which you should see in the subscribe client.

So first open two command prompts in one type.

mosquitto_sub -h localhost -t test -d

in the second type

mosquitto_pub -h localhost -m test message

You should see the message appear in the subscribe window.

test-mosquitto-broker-pi

Related Tutorials and Resources:

Please Let me Know if you found it Useful
[Total: 0 Average: 0]

Leave a Reply

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