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

              How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

              How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

              Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

              Powering the Raspberry Pi Pico W with the MB102 Power Supply

              Powering the Raspberry Pi Pico W with the MB102 Power Supply

              Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

              How to Use L298N Motor Driver with Pico W

              How to Use L298N Motor Driver with Pico W

              Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

              Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

              Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

              Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

              Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

              Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

              In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

              How to Post to Reddit Using Python

              How to Post to Reddit Using Python

              Post to reddit automatically using a Python script.

              How to Create a Time-Lapse Video with a Raspberry Pi Camera

              How to Create a Time-Lapse Video with a Raspberry Pi Camera

              Learn how to make a timelapse with your Raspberry Pi in Python.

              How to Integrate the MPU6050 with the STM32 Blue Pill

              How to Integrate the MPU6050 with the STM32 Blue Pill

              Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

              Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

              Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

              This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

              Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

              Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

              In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

              Back to blog

              Leave a comment

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