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

              Implementing Google reCAPTCHA in a Simple React and Node.js App

              Implementing Google reCAPTCHA in a Simple React and Node.js App

              Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

              AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

              AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

              In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

              How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

              How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

              Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

              Raspberry Pi Pico/Pico W Free Simulator

              Raspberry Pi Pico/Pico W Free Simulator

              Discover how to simulate Raspberry Pi Pico projects using Wokwi, a free online simulator for Arduino and MicroPython....

              Interfacing the MPU6050 with Raspberry Pi Pico W in C++

              Interfacing the MPU6050 with Raspberry Pi Pico W in C++

              Interface with the MPU6050 using the Raspberry Pi Pico W in C++.

              How to Write your First C++ Program on the Raspberry Pi Pico W

              How to Write your First C++ Program on the Raspberry Pi Pico W

              Write your first C++ Program on the Pico W in a few simple steps.

              How to Use ThingSpeak with the Raspberry Pi Pico W

              How to Use ThingSpeak with the Raspberry Pi Pico W

              Learn how to create a real-time environmental monitoring system with the Raspberry Pi Pico W and ThingSpeak!

              How to Use ADS1115 with the Raspberry Pi (Part 1)

              How to Use ADS1115 with the Raspberry Pi (Part 1)

              Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

              How to Install Pip Packages in AWS Lambda Using Docker and ECR

              How to Install Pip Packages in AWS Lambda Using Docker and ECR

              Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

              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...

              Back to blog

              Leave a comment

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