Sending Messages & Commands to Your Raspberry Pi Using MQTT

In this tutorial learn how to send messages to/from your local computer to your Raspberry Pi over the local network using the Mosquitto MQTT and Python! It's free and incredibly easy to set up and manage. This can form the basis of incredible IoT and distributed systems applications! You can do some amazing things with this simple tool in practice.

Before reading the remainder, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

An interactive version of this video and downloadable instructions available on Razzl on the App Store and Android Store which contains the code and PDF instructions. Cheers!

IOS: https://apps.apple.com/us/app/razzl-watch-make-learn/id1604713309

Step 1-) Raspberry Pi Setup

On your Raspberry Pi run the following commands in your terminal.
  1. sudo apt update
  2. sudo apt install -y mosquitto mosquitto-clients
  3. pip install paho-mqtt
      Next, add these two lines to the config
      1. listener 1883
      2. allow_anonymous true
          Add them to the bottom of the file by first typing
          1. sudo nano /etc/mosquitto/mosquitto.conf
            You should then restart the mosquitto service with the command,
            1. sudo systemctl restart mosquitto
              The last information you need is the IP Address of your Raspberry Pi, you can get it by running the 'ifconfig' command in the terminal and looking at the following section (See Image). In the wlan0 section.
              Now you have all of the infrastructure you need on the Raspberry Pi side to use Mosquitto!
              The only thing left is to download and run the code in Python attached to this project on Razzl, or simply copy the code in the video.
              You should run the code once and let it remain running as long as you want to receive messages on the Pi, you can modify the on_message function to fit the demands of your project, this is the function that is called every time your MQTT channel receives a message.
              Your Raspberry Pi at this point is waiting for messages/commands, very exciting. You should also get a result code of 0 when you first run the script. That means the code is working.

              Step 2-) Local Code

              On your local computer run the following code, attached to the project on Razzl. It is very simple, you are just sending a message to the topic for the MQTT broker.
              Make sure you also pip install paho.mqtt
              If everything is successful you should see a message in the console of your Raspberry Pi Python code as shown in the image! Congrats you did it.
              You can now leverage MQTT and do more intricate things, such as send commands to control GPIO pins, maybe send a command to take a photo or more! This can form the basis of incredibly complex and powerful IoT and embedded systems applications.

              Conclusion

              Hope you enjoyed the video, let me know what you think of the Razzl platform. Be sure to follow me there and also follow me on YouTube and other social media. Also, would love to hear your questions in general and I look forward to doing more intricate MQTT and robotics tutorials for you guys.

              Create a free account to access full content.

              All access to code and resources on ShillehTek.

              Signup Now

              Already a member? Sign In

              Explore More on Our Blog

              Create Tabular Product Descriptions on Your Shopify Store

              Create Tabular Product Descriptions on Your Shopify Store

              Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

              SSH Into Raspberry Pi with Tailscale VPN

              SSH Into Raspberry Pi with Tailscale VPN

              Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

              Send Email with Lua and the ESP32

              Send Email with Lua and the ESP32

              In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

              How to Code with Lua on ESP32 with XEdge32

              How to Code with Lua on ESP32 with XEdge32

              Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

              Stream Audio From Raspberry Pi to Local Computer

              Stream Audio From Raspberry Pi to Local Computer

              Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

              SSH Raspberry Pi via Cell Phone

              SSH Raspberry Pi via Cell Phone

              This beginner-friendly guide will walk you through remotely controlling your Raspberry Pi using SSH through your cell phone.

              Remotely Control Raspberry Pi via SSH from External Network

              Remotely Control Raspberry Pi via SSH from External Network

              Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control...

              Stream Video from Raspberry Pi Camera to YouTube Live

              Stream Video from Raspberry Pi Camera to YouTube Live

              Learn how to stream to YouTube from a Raspberry Pi Camera.

              How to Connect BH1750 with Arduino: Measure Ambient Light

              How to Connect BH1750 with Arduino: Measure Ambient Light

              Learn how to measure ambient light for smart lighting control using Arduino and the BH1750 Light Intensity Module.

              How to Connect MPU9250 and Raspberry Pi (Part 2 - Calibration)

              How to Connect MPU9250 and Raspberry Pi (Part 2 - Calibration)

              Learn how to calibrate the MPU9250 in Python with the Raspberry Pi to get more accurate acceleration and gyroscopic...

              Back to blog

              Leave a comment

              Please note, comments need to be approved before they are published.