챗GPT로 코딩하기
[강의교안 이용 안내]
마이크로파이썬을 활용한 사물인터넷
Chapter
12
센서 데이터 모니터링하기
index
Creating a MicroPython Program
12.2
Creating a Node-RED Flow
12. 3
12.1
Building a circuit
3/27
Learning Objectives
4/27
12.1 Building a Circuit
1. Configuration using shields
12.1 Building a Circuit
purpose
Read the value of the temperature and humidity sensor and send it to the MQTT broker,
Display data on OLED and control external power with relay
shield composition element
6/27
2. Configuration using a breadboard
12.1 Building a Circuit
purpose
Circuit with breadboard and individual components without shield By composing
Implementing sensor data measurement and display
breadboard composition element
7/27
12.2 MicroPython �Create a program
MicroPython Create a program
12.2 MicroPython Create a program
Prompts and Code
Coding Generative AI
AI Prompt Practice 12-1
[ File name : ai-12-1-dht22-oled.md]
MicroPython code output by AI
Program 12-1
[ File name : 12_1_dht_oled.py]
Webpage
https://iotmaker.kr/mpy-ai-12-1/
9/27
MicroPython Create a program
12.2 MicroPython Create a program
execution result
connect_Wi-Fi> Starting Wi-Fi (s2020)... connect_Wi-Fi> Wi-Fi connection complete: ('192.168. 0.219', '255.255. 255. 0', '192.168. 0. 1', '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 Temperature: 24.9, Humidity: 46 pub> tele/ESP32-01/SENSOR, |{"TempUnit": "C", "DHT22": {"Humidity": 46, "Temperature": 24.9}}|, retain=False, qos=0 RCV> cmnd/ESP32-01/TELEPERIOD, |5| pub> stat/ESP32-01/TELEPERIOD, |5|, retain=True, qos=0 pub> tele/ESP32-01/INFO, |{"TELEPERIOD": 5}|, retain=True, qos=0 TELEPERIOD changed to 5 seconds. Temperature: 24.9, Humidity: 45 pub> tele/ESP32-01/SENSOR, |{"TempUnit": "C", "DHT22": {"Humidity": 45, "Temperature": 24.9}}|, retain=False, qos=0
10/27
MicroPython Create a program
12.2 MicroPython Create a program
execution result
11/27
MicroPython Create a program
12.2 MicroPython Create a program
program outline
Key Features
- Reads temperature and humidity data with a DHT22 sensor at 10-second intervals and publishes it to the MQTT broker in JSON format.
- Real-time monitoring is provided by displaying temperature and humidity alternately on OLED at 3-second intervals.
- Publish 'Online' topic when MQTT is connected, 'Offline' topic when disconnected
- Adjusts data transmission cycle (TELEPERIOD) and performs relay control according to commands received from MQTT broker.
System configuration
sensor measurement : Reading DHT22 sensor → Converting data with Parse module
display : Displaying temperature/humidity information on SSD1306 OLED (using the draw_hangul function)
network communication : Handling MQTT connections and callbacks with the MQTTClient and UseMQTTClient classes
Timer : Schedule OLED updates and sensor data transmission cycles with the TimerRun class.
12/27
MicroPython Create a program
12.2 MicroPython Create a program
program flow
Initialization phase
Create a Run object and a TimerRun instance
Initialize instances of peripherals such as DHT22, OLED, and relays and assign pins
I2C interface settings (for OLED)
MQTT setup and connection
After creating an MQTTClient instance, wrap it with UseMQTTClient and specify the callback function (mqtt_callback, mqtt_on_connect).
After connection, initial information such as TELEPERIOD, sensor data, relay status, etc. are issued.
Repeat execution
Registered by calling run.run() in the main loop Timer
(Send sensor data, update OLED) and keep MQTT tasks running
13/27
12.3 Node-RED �Flow making
Creating a Node-RED Flow
12.3 Creating a Node-RED Flow
outline
purpose
Receive DHT22 sensor data transmitted at 10-second intervals from MQTT broker and display it as a graph on the dashboard.
Additional features
component
15/27
1. Completed Dashboard look
12.3 Creating a Node-RED Flow
16/27
2. Completed Flow look
12.3 Creating a Node-RED Flow
Flow file : [ File name : flows-12-1-dht22-relay.json]
17/27
2. Completed Flow look
12.3 Creating a Node-RED Flow
18/27
3. Node Write
12.3 Creating a Node-RED Flow
In the radio group Select
19/27
3. Node Write
12.3 Creating a Node-RED Flow
context data How to use
- Flow context data is stored in node numbers 11), 13), 15), 27), 33) .
- The context data used in this flow is Next equivalence
Key context variables
20/27
3. Node Write
12.3 Creating a Node-RED Flow
link out and link in nodes
21/27
3. Node Write
12.3 Creating a Node-RED Flow
maximum Temperature and lower limit temperature Specify : number input
22/27
3. Node Write
12.3 Creating a Node-RED Flow
electric heater To operate : switch
23/27
3. Node Write
12.3 Creating a Node-RED Flow
At temperature according to Relay Control
24/27
3. Node Write
12.3 Creating a Node-RED Flow
graph Display : chart
25/27
4. Relay module How to use
12.3 Creating a Node-RED Flow
Relay basic principles
- The relay module turns ON when current flows through the contacts.
- A physical circuit configuration method applicable to both direct current and alternating current (also called dry contact)
- The driving power is 5V, and the control signal is 3.3V.
Control method
- When a HIGH signal comes out of the pin, the relay is activated and turns on.
- Current sinking mode modules can operate on LOW signals, so caution is required.
Terminal configuration
- The relay module has three screw terminals (NO, C, NC) .
1. NO (Normally Open): Not normally connected
2. C (Common): Common terminal
3. NC (Normally Closed): Normally connected
When connecting an external power source, C and NO terminals are generally used.
the relay is off, no current flows between NO and C, and only between C and NC.
26/27
Q&A
Copyright© 2025 Hanbit Academy, Inc.
All rights reserved.