[go: up one dir, main page]

0% found this document useful (0 votes)
74 views14 pages

Practical 8 - MQTT and Node-Red

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views14 pages

Practical 8 - MQTT and Node-Red

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

BAIT2123 INTERNET OF THINGS Jul 2024

Practical 8 - MQTT, 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

MQTT = Message Query Telemetry Transport

We use Paho MQTT, that is a simple library to implement message communication using MQTT
protocol, pre-installed in our Raspberry Pi module

Step 1: Reference: http://mosquitto.org/


* check if you have install mosquitto and MQTT by typing:
dpkg -l mosquitto mosquitto-clients
* If you are using your own Raspberry Pi, do install mosquitto and MQTT by typing:
sudo apt-get install -y mosquitto mosquitto-clients
sudo pip3 install paho-mqtt
in Raspbian Bookworms, use
sudo apt-get install -y mosquitto mosquitto-clients
sudo pip3 install paho-mqtt --break-system-packages
* For newly installed mosquitto library and mqtt library, edit the mosquitto.conf by
inserting the following lines at the last line of the file, to ensure all the Pi’s connected IP
addresses can be utilized as broker IP.
sudo nano /etc/mosquitto/mosquitto.conf
At last line of the file, insert:
BAIT2123 INTERNET OF THINGS Jul 2024

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:

sudo systemctl enable mosquitto.service

Step 3: Typing this command in the terminal:

● Check your own IP address by typing ifconfig in 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

Step 5: Check your raspberry pi IP address by typing this in the terminal:


hostname -I
MQTT by default is installed as a broker in each raspberry pi. Replace Broker IP with
your raspberry pi IP.

Step 6: Run both codes to ensure the publish code

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 1: In Arduino IDE, install the PubSubClient Library:


- Tools > Manage Libraries... >
Search for "PubSubClient"
Select PubSubClient by Nick O'Leary and Click Install

Step 2: Modify the default *.ino code to the following codes:


BAIT2123 INTERNET OF THINGS Jul 2024
BAIT2123 INTERNET OF THINGS Jul 2024
BAIT2123 INTERNET OF THINGS Jul 2024
BAIT2123 INTERNET OF THINGS Jul 2024
BAIT2123 INTERNET OF THINGS Jul 2024

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

Part 3: Using NodeRed software to boost your IoT project development

Installation in Linux Distribution (e.g, Debian / Raspbian)


* The default Raspberry Pi microSD card is already installed with the Node-Red framework.

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 2: After installed Node-Red, type Node-red-start in the terminal


BAIT2123 INTERNET OF THINGS Jul 2024

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.

Step 5: Add in a function node in the example flow


- The Function node can be utilized as a processing node, allowing us to pass each
message through a JavaScript function.
- Delete the existing wire (select it and press delete on the keyboard).
- Wire a Function node in between the Inject and Debug nodes.
- Double-click on the Function node to bring up the edit dialog. Copy the following code
into the function field:
// Create a Date object from the payload
var date = new Date(msg.payload);
// Change the payload to be a formatted Date string
msg.payload = date.toString();
// Return the message so it can be sent on
return msg;
- Click Done to close the edit dialog and then click the deploy button.
- Now when we click the Inject button, the messages in the sidebar will now be formatted
as readable timestamps.
BAIT2123 INTERNET OF THINGS Jul 2024

Step 6: Set up a Node-Red dashboard


- For the new Raspbian OS, install the Node-RED Interfaces, on top of the Node-RED
installation. Refer to : https://flows.nodered.org/node/node-red-dashboard
- In the Node-RED interface (flow), look for the Dashboard nodes from the node list.
Select and drag the “button” and “text” to the flow interface. Link them with the function
accordingly (to act as an inject node and debug node).

- 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.

You might also like