챗GPT로 코딩하기
[강의교안 이용 안내]
마이크로파이썬을 활용한 사물인터넷
Chapter
11
Node-RED�서버 만들고 활용하기
index
Installing Node-RED
11.2
Node-RED Flow Edit
11. 3
Controlling MQTT
11.4
Dashboard making
11.5
11.1
Node-RED Overview
addition Node and expansion function
11.6
3/48
11.1 Node-RED Overview
1. What is NODE-RED ?
11.1 Node-RED Overview
What is Node-RED?
programming developed by IBM equipment
Visual Programming
To make it easy to visually connect IoT devices, APIs, and various services. Designed
Node-based configuration
A flow is formed by connecting nodes with lines, and each node is a group of programs that perform a specific function .
Scalability
there are many open source nodes, with approximately 5,100 nodes registered as of October 2024.
※ Since IoT systems generate data from multiple devices, it is necessary to display, share, and analyze this data in an easily viewable manner. box
The controller, which is the core of the IoT system, controls devices, links to external systems, and provides dashboard functions. Perform
Node- RED is optimized for implementing these controllers and has a highly scalable ecosystem .
5/48
2. Low-code and event- driven
11.1 Node-RED Overview
low-code development method
event Driven functioning method
6/48
2. Low-code and event- driven
11.1 Node-RED Overview
Operating environment
Internet browser
Using JavaScript to display dynamic information
Node.js
Chrome's JavaScript runtime engine
Node-RED
A platform that runs on Node.js
- If you install Node.js first and then install Node-RED, it will run on most operating systems. possible
- Node-RED editor and dashboard can be accessed and viewed through an internet browser.
- When using Node-RED for controlling IoT devices, Raspberry Pi is more power-efficient and cost-effective. From the side economic
- For your convenience, this book installs Mosquitto and Node-RED on Windows. Used
7/48
11.2 Installing Node-RED
Download and install Node.js
11.2 Installing Node-RED
Download
Download the LTS version from https://nodejs.org/
installation
Run the downloaded installation file
check
Check version information with node -v and npm -v in the command prompt.
9/48
Installing Node-RED
11.2 Installing Node-RED
Run Command Prompt
Command with administrator privileges prompt execution
Enter the installation command
npm install -g --unsafe-perm node-red
Confirm installation completion
When the installation process is completed successfully message Displayed
10/48
3. Node-RED Server Run
11.2 Installing Node-RED
Execute command
Type node-red at the command prompt
How to connect
Enter http://127.0.0.1:1880 or http://localhost:1880 in your browser.
caution
The running command prompt window is
Should not be closed
11/48
11.3 Installing Node-RED
Edit Node-RED screen
11.3 Installing Node-RED
edit screen composition element
palette
The node list area on the left, grouped by function
canvas
A centrally located workspace where you can place nodes and connect them with wires to configure your flow.
Sidebar
The area on the right provides node information, debug messages, help, etc.
13/48
2. First flow Write
11.3 Installing Node-RED
inject node
manually or periodically
debug node
Check by outputting msg data during flow test
Flow file: [File name: flows-11-1-inject-debug.json]
14/48
2. First flow Write
11.3 Installing Node-RED
inject node
Data output function
The inject node performs the function of outputting data.
Manual output
Manual printing is possible with the click of a button
Automatic output
Automatic output at set times and cycles is possible.
Various data formats
By default, it outputs the current time, but it can output various data formats depending on the options.
Can be printed
15/48
2. First flow Write
11.3 Installing Node-RED
inject node
16/48
2. First flow Write
11.3 Installing Node-RED
debug node function
17/48
11.4 Controlling MQTT
1. Run usemqttclient.py
11.4 Controlling MQTT
About this program
The module usemqttclient.py contains the class UseMQTTClient and the code for the main() function that can be used to test this class.
Here we are running this module alone to use the functionality of the main() function.
Node-RED flow runs in response to this program
Execution order of main() function
Modify config.py to your own in advance
19/48
1. Run usemqttclient.py
11.4 Controlling MQTT
config.py
DEVICE = 'ESP32-01' # The actual device name, used as part of the topic.
SSID = 'my-ssid' # Change to actual value
SSID_PASS = 'my-ssid-pass' # Change to actual value
MQTT_SERVER = '192.168.xxx.xxx' # Change to actual value
MQTT_PORT = 1883
MQTT_USER = 'SECOND_USER' # Value registered in pw.txt
MQTT_PASS = 'SECOND_PASSWORD' # Value registered in pw.txt
import binascii
import machine
""" Create a unique value using the device's MAC address """
MQTT_CLIENT_ID = binascii.hexlify(machine.unique_id()).decode()DEVICE = 'ESP32-01' # This is the actual device name and is used as part of the topic.
SSID = 'my-ssid' # Change to actual value
SSID_PASS = 'my-ssid-pass' # Change to actual value
MQTT_SERVER = '192.168.xxx.xxx' # Change to actual value
MQTT_PORT = 1883
MQTT_USER = 'SECOND_USER' # Value registered in pw.txt
MQTT_PASS = 'SECOND_PASSWORD' # Value registered in pw.txt
import binascii
import machine
""" Create a unique value using the device's MAC address """
MQTT_CLIENT_ID = binascii.hexlify(machine.unique_id()).decode()
20/48
1. Run usemqttclient.py
11.4 Controlling MQTT
Wi-Fi connection
Start Wi-Fi and complete connection
MQTT connection
Connecting to an MQTT server and setting up LWT
Data publication
Periodic release of sensor data
connect_Wi-Fi> Starting Wi-Fi (my-ssid)... connect_Wi-Fi> Starting Wi-Fi (your_ssid)... connect_Wi-Fi> Wi-Fi connection complete: ('192.168.24.207', '255.255.255.0', '192.168.24.177', '61.41.153.2') connect_mqtt> Starting MQTT connection LWT> tele/ESP32-01/LWT, |Offline|, retain=True connect_mqtt> MQTT connection complete pub> tele/ESP32-01/LWT, |Online|, retain=True, qos=0 sub> cmnd/ESP32-01/#, qos=0 on_connect() called pub> tele/ESP32-01/INFO, |{"TELEPERIOD": 10}|, retain=True, qos=0 pub> tele/ESP32-01/SENSOR, |{"ser": 1}|, retain=False, qos=0 pub> tele/ESP32-01/SENSOR, |{"ser": 2}|, retain=False, qos=0 pub> tele/ESP32-01/SENSOR, |{"ser": 3}|, retain=False, qos=0
21/48
2. Flow Write
11.4 Controlling MQTT
Receive topic
Receive 4 topics
Data verification
Output to debug node
Issue command
Issue commands with the inject node
Topic publication
Issue TELEPERIOD value
22/48
2. Flow Write
11.4 Controlling MQTT
mqtt -broker information Register
once at first Just register
MQTT in/out node Must be preceded when used box
Server Settings
Server address, port, user authentication information, etc. Set up
Security Settings
Username and password if required �By setting Security Reinforcement
23/48
2. Flow Write
11.4 Controlling MQTT
24/48
2. Flow Write
11.4 Controlling MQTT
mqtt in node Set up
About the mqtt server and topic to subscribe to information registration
You can also use wildcards (+,#) when specifying topics .
Example of topic settings
25/48
2. Flow Write
11.4 Controlling MQTT
debug node Set up
out the output value of a specific node
Select output destination
If you select 'Node Status (32 characters)' as the output target, it will be displayed under the corresponding node in the editing window.
Specify output format
To output the entire message or only specific properties
Can be set
Console output options
You can also set it to output to the system console .
26/48
2. Flow Write
11.4 Controlling MQTT
Specifying a payload in the inject node
to output a specific value method
27/48
2. Flow Write
11.4 Controlling MQTT
mqtt out node Set up
The server and topic to be published Designated
QoS and retained are also added designation possible
How to set a topic
Payload transfer
The value of msg.payload is the message content. Sent
28/48
2. Flow Write
11.4 Controlling MQTT
Click the inject node
The TELEPERIOD value has changed and is being passed to the device.
Device reception
Receive topic 'cmnd/ESP32-01/TELEPERIOD' and payload
Device handling
Change the sensor data release time
Results publication
Publish the command execution result to the topic 'stat/ESP32-01/TELEPERIOD'
Information Update
Issue TELEPERIOD value to topic 'tele/ESP32-01/INFO'
29/48
11.5 Creating a Dashboard
Basic Description
11.5 Creating a Dashboard
User interface
The dashboard is provided by Node-RED.
User interface
Visualization
The execution results and process of the flow
Display on a separate screen
Separate installation
for Node-RED dashboards are Separately
Need to install
Latest version
The latest dashboard version is
'@flowfuse/node-red-dashboard'
31/48
1. On the palette Dashboard Install
11.5 Creating a Dashboard
Open menu
Menu in the top right corner Button Click
Select Palette Management
Select [ Manage Palettes ] from the drop-down menu
Node search
In the search box
Enter '@flowfuse/node-red-dashboard '
Installation complete
installation button Click to complete installation
32/48
2. Dashboard Node Add
11.5 Creating a Dashboard
Widget
Individual dashboard nodes
Group
A collection of related widgets
Page
Screen containing multiple groups
- In order to use the dashboard node, [ui-base], [ui-page], and [ui-group] nodes must be created first. box
- These nodes can be set when adding a dashboard node for the first time.
- When adding a dashboard node later , you can select and use the already set [Page] and [Group] nodes .
- Additional [Page] and [Group] nodes as needed Can be created
- Dashboard When accessing, use a URL in the format http://localhost:1880/dashboard/page1
33/48
2. Dashboard Node Add
11.5 Creating a Dashboard
1
[Figure 11-29]
In 'Edit text node', 'Add node type of ui-group...'
2
[Figure 11-30]
In 'Add ui-group settings node', 'Add ui-page node type...'
3
[Figure 11-31]
In 'Add ui-page settings node', 'Add ui-base node type...'
4
[Figure 11-32]
In 'Add ui-base setting node', enter the name ('UI name') and
Specify Path('/dashboard')
5
[Figure 11-33]
In 'Add ui-page settings node', name ('Page 1')
Specify Path('/page1')
6
[Figure 11-34]
In 'Add ui-group settings node', specify the name ('group 1') and size.
7
[Figure 11-35]
In 'Edit Node of Text', select Group and give it a name ('LWT')
Specify Label('Device Connection')
34/48
3. Dashboard Flow Complete
11.5 Creating a Dashboard
Node-RED Flow
[ File name : flows-11-3-mqtt-dashboard.json]
receives MQTT messages
On the dashboard Displaying Function Implemented
35/48
3. Dashboard Flow Complete
11.5 Creating a Dashboard
text node Set up
36/48
3. Dashboard Flow Complete
11.5 Creating a Dashboard
switch node Set up
37/48
3. Dashboard Flow Complete
11.5 Creating a Dashboard
change node Set up
38/48
3. Dashboard Flow Complete
11.5 Creating a Dashboard
dropdown node Set up
39/48
3. Dashboard Flow Complete
11.5 Creating a Dashboard
Dashboard layout Adjust
40/48
11.6 Additional Nodes and Extensions
1. Context data
11.6 Additional Nodes and Extensions
Node-RED can utilize a wide range of context data .
42/48
2. JSONata
11.6 Additional Nodes and Extensions
An expression language that allows you to query, transform, and filter JSON data.
Can be used for data processing in change nodes, switch nodes, etc.
// Example: Extract only temperature values
payload.readings.temperature
43/48
3. function node
11.6 Additional Nodes and Extensions
function node Through JavaScript code By writing Advanced logic avatar
// Example : Temperature conversion function
var temp = msg.payload.temp ;
msg.payload = (temp * 9/5) + 32;
return msg ;
44/48
4. Node- RED Python Connect
11.6 Additional Nodes and Extensions
Connecting Python to Node-RED
node-red- contrib -python-function -ps : This node's internal code can be written in Python.
Running a Python shell
node-red- contrib - pythonshel l : A node that executes a separately created Python program and returns the result.
45/48
5. Flow Import / Export
11.6 Additional Nodes and Extensions
Save your flow in JSON format and export it to the clipboard or file.
46/48
Practice Problems (1/2 )
Practice configuring Node-RED flows and dashboards with the following exercises.
Problem Overview
Based on the flow in [Figure 11-38] and the dashboard in [Figure 11-39], we build a system that processes virtual sensor data.
File name: flows-11-3-mqtt-dashboard.json → flowsexam-11-1-dashboard.json
Sensor data configuration
{
"DHT22": {
"Temperature": any number,
"Humidity": any number
}
}
Dashboard implementation
Receive topic 'tele/ESP-01/SENSOR', separate temperature and humidity values, and display them in the gauge node of the dashboard.
Configure the flow shown in Figure 11-47 and complete the dashboard as shown in Figure 11-48.
47/48
Q&A
Copyright© 2025 Hanbit Academy, Inc.
All rights reserved.