Skip to content

Raspberry Pi Mosquitto MQTT: Send messages and commands | ShillehTek

November 21, 2023

Video Tutorial (Optional)

Watch first if you want to see Mosquitto MQTT set up on a Raspberry Pi and send messages and commands from your local computer in real time.

Project Overview

Raspberry Pi + Mosquitto MQTT: In this tutorial, you set up a Mosquitto MQTT broker on a Raspberry Pi and use Python to send messages and commands to and from a local computer over your local network.

MQTT is free, lightweight, and a great foundation for IoT and distributed systems projects.

Subscribe: Youtube

Support: https://www.buymeacoffee.com/mmshilleh

Interactive version and downloads (Razzl): https://apps.apple.com/us/app/razzl-watch-make-learn/id1604713309

  • Time: 20 to 40 minutes
  • Skill level: Beginner
  • What you will build: An MQTT setup where your Raspberry Pi listens for messages and your local computer publishes to a topic using Python

Parts List

From ShillehTek

  • None required for this software-based project

External

  • Raspberry Pi (any model that can run Mosquitto)
  • Local computer on the same network as the Raspberry Pi
  • Mosquitto MQTT broker and clients (installed via apt)
  • Python package: paho-mqtt (installed via pip)
  • Optional: Razzl app for interactive instructions and downloads: https://apps.apple.com/us/app/razzl-watch-make-learn/id1604713309

Note: This tutorial configures Mosquitto to allow anonymous connections on port 1883. Only do this on a trusted local network.

Step-by-Step Guide

Step 1 - Raspberry Pi setup (Mosquitto + Python)

Goal: Install Mosquitto and the Python MQTT library on your Raspberry Pi, then configure the broker so it can accept connections.

What to do: On your Raspberry Pi, run the following commands in a terminal.

Code:

sudo apt update
sudo apt install -y mosquitto mosquitto-clients
pip install paho-mqtt

Add these two lines to the Mosquitto config:

Code:

listener 1883
allow_anonymous true

Edit the config file (add the lines at the bottom):

Code:

sudo nano /etc/mosquitto/mosquitto.conf

Restart Mosquitto:

Code:

sudo systemctl restart mosquitto

Find your Raspberry Pi IP address by running ifconfig and looking under the wlan0 section (see image).

Raspberry Pi terminal output showing ifconfig wlan0 section to find the Raspberry Pi IP address for MQTT broker
Use ifconfig to find the Raspberry Pi IP address (wlan0) for your MQTT broker connection.

At this point, your Raspberry Pi has the infrastructure needed to use Mosquitto.

The remaining work is to run the Python code (from the project on Razzl or copied from the video). Run it once and keep it running as long as you want the Raspberry Pi to receive messages. You can modify the on_message function to match your project needs, since it runs each time your MQTT topic receives a message.

Expected result: Your Raspberry Pi is ready to act as an MQTT broker and run a Python script that waits for messages. When you first run the script, you should see a result code of 0.

Step 2 - Run the local computer publisher

Goal: Publish a message to your Raspberry Pi MQTT broker from your local computer using Python.

What to do: On your local computer, run the Python code attached to the project on Razzl (or copy it from the video). This script sends a message to the MQTT topic on the broker.

Make sure you also install the MQTT Python library on your local computer:

Code:

pip install paho.mqtt

If everything is successful, you should see a message appear in the console output of the Python code running on your Raspberry Pi (see image).

Raspberry Pi console showing a received MQTT message after publishing from a local computer using Python
Example output: the Raspberry Pi receives and prints the MQTT message sent from your local computer.

Expected result: Your Raspberry Pi receives the published message and prints it in the terminal running the Python subscriber code.

Conclusion

You set up Mosquitto MQTT on a Raspberry Pi and used Python (paho-mqtt) to send messages and commands from your local computer over your local network. With this working, you can extend the same pattern to more advanced tasks like triggering GPIO actions or other automation.

Want parts and tools for your next IoT build? Shop at ShillehTek.com. If you want help customizing an MQTT-based system or building something for your product, check out our IoT consulting services.