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

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.