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.

Back to blog

Leave a comment

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