Setting up Mosquitto with a Python Subscriber as a Service

Published on: November 22, 2020

Welcome to the 19th Raspberry Pi tutorial! This tutorial will cover setting up Mosquitto on your Raspberry Pi and test the broker and the client from your Pi and your computer using MQTT Lens. 

I plan on doing some more work with MQTT in the near future (Setting up Google assistant without exposing my internal network) so I thought it would be a good place to start. You can also set up MQTT on your HomeAssistant (HASSIO) and use that as your broker. But I thought it may be beneficial to do some of this from scratch.  

Installing Mosquitto

Mosquitto is an open source MQTT broker. It's created by the Eclipse foundation and you can get more info here: https://mosquitto.org

All of the steps in this tutorial can also be found on the GitHub repo (https://github.com/naztronaut/EP-MQTT). Let's install Mosquitto with this command: 

    sudo apt install -y mosquitto mosquitto-clients

The set up shouldn't take more than a minute. After you are done, you can the Mosquitto service to your systemctl so that the service is maintained by your system so that if your Pi ever reboots, the service is turned right back on:

    sudo systemctl enable mosquitto.service

That's pretty much it to install Mosquitto.  You can continue to use it as-is without needing to change the configuration. In this tutorial, I don't show you how to set up authentication because my video was already longer than I wanted, but if there's enough interest, I will create a follow-up on that set up. 

Testing Your Mosquitto Install from CLI

Now that we set things up, we can test to make sure our broker is working and that it can receive messages from clients. You can test this directly from the command line. You should open a new shell window and log into your Pi. In one Pi, create a subscriber like below:

    mosquitto_sub -d -t "testTopic"

You need the -t flag with a topic set up so that your broker knows what to listen for. In your other shell window, publish a message:

    mosquitto_pub -d -t testTopic -m "Hello world!"

You need to make sure the topic lines up because that's what you are looking for. And if things work, you should see your "Hello World" message appear in the subscribed window (or in the case of the screenshot below "Hellow other window").

Testing Your Mosquitto Install from MQTTLens

MQTTLens is a Chrome app that you can use to test your MQTT setup. You can get the app here: MQTTLens for Chrome

You can create a new connection with your Pi's IP addresss as the hostname and keep the default port 1883. Since we didn't set up a username and password, they can be left blank:

Once that's set up, you can subscribe to a message on your Pi and publish it from MQTTLens to test your setup:

Setup Python Subscriber Script as a Service

To automate things, you can run the Python subscriber script (which uses the Paho-MQTT library) as a service. Whenever the Pi is on, that service will monitor your MQTT Broker for topics you've subscribed to and do whatever you want it to do. You can place the script anywhere in your Pi, but in this tutorial, I put it in /home/pi. Run this command to get the script:

    sudo wget https://raw.githubusercontent.com/naztronaut/EP-MQTT/main/mqtt_subscriber.py

On line 10, I've subscribed to 3 different topics. You can edit those as you please. Now let's install our service:

    cd /etc/systemd/system
    sudo wget https://raw.githubusercontent.com/naztronaut/EP-MQTT/main/mqtt_subscriber.service
    sudo systemctl daemon-reload
    sudo systemctl enable mqtt_subscriber.service
    sudo service mqtt_subscriber start

The above commands places the service file in your systemd location, restarts systemctl, enables the service, and starts the service. If everything goes well, your Pi should now be montirofing for changes to yo ur topics. If you make changes to the service, you should run daemon-reload again and restart the service. 

The Python script writes to a .txt file as a way to see changes immediately. I did that for the demo so if you want to see what the looks like, check out the video. 

My future plans include bridging this MQTT service (or the one I have running on my HASSIO) to one running in the cloud so that I can set up Google Assistant on IFTTT and do stuff with my voice. Stay tuned!

Patreon

I do these projects to learn and teaching is a good way to learn. But if you do want to support me, head on over to Patreon and become a Patron: https://www.patreon.com/nazmus

If you've implemented this project, I'd like to see it! So please share it with me through any of my regular channels. 

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:

Find the code on GitHub at https://github.com/naztronaut/EP-MQTT



Comments: