1 of 27

챗GPT로 코딩하기

[강의교안 이용 안내]

  • 본 강의교안의 저작권은 양재삼한빛아카데미㈜에 있습니다.
  • 이 자료를 무단으로 전제하거나 배포할 경우 저작권법 136조에 의거하여 벌금에 처할 수 있고 �이를 병과(倂科)할 수도 있습니다.

마이크로파이썬을 활용한 사물인터넷

2 of 27

Chapter

12

센서 데이터 모니터링하기

3 of 27

index

Creating a MicroPython Program

12.2

Creating a Node-RED Flow

12. 3

12.1

Building a circuit

3/27

4 of 27

Learning Objectives

  • DHT22 sensor conjugation : Periodically reads temperature and humidity and sends them to the MQTT broker. Learn how .
  • OLED display control : Displays letters and numbers, and alternates between multiple screens of information . Learn how .
  • Relay control : Automatically controls the relay according to the set temperature conditions Understand the method .
  • System Integration : Integrates sensor data measurement, display update, and network communication into one program. Learn how .

4/27

5 of 27

12.1 Building a Circuit

6 of 27

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

  • board : S3 mini or S2 mini (microcontroller)
  • sensor : Measuring temperature and humidity using the DHT22 shield
  • display : Displaying temperature and humidity information using an OLED shield
  • control : Power control using relay shield
  • connection : Connect the board to the PC using a USB-C cable

6/27

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

  • Board : Use S3 mini, S2 mini or C3 mini
  • Sensor : Measure temperature and humidity using the DHT22 sensor
  • Display : OLED display (SSD1306, I2C interface)
  • Control : Use relay module (use another pin (L6) to avoid duplication of D1 and SCL pins of relay shield)
  • Additional parts : breadboard, 4.7kΩ resistor (for pull-up)

7/27

8 of 27

12.2 MicroPython �Create a program

9 of 27

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

10 of 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

11 of 27

MicroPython Create a program

12.2 MicroPython Create a program

execution result​

11/27

12 of 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

13 of 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

14 of 27

12.3 Node-RED �Flow making

15 of 27

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

  • Display real-time charts and text information on your dashboard based on temperature and humidity data.
  • Heater (relay) control: Implementation of temperature-based automatic operation function (comparison of lower and upper temperature limits)
  • Control operation by sending a command to the device to change the data reception interval (TELEPERIOD).

component

  • MQTT in/ MQTT out node: Sends and receives sensor data and command data.
  • Dashboard node: Configure the user interface using various widgets such as charts, text, radio groups, number inputs, and switches.
  • Flow context data: Management of setting information such as automatic driving mode, temperature criteria (lower/upper limits), etc.

15/27

16 of 27

1. Completed Dashboard look

12.3 Creating a Node-RED Flow

16/27

17 of 27

2. Completed Flow look

12.3 Creating a Node-RED Flow

Flow file : [ File name : flows-12-1-dht22-relay.json]

17/27

18 of 27

2. Completed Flow look

12.3 Creating a Node-RED Flow

18/27

19 of 27

3. Node Write

12.3 Creating a Node-RED Flow

In the radio group Select

  1. name displayed in the dashboard editing window. designation
  1. [Group] : Dashboard group
  1. Displayed on the dashboard Text
  1. returned via msg.payload. Passed to node
  1. Displayed in the drop-down list Text
  1. [+option] : Drop-down list items addition
  1. you must select it from the options in the dashboard. Printed​
  1. Click the button setting Finally

19/27

20 of 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

  • flow.operation : 'auto' and 'manual' for automatic and manual driving. Show​
  • flow.temp_floor : lower limit temperature
  • flow.temp_ceil : upper temperature limit
  • flow.temperature : received temperature

20/27

21 of 27

3. Node Write

12.3 Creating a Node-RED Flow

link out and link in nodes

  1. name displayed in the dashboard editing window. designation
  1. the node to link in Select ( select multiple ) number There is )
  1. Click the button setting Finally

21/27

22 of 27

3. Node Write

12.3 Creating a Node-RED Flow

maximum Temperature and lower limit temperature Specify : number input

  1. Displayed on the dashboard Text
  1. [min]: minimum value
  1. [max]: maximum value
  1. The number to be increased or decreased when specifying using a spinner
  1. [Input Data Pass]: Messages arriving at the input port are displayed on the dashboard. Printed
  1. If you lose focus while entering numbers directly, Entered​
  1. [Press Enter]: When you input numbers directly, press the Enter key. Entered​
  1. Click the button to finish the setup

22/27

23 of 27

3. Node Write

12.3 Creating a Node-RED Flow

electric heater To operate : switch

  1. name displayed in the dashboard editing window. designation
  1. [Group] : Dashboard group
  1. Displayed on the dashboard Text
  1. [Input data pass]: The message that arrived at the input port did not pass. No
  1. Click the button setting Finally

23/27

24 of 27

3. Node Write

12.3 Creating a Node-RED Flow

At temperature according to Relay Control

  1. 19) Repeat every 5 seconds (inject) : Repeat the automation flow according to the time interval. Start​
  1. link in 1(link in) : The automation flow is resumed even when the relay setting information changes or new temperature data arrives. Start​
  1. 20) flow.operation==auto(switch) : Switch to the subsequent node only when flow.operation, which is the flow context data, is auto. Branch​
  1. 21)low,high(switch) : Compare flow.temperature with flow.temp_floor(lower temperature) and flow.temp_ceil(upper temperature). Branch​
  1. 22)on : If the current temperature is lower than the lower limit temperature, send ' on' to msg.payload .
  1. 22)off : If the current temperature is higher than the upper limit temperature, send 'off' to msg.payload.

24/27

25 of 27

3. Node Write

12.3 Creating a Node-RED Flow

graph Display : chart

  1. name displayed in the dashboard editing window. designation
  1. [Group] : Dashboard group
  1. Displayed on the dashboard Text
  1. Select a chart format from 6 options .
  1. [Point Style]: The shape of the data display points. select
  1. [X-Axis Limit]: Data limit on the X-axis mark
  1. [min]: Y-axis value Minimum
  1. [max]: Y-axis value Maximum value
  1. Click the button setting Finally

25/27

26 of 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

27 of 27

Q&A

Copyright© 2025 Hanbit Academy, Inc.

All rights reserved.