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 -t test -m message
You should see the message appear in the subscribe window.
Related Tutorials and Resources:
This only installs a updated 1.6.x series on Raspberry Pi but 2.0.x on Ubuntu.
I have problems with getting one broker sending topics to another on the 1.6.x series (random socket errors and difficulty re-connecting) that appears to work find on the 2.0.x series on Ubuntu, but I would really like to use the Pi.
I don’t think the v2 is available for the pi yet as it is a different chipset. I haven’t seen socket errors on pi how are you testing?
rgds
Steve
Thank you so much, only this method worked with me to install Mosquitto on Rpi 3.
I think your second test window should have `mosquitto_pub -h localhost -t test -m message`
Yes tks I’ve changed it.
Rgds
Steve