Connect Raspberry Pi to a Node App: Sending Messages & Commands to Your Raspberry Pi Using MQTT (P2)
Welcome to part 2 of our series on connecting your Raspberry Pi to a Node.js app using MQTT. In this article, we will focus on sending messages and commands to your Raspberry Pi from a Node.js app using MQTT.
Setting up the Node.js app
First, make sure you have Node.js installed on your computer. If not, you can download it from the official website and follow the installation instructions.
Next, create a new directory for your Node.js app and navigate to it using the command line. Then, run the following command to initialize a new Node.js project:
npm init -y
This will create a package.json
file for your project.
Now, you need to install the MQTT.js library, which will allow you to connect to the MQTT broker and send messages to your Raspberry Pi. Run the following command to install it:
npm install mqtt
Next, create a new JavaScript file (e.g. app.js
) in your project directory and write the following code to connect to the MQTT broker and send a message to your Raspberry Pi:
// Import the MQTT library
const mqtt = require('mqtt');
// Connect to the MQTT broker
const client = mqtt.connect('mqtt://');
// When connected, publish a message to the topic 'rpi/commands'
client.on('connect', () => {
client.publish('rpi/commands', 'Hello from Node.js!');
});
Replace <your-broker-ip>
with the IP address of your MQTT broker (e.g. your Raspberry Pi).
Receiving messages on the Raspberry Pi
Now that you have set up the Node.js app to send messages to your Raspberry Pi, you need to create a script on the Raspberry Pi to receive these messages and execute commands based on them.
Assuming you have already set up an MQTT broker on your Raspberry Pi (as described in part 1 of this series), create a new Python script (e.g. mqtt_receiver.py
) and write the following code to subscribe to the rpi/commands
topic and execute commands:
import paho.mqtt.client as mqtt
import os
# Callback function for when the client receives a message
def on_message(client, userdata, message):
command = message.payload.decode('utf-8')
# Execute the received command (e.g. printing the message)
print(command)
# Connect to the MQTT broker
client = mqtt.Client()
client.connect('', 1883, 60)
# Subscribe to the topic 'rpi/commands'
client.subscribe('rpi/commands')
# Set the callback function for when a message is received
client.on_message = on_message
# Keep the script running to receive messages
client.loop_forever()
Replace <your-broker-ip>
with the IP address of your Raspberry Pi.
Conclusion
Congratulations! You have now set up your Node.js app to send messages and commands to your Raspberry Pi using MQTT. You can now build on this foundation to create more complex and interactive applications that communicate with your Raspberry Pi in real-time. Stay tuned for the next article in this series, where we will explore receiving messages and data from the Raspberry Pi in our Node.js app.
Thank you. Is that an orchid plant?
Excellent! I would like to store the RPi picture in my Google Drive, OneDrive, DropBox, or local computer. I've not had much success with S3 , seems overly complicated to set up- I gave up. Please consider a tutorial setting up MQTT broker with websockets enabled. Couldn't you send the picture direct to your laptop/desktop instead of S3? Thanks for your useful content.