Sending Messages & Commands to Your Raspberry Pi w MQTT P2

Prerequisites:

P1:

S3 + Raspberry Pi Video:

Learn how to send local commands to your Raspberry Pi to create actions using MQTT and Node

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!

https://share.razzl.com/fftd

Step 1-) Raspberry Pi Code

Run the MQTT Example shown in the video.

I used a Raspberry Pi Camera attached to my device for this example. At this point, you should be familiar with S3 and the MQTT portion if you watch the prerequisites linked in the description.

You should see a code of 0 which means your Raspberry Pi code is expecting a message to be published and waiting forever until you exit the Python Script.

If it receives a payload of "Yes" it will simply take a photo using the function capture() defined in the s3 code! This payload will be generated by the Node backend running on your local computer.

This code is explained in the s3 prerequisite.

You can modify the code if you don't want to use a camera, I just thought the camera was great functionality to build a simple application and we will be extending that in the next video. 

Step 2-) Node Setup

Now we simply have to run the Node project locally, it is a very simple project.

First, make sure you have npm installed on your computer, you most likely do, if not, you can google how to install it online for your system.

Next, create a Node project by creating a folder wherever you like and running init command in the folder directory.

  • npm init -y

I am in the folder raspberrypi-youtube-backend in the Terminal of VSCode where I run the npm init command.

Next, enter the folder in VSCode by opening it File > Open, you should see a package.json file which means you initialized successfully.

Now create an index.js file in the folder and copy the content from the code in the video. You can download the code on Razzl if you do not want to copy it in the video!

Also, npm install the three respective packages, mqtt, body-parser, and express, and then run the backend using the command.

  • node index.js

The project should be running on local host 3000

The provided Node.js code sets up a web server using the Express framework, allowing it to handle HTTP requests. It utilizes the 'body-parser' middleware for parsing JSON in request bodies and the 'mqtt' library to connect to an MQTT broker. The server listens on port 3000, and you can adjust this to your preferred port. The MQTT broker's address and channel for publishing messages are specified as 'mqtt://192.168.1.111' and 'your/command/channel, ' respectively. The server defines a route, '/takePhoto, ' which, upon receiving a GET request, publishes the message 'Yes' to the configured MQTT channel. A success response is then sent to the client. The server starts and runs on the specified port, and a graceful shutdown mechanism is implemented to end the MQTT client and exit the process when a SIGINT signal is received (e.g., when stopping the server using Ctrl+C). Adjustments to the MQTT settings are necessary based on your specific MQTT configuration.

You can now open a browser and hit the endpoint /takePhoto. You should see the logs pop up in your Raspberry Pi code that it received the message successfully!

Go to your s3 bucket and you can see the image/video you told the Raspberry Pi to take, pretty cool. This will form the backend for our full-stack application when we integrate React in the next part of the series. I hope you stay tuned for that.

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.