Skip to content

Raspberry Pi MQTT: Send Commands with Node.js | ShillehTek

November 23, 2023

Video Tutorial

Watch first if you want to follow along with the full walkthrough, including prerequisites.

Prerequisite (Part 1):

S3 + Raspberry Pi video:

Project Overview

Raspberry Pi + MQTT + Node.js: In this project, you use a Raspberry Pi with MQTT and a small Node.js server to send local commands that trigger an action (example: publishing "Yes" to make the Pi capture a photo).

The Raspberry Pi runs a Python MQTT client that waits for messages. Your computer runs a Node backend (Express) that publishes a payload to your MQTT topic when you hit an HTTP endpoint in a browser.

Optional resources from the original post: subscribe on Youtube and support at https://www.buymeacoffee.com/mmshilleh. Interactive version and downloads: https://share.razzl.com/fftd.

  • Time: 20 to 45 minutes (assuming prerequisites are done)
  • Skill level: Intermediate
  • What you will build: A local HTTP endpoint (Node.js) that publishes an MQTT command a Raspberry Pi listens to, triggering an action such as taking a photo

Parts List

From ShillehTek

  • No ShillehTek product links were provided in the original post.

External

  • Raspberry Pi running Python (example uses a Raspberry Pi Camera for the action)
  • MQTT broker reachable on your network (example broker URL in the description: mqtt://192.168.1.111)
  • Computer with Node.js and npm installed
  • Node packages: mqtt, body-parser, express
  • Web browser to call the backend endpoint (example: /takePhoto)
  • Optional: AWS S3 bucket (used in the referenced prerequisite to store captured media)

Note: You must update the MQTT broker address and the publish topic/channel to match your own MQTT setup.

Step-by-Step Guide

Step 1 - Run the Raspberry Pi MQTT script

Goal: Start the Raspberry Pi code so it subscribes to your topic and waits for commands.

What to do: Run the MQTT example shown in the video on your Raspberry Pi. In the example, a Raspberry Pi Camera is attached, and the code waits indefinitely for a published message.

When the script is running, you should see a code of 0, indicating the Raspberry Pi is waiting for a message until you exit the Python script.

If the Raspberry Pi receives a payload of "Yes", it triggers the camera action using the capture() function defined in the related S3 code (covered in the prerequisite video).

Expected result: The Raspberry Pi script stays running and logs activity only when a message is received.

Step 2 - Initialize a Node.js project

Goal: Create a simple local Node project that will publish MQTT messages.

What to do: Create a folder for your backend (example from the post: raspberrypi-youtube-backend), open a terminal in that folder, and initialize npm.

Code:

npm init -y

Open the folder in VSCode (File > Open). If you see package.json, your project initialized successfully.

Expected result: A Node project exists with a package.json file in the folder.

Step 3 - Add the Node backend and required packages

Goal: Run an Express server that publishes "Yes" to your MQTT topic when you hit an HTTP route.

What to do: Create an index.js file and copy the Node.js code from the video (or download it from the Razzl link: https://share.razzl.com/fftd).

The Node.js code uses Express to run a web server, body-parser to parse JSON bodies, and mqtt to connect to your MQTT broker. In the example description, the MQTT broker is set to mqtt://192.168.1.111 and publishes to a configured command channel.

Install the three packages (mqtt, body-parser, express) as shown in the video, then run the backend.

Code:

node index.js

The project should be running on localhost:3000 (or whatever port you configured).

Expected result: Your Node server starts successfully and stays running, ready to publish MQTT messages when the route is called.

Step 4 - Trigger the command from a browser

Goal: Send an HTTP request that causes the Node backend to publish an MQTT message the Raspberry Pi receives.

What to do: Open a browser and hit the endpoint /takePhoto on your Node server (as defined in the provided Node.js code).

When that endpoint is called, the backend publishes the message "Yes" to your configured MQTT channel. The Raspberry Pi code should log that it received the message successfully.

If you are using the S3 workflow from the prerequisite, check your S3 bucket to see the image or video captured by the Raspberry Pi.

Expected result: The Raspberry Pi logs the received MQTT payload, and the action (example: capture) runs.

Conclusion

You now have a simple command pipeline where a Raspberry Pi listens on MQTT, and a local Node.js (Express) endpoint publishes a payload like "Yes" to trigger an action such as taking a photo.

Want the parts and modules for your next Raspberry Pi and IoT builds? Grab what you need from ShillehTek.com. If you want help tailoring this MQTT workflow for a real product or custom automation, check out our IoT consulting services.