챗GPT로 코딩하기
[강의교안 이용 안내]
마이크로파이썬을 활용한 사물인터넷
Chapter
13
전등 제어와�경보 시스템 구성하기
index
Creating a MicroPython Program
13.2
Creating a Node-RED Flow
13. 3
13.1
Building a circuit
3/26
Learning Objectives
4/26
13.1 Building a Circuit
1. Using a shield composition
13.1 Building a Circuit
purpose
To implement the lighting control and alarm system, the board is configured based on the shield. sensors and actuators at once
Shield components
Things to consider when configuring a circuit
Each shield has a fixed board and pin layout, so the connection order and pin duplication problem (e.g. I2C pin sharing between OLED and BH1750)
6/26
2. Configuration using a breadboard
13.1 Building a Circuit
Alternative configuration
How to build a circuit using a breadboard and individual components without a shield
breadboard component
Microcontroller: S3 mini, S2 mini, or C3 mini series boards
Sensor:
Output device:
Additional components: breadboard, USB-C cable, etc.
Considerations when connecting
OLED and BH1750 use I2C communication, so be careful about pin sharing.
The PIR sensor and MQ-9 sensor have their power and signal pin connections configured precisely.
7/26
13.2 MicroPython �Create a program
MicroPython Create a program
13.2 MicroPython Create a program
Prompts and Code
Coding Generative AI
AI prompt Exercise 13-1
[ File name : ai-13-1-pir-gas.md]
MicroPython code output by AI
Program 13-1
[ File name : 13_1_pir_gas.py]
Webpage
https://iotmaker.kr/mpy-ai-13-1/
9/26
MicroPython Create a program
13.2 MicroPython Create a program
execution result
connect_Wi -Fi> Start Wi-Fi (s2020)...
connect_Wi -Fi> Wi-Fi connection Completed : ('192.168.0.219', '255.255.255.0', '192.168.0.1', '61.41.153.2')
connect_mqtt > MQTT connection start
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": 5}|, retain=True, qos =0
on_connect_more ()...
Brightness: 471
gas: 56
pub> tele/ESP32-01/SENSOR, |{"BH1750": {"Brightness": 471}, "gas": {"value": 56}}|, retain= False, qos =0
RCV> cmnd /ESP32-01/TELEPERIOD, |10|
pub> stat/ESP32-01/TELEPERIOD, |10|, retain=True, qos =0
pub> tele/ESP32-01/INFO, |{"TELEPERIOD": "10"}|, retain=True, qos =0
TELEPERIOD has been changed to 10 seconds .
pub> tele/ESP32-01/PIR, |on|, retain=True, qos =0
PIR on
RCV> cmnd /ESP32-01/LED_1, |on|
pub> stat/ESP32-01/LED_1, |on|, retain=False, qos =0
pub> tele/ESP32-01/PIR, |off|, retain=True, qos =0
PIR off
10/26
MicroPython Create a program
13.2 MicroPython Create a program
program outline
Sensor data measurement
Data processing and transmission
OLED display update
The gas value is displayed on the OLED with a title such as “Gas Illuminance” at 3-second intervals.
threshold and illuminance values
Automatic control function
11/26
MicroPython Create a program
13.2 MicroPython Create a program
program flow
Initialization and setup steps
Object creation: Create a Run object, initialize instances of each sensor (gas, BH1750, PIR) and actuator (LED, buzzer).
I2C Interface: Setting up the I2C bus for connecting the OLED and BH1750 sensors.
OLED Initialization: Initialize the OLED display, set the screen size, and set the resolution using the SSD1306 library.
Register a timer
timer_oled: Calls the display_oled function every 3 seconds to refresh the OLED screen.
timer_sensor: Reads sensor data at a default TELEPERIOD (e.g. 5 seconds) interval and sends it to the MQTT broker.
MQTT connection and event handling
After creating MQTTClient, wrap it with UseMQTTClient and set the mqtt_callback and mqtt_on_connect functions.
mqtt_on_connect: After connection, initial information such as TELEPERIOD (MQTT INFO message) is issued and additional tasks (on_connect_more) are executed.
mqtt_callback: Change TELEPERIOD according to received topic, control LED ,
Processing THRESHOLD (gas threshold) update, etc.
Sensor data transmission
read_sensors_more: Read BH1750 and MQ-9 sensor values into dictionary
Data organization
Convert the configured data into a JSON string
Publish to MQTT broker
12/26
13.3 Node-RED �Flow making
Creating a Node-RED Flow
13.3 Creating a Node-RED Flow
outline
Purpose and Role
Core Features
component
14/26
1. Completed Dashboard look
13.3 Creating a Node-RED Flow
15/26
2. Completed Flow look
13.3 Creating a Node-RED Flow
Flow File : [ File name : flows-13-1-pir-gas.json]
16/26
2. Completed Flow look
13.3 Creating a Node-RED Flow
17/26
2. Completed Flow look
13.3 Creating a Node-RED Flow
18/26
3. Node Write
13.3 Creating a Node-RED Flow
sensor The value On the dashboard Expressing
19/26
3. Node Write
13.3 Creating a Node-RED Flow
sensor information Subscribe : mqtt in
20/26
3. Node Write
13.3 Creating a Node-RED Flow
Control lights based on PIR status and light levels
A typical example is to implement automation by comparing sensor information with pre-determined thresholds. Flow
Motion detection and light measurement
Detects movement with PIR sensor and ambient brightness with light sensor Measure
Automatic control logic
system that turns on the lights only when motion is detected and the surroundings are dark. Implemented
21/26
3. Node Write
13.3 Creating a Node-RED Flow
Control lights based on PIR status and light levels
p3)on,off
Divide it into on or off depending on the output value of the 'p1)tele/ESP32-01/PIR' node. Branch
p4)If it's dark
When the PIR value is on, the flow to this node Connected
illuminance value (flow.light) is less than the lighting threshold (flow.light_threshold) ( if it is dark ) , go to port 1 . Branch and turn on LED_1 If the illuminance value is higher than the lighting threshold, nothing happens.
Control execution
to the MQTT broker to turn the LED on or off based on conditions. Sent
22/26
3. Node Write
13.3 Creating a Node-RED Flow
trigger Node How to use
23/26
3. Node Write
13.3 Creating a Node-RED Flow
Voice alarm based on gas concentration
g7), g8) gas.value
The value of the gas sensor is retrieved through the switch node 'g7)gas.value' and the change node 'g8)gas.value'. Extract
g11) If the concentration is high
In this node, a conditional expression is used to check whether there is a gas leak or not. Judgement
24/26
3. Node Write
13.3 Creating a Node-RED Flow
Alarm By voice Send
25/26
Q&A
Copyright© 2025 Hanbit Academy, Inc.
All rights reserved.