Practical 8 - MQTT and Node-Red
Practical 8 - MQTT and Node-Red
Objective
Understand the MQTT protocol, establish the MQTT connection using Raspberry Pi and
NodeMCU ESP32.
Understand the Node-Red platform and create an Internet of Things solution using flow
architecture design and dashboard interface.
Procedure
Part 1: Establish MQTT Broker and Client (Subscriber and Publisher) using Raspberry Pi
We use Paho MQTT, that is a simple library to implement message communication using MQTT
protocol, pre-installed in our Raspberry Pi module
Unset
allow_anonymous true
listener 1883
Then press Ctrl + S to save, and Ctrl + X to exit the nano editor.
Reboot the Raspberry Pi.
Step 2: Start the mosquitto broker automatically by typing this command in the terminal:
● Test your MQTT services by open two terminals and type the following command:
Terminal A as subscriber:
○ mosquitto_sub -h [YOUR IF ADDRESS] -t 'test/topic'
Terminal B as publisher:
○ mosquitto_pub -h [YOUR IF ADDRESS] -t 'test/topic' -m 'helloWorld'
Step 4: In Thonny Python (ID), click “New” to create a new python file and Save As “test09.py”
and “test10.py”. Type the following codes for subscriber and publisher:
BAIT2123 INTERNET OF THINGS Jul 2024
For Raspbian Bookworm with Grove Base Hat, use the following code for your “test10.py”:
BAIT2123 INTERNET OF THINGS Jul 2024
Task 1: Modify the code to cross control other sensors / devices with another
raspberry pi.
* We can use the following MQTT Online Client to test your MQTT communication:
http://mqtt-client.emqx.com/, https://testclient-cloud.mqtt.cool/
Part 2: Establish MQTT Client (Subscriber and Publisher) using NodeMCU ESP32
Step 3: Upload the compiled code to NodeMCU ESP32 Module and observe the result.
- Click Sketch > Verify / Compile and Sketch > Upload to compile this modified code and
upload to the NodeMCU ESP32 module.
- Check if the mqtt status is received when the mqtt message “led=1” is sent.
Task 1 : Modify the code with more status definition for publishing DHT values (and other
sensors value)
Task 2 : Modify the code with more status definition for executing the LED on / off, to
allow remote controlling.
BAIT2123 INTERNET OF THINGS Jul 2024
Step 1: Refer to the reference (only for newly installed Raspbian OS):
* The default Raspberry Pi microSD card is already installed with the Node-Red
Framework, go to Step 2:
https://nodered.org/docs/getting-started/raspberrypi
Installing Node-Red in Raspberry Pi with the following command:
bash <(curl -sL
https://raw.githubusercontent.com/node-red/linux-installers/master/deb/up
date-nodejs-and-nodered)
Step 3: While Node-RED is running, turn on your browser with the localhost:1880, you
will see the Node-RED interface.
Step 4: Insert an example flow using simple input and output nodes
- We can use an inject node as an input node, to allow us to inject messages into a flow,
either by clicking the button on the node, or setting a time interval between injects.
- Drag one onto the workspace from the palette. Select the newly added Inject node to
see information about its properties and a description of what it does in the Information
sidebar pane.
- The Debug node is utilized as an output node, causing any message to be displayed in
the Debug sidebar.
BAIT2123 INTERNET OF THINGS Jul 2024
- By default, it just displays the payload of the message, but it is possible to display the
entire message object. Connect the Inject and Debug nodes together by dragging
between the output port of one to the input port of the other.
- At this point, the nodes only exist in the editor and must be deployed to the server. Click
the Deploy button.
- With the Debug sidebar tab selected, click the Inject button (the small square button next
to our inject node). We should see numbers appear in the sidebar. By default, the Inject
node uses the number of milliseconds since January 1st, 1970 as its payload.
- Double click the inserted Button node, Add a new dashboard group with the name
“Input” under the new dashboard tab “Example”. Do the same setting on the inserted
Text node, with the name as “Output”, under the same dashboard tab.
- Click the Deploy button. Launch a new web browser tab, with the URL:
http://127.0.0.1:1880/ui
- A simple Dashboard interface is created, with a button (click it) as an input and the
output displays the text with time information.
BAIT2123 INTERNET OF THINGS Jul 2024
Task 1: Modify the Node-Red flow diagram to insert MQTT input and output nodes.
Configure the necessary MQTT broker connection, topics and mqtt_command for further
execution.
Task 2: Add a dashboard LED or Switch nodes (to act as Push Button), trigger
mqtt_command by toggling the Switch nodes in the Node-Red dashboard.
Task 3: Extend the previous work by uploading the DHT values via MQTT command, and
display in the Chart Node in Node-Red dashboard.