Prototype Practice - sunbang123/Graduation_Project GitHub Wiki

image

ํ™ˆ์–ด์‹œ์Šคํ„ดํŠธ์—์„œ ์ž๋™ํ™”์‚ฌ์šฉ!!!


image ์ €์žฅ๊ณต๊ฐ„ํ™•์ธ

Prototype Practice

๐Ÿ”ง ํ”„๋กœํ† ํƒ€์ž… ๊ตฌํ˜„

  • Raspberry Pi ์‹ค์Šต ๊ตฌํ˜„
  • Node-RED ํ”Œ๋กœ์šฐ ๊ตฌํ˜„
  • Home Assistant ์—ฐ๋™

๋ชจ์…˜ ์„ผ์„œ ์‹ค์Šต

๊ธฐ๋Šฅ ์ž‘๋™ ๋ฐฉ์‹

  • PIR(Passice Infrared) ์ธ์ฒด ๊ฐ์ง€ ์ ์™ธ์„  ์„ผ์„œ.
  • ์ ์™ธ์„ ์„ ๊ฐ์ง€ํ•˜์—ฌ ์›€์ง์ž„์„ ๊ฐ์ง€ํ•จ. ์—๋„ˆ์ง€ ์ ˆ์•ฝ ์กฐ๋ช… (ex) ์ผ๋ฐ˜ ๊ฑด๋ฌผ์˜ ๋ณต๋„ ์กฐ๋ช…

๊ตฌ์กฐ๋„

์˜ˆ์ œ ์ฝ”๋“œ

from gpiozero import MotionSensor, LED

pir=MotionSensor(18) # PIR ์—ฐ๊ฒฐ ํ•€ INPUT์œผ๋กœ ์ง€์ •: GPIO 18
led=LED(15) # LED ์—ฐ๊ฒฐ ํ•€ OUTPUT์œผ๋กœ ์ง€์ • : GPIO 15

# ๋ฌดํ•œ๋ฐ˜๋ณต
while True:
 if pir.value == 1: # ์„ผ์„œ์— ์ธ์‹๋˜์—ˆ์„ ๋•Œ LED ON
  led.on()
  print("motion detect")
 else: # ์„ผ์„œ์— ์ธ์‹๋˜์ง€ ์•Š์•˜์„ ๋•Œ LED OFF
  led.off()
  print("no motion")
  • ์›€์ง์ž„์ด ๊ฐ์ง€๋˜๋ฉด LED๊ฐ€ ๋™์ž‘ํ•˜๊ณ  ์„ค์ •ํ•œ ์ผ์ • ์‹œ๊ฐ„ ๋’ค์— ๊บผ์ง. ๋™์ž‘ ์‹œ๊ฐ„์€ ์ฝ”๋“œ ๋˜๋Š” ๋ชจ๋“ˆ ์ €ํ•ญ๊ธฐ๋ฅผ ํ†ตํ•ด ์„ค์ •

์‹ค์Šต ์žฅ์น˜

  • ๊ตฌ์กฐ๋„ ๊ทธ๋ฆผ๊ณผ ๋˜‘๊ฐ™์€ ๋ชจ์…˜์„ผ์„œ๋ฅผ ์‚ฌ์šฉํ•จ.
KakaoTalk_20241008_195100877.mp4
Untitled (24)

๊ฒฐ๊ณผ


๐ŸŒฑ ESP32 ๋ฐ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5๋ฅผ ์ด์šฉํ•œ ํ† ์–‘ ์ˆ˜๋ถ„ ์„ผ์„œ ์‹ค์Šต ๊ฐ€์ด๋“œ (AWS IoT & Node-RED)

ESP32์™€ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5๋ฅผ ํ™œ์šฉํ•˜์—ฌ AWS IoT Core์™€ Node-RED๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์Šค๋งˆํŠธ ํ™”๋ถ„ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค!

๐Ÿ“‹ ์ค€๋น„๋ฌผ

ํ•ญ๋ชฉ ์ˆ˜๋Ÿ‰
๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5 1
ESP32 ๊ฐœ๋ฐœ ๋ณด๋“œ 1
์•„๋‚ ๋กœ๊ทธ ํ† ์–‘ ์ˆ˜๋ถ„ ์„ผ์„œ 1
์ ํผ ์™€์ด์–ด ๋‹ค์ˆ˜
๋ธŒ๋ ˆ๋“œ๋ณด๋“œ 1
๋งˆ์ดํฌ๋กœ SD ์นด๋“œ (16GB ์ด์ƒ) 1
USB ์ผ€์ด๋ธ” (ESP32์šฉ) 1
์ €ํ•ญ (10kฮฉ) 1

๐Ÿ”ง ํ•˜๋“œ์›จ์–ด ์„ค์ •

  1. ํ† ์–‘ ์ˆ˜๋ถ„ ์„ผ์„œ๋ฅผ ESP32์˜ ์•„๋‚ ๋กœ๊ทธ ํ•€(์˜ˆ: GPIO 36)์— ์—ฐ๊ฒฐ
    • ์„ผ์„œ์˜ VCC๋ฅผ ESP32์˜ 3.3V์— ์—ฐ๊ฒฐ
    • ์„ผ์„œ์˜ GND๋ฅผ ESP32์˜ GND์— ์—ฐ๊ฒฐ
    • ์„ผ์„œ์˜ ์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ์„ 10kฮฉ ์ €ํ•ญ์„ ํ†ตํ•ด ESP32์˜ GPIO 36 (ADC)์— ์—ฐ๊ฒฐ
    • 10kฮฉ ์ €ํ•ญ์˜ ๋‹ค๋ฅธ ์ชฝ ๋์„ GND์— ์—ฐ๊ฒฐ (ํ’€๋‹ค์šด ์ €ํ•ญ์œผ๋กœ ์‚ฌ์šฉ)
  2. ESP32๋ฅผ USB ์ผ€์ด๋ธ”์„ ํ†ตํ•ด ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5์— ์—ฐ๊ฒฐ

ํšŒ๋กœ๋„

     3.3V
      |
      |
    ์„ผ์„œ(VCC)
      |
      |
    ์„ผ์„œ(์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ)
      |
      |-----> GPIO 36 (ADC)
      |
     10kฮฉ
      |
     GND

์ด ํšŒ๋กœ ๊ตฌ์„ฑ์—์„œ 10kฮฉ ์ €ํ•ญ์€ ํ’€๋‹ค์šด ์ €ํ•ญ์œผ๋กœ ์ž‘์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค:

  1. ๋…ธ์ด์ฆˆ ๊ฐ์†Œ: ์„ผ์„œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์˜ค์ž‘๋™ํ•  ๋•Œ ํ”Œ๋กœํŒ… ์ƒํƒœ๋ฅผ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  2. ์ „๋ฅ˜ ์ œํ•œ: ์„ผ์„œ์™€ ๋งˆ์ดํฌ๋กœ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋ณดํ˜ธํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.
  3. ์‹ ํ˜ธ ์•ˆ์ •ํ™”: ๋” ์•ˆ์ •์ ์ด๊ณ  ์ผ๊ด€๋œ ์ฝ๊ธฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • ESP32์™€ ํ† ์–‘ ์ˆ˜๋ถ„ ์„ผ์„œ:
    • GPIO 36 (ADC): ESP32์˜ ์•„๋‚ ๋กœ๊ทธ ์ž…๋ ฅ ํ•€์— ์„ผ์„œ์˜ ์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
    • 3.3V: ESP32์˜ 3.3V ์ถœ๋ ฅ์„ ์„ผ์„œ์˜ VCC์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
    • GND: ESP32์˜ GND๋ฅผ ์„ผ์„œ์˜ GND์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ป ์†Œํ”„ํŠธ์›จ์–ด ์„ค์ •

๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5 ์„ค์ •

  1. ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด OS (64๋น„ํŠธ) ์„ค์น˜
  2. Node-RED ์„ค์น˜:
    bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
    
  3. Node-RED ์ž๋™ ์‹œ์ž‘ ์„ค์ •:
    sudo systemctl enable nodered.service
    

ESP32 ์„ค์ •

  1. Arduino IDE ์„ค์น˜ ๋ฐ ESP32 ๋ณด๋“œ ๋งค๋‹ˆ์ € ์ถ”๊ฐ€
  2. ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜:
    • MQTT by Joel Gaehwiler
    • ArduinoJson by Benoit Blanchon
  3. ESP32 ๋ณด๋“œ์šฉ Arduino ์ฝ”๋“œ ์ž‘์„ฑ ๋ฐ ์—…๋กœ๋“œ

AWS IoT Core ์„ค์ •

  1. AWS ๊ณ„์ • ์ƒ์„ฑ (์•„์ง ์—†๋Š” ๊ฒฝ์šฐ)
  2. AWS IoT Core์—์„œ ์ƒˆ "์‚ฌ๋ฌผ" ์ƒ์„ฑ
  3. ์ธ์ฆ์„œ ๋ฐ ํ‚ค ๋‹ค์šด๋กœ๋“œ
  4. ์ •์ฑ… ์ƒ์„ฑ ๋ฐ ์—ฐ๊ฒฐ

๐Ÿ” ESP32์—์„œ ์„ผ์„œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ๋ฐ AWS IoT๋กœ ์ „์†ก

#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include "secrets.h"

const int soilMoisturePin = 36;  // GPIO 36 (ADC0)

WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);

void connectAWS()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("Connecting to Wi-Fi");

  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }

  // Configure WiFiClientSecure to use the AWS IoT device credentials
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);

  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.begin(AWS_IOT_ENDPOINT, 8883, net);

  // Create a message handler
  client.onMessage(messageHandler);

  Serial.print("Connecting to AWS IOT");

  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if(!client.connected()){
    Serial.println("AWS IoT Timeout!");
    return;
  }

  Serial.println("AWS IoT Connected!");
}

void publishMessage()
{
  int moistureValue = analogRead(soilMoisturePin);
  int moisturePercent = map(moistureValue, 4095, 0, 0, 100);
  
  StaticJsonDocument<200> doc;
  doc["moisture"] = moisturePercent;
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer);

  client.publish("garden/moisture", jsonBuffer);
}

void setup() {
  Serial.begin(115200);
  connectAWS();
}

void loop() {
  publishMessage();
  client.loop();
  delay(1000);
}

void messageHandler(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
}

๐Ÿ Node-RED ํ”Œ๋กœ์šฐ

  1. Node-RED ๋Œ€์‹œ๋ณด๋“œ์— ์ ‘์† (๊ธฐ๋ณธ ์ฃผ์†Œ: http://๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ดIP:1880)
  2. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ”Œ๋กœ์šฐ๋ฅผ ์ƒ์„ฑ:
[mqtt in] --> [json] --> [function] --> [dashboard]
  • mqtt in ๋…ธ๋“œ: AWS IoT Core์˜ MQTT ๋ธŒ๋กœ์ปค์— ์—ฐ๊ฒฐ
  • json ๋…ธ๋“œ: ์ˆ˜์‹ ๋œ JSON ๋ฉ”์‹œ์ง€๋ฅผ ํŒŒ์‹ฑ
  • function ๋…ธ๋“œ: ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ ์•Œ๋ฆผ ๋กœ์ง ๊ตฌํ˜„
  • dashboard ๋…ธ๋“œ: ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”

function ๋…ธ๋“œ ์ฝ”๋“œ ์˜ˆ์‹œ:

const moisture = msg.payload.moisture;
msg.payload = moisture;

if (moisture < 30) {
    node.warn("ํ† ์–‘์ด ๊ฑด์กฐํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ์„ ์ฃผ์„ธ์š”!");
}

return msg;

๐Ÿงฎ ์ฃผ์š” ๊ธฐ๋Šฅ

  1. ์ˆ˜๋ถ„ ํ•จ๋Ÿ‰ ์ธก์ •: ESP32์—์„œ ์„ผ์„œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
  2. ๋ฐ์ดํ„ฐ ์ „์†ก: ESP32์—์„œ AWS IoT Core๋กœ MQTT๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์ „์†ก
  3. ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ ์‹œ๊ฐํ™”: Node-RED์—์„œ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ , ์ฒ˜๋ฆฌ, ๋Œ€์‹œ๋ณด๋“œ ํ‘œ์‹œ
  4. ์•Œ๋ฆผ: ์ž„๊ณ„๊ฐ’ ๊ธฐ๋ฐ˜ ์•Œ๋ฆผ (Node-RED์—์„œ ์ฒ˜๋ฆฌ)

๐Ÿš€ ์‹ฌํ™” ํ•™์Šต

  1. AWS Lambda๋ฅผ ์‚ฌ์šฉํ•œ ์„œ๋ฒ„๋ฆฌ์Šค ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
  2. Amazon DynamoDB๋ฅผ ํ™œ์šฉํ•œ ์žฅ๊ธฐ ๋ฐ์ดํ„ฐ ์ €์žฅ
  3. AWS SNS๋ฅผ ์ด์šฉํ•œ ๋ชจ๋ฐ”์ผ ํ‘ธ์‹œ ์•Œ๋ฆผ ๊ตฌํ˜„

โš ๏ธ ์ฃผ์˜์‚ฌํ•ญ

  • ์ „๊ธฐ ์—ฐ๊ฒฐ ์‹œ ์•ˆ์ „์— ์œ ์˜
  • AWS ํ”„๋ฆฌ ํ‹ฐ์–ด ์‚ฌ์šฉ๋Ÿ‰ ์ œํ•œ ํ™•์ธ
  • ESP32 ๋ฐ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5 ๋ฐœ์—ด ๊ด€๋ฆฌ

๐Ÿ”— ์‹œ์Šคํ…œ ๊ตฌ์กฐ

graph TD
    A[ํ† ์–‘ ์ˆ˜๋ถ„ ์„ผ์„œ] -->|์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ| B[ESP32]
    B -->|MQTT| C[AWS IoT Core]
    C -->|MQTT| D[Node-RED]
    D -->|๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ| E[Node-RED ๋Œ€์‹œ๋ณด๋“œ]
    D -->|์กฐ๊ฑด๋ถ€ ์•Œ๋ฆผ| F[์‚ฌ์šฉ์ž ์•Œ๋ฆผ]
Loading

์ด ๊ตฌ์กฐ๋Š” ์„ผ์„œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘๋ถ€ํ„ฐ ์‹œ๊ฐํ™” ๋ฐ ์•Œ๋ฆผ๊นŒ์ง€์˜ ์ „์ฒด ํ๋ฆ„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.


๊ณต๊ธฐ์ฒญ์ •๊ธฐ

3.3V
      |
      |---> ์„ผ์„œ๋“ค(VCC)
      |
    ์„ผ์„œ๋“ค(๋ฐ์ดํ„ฐ ํ•€)
      |
      |-----> ํ•ด๋‹น GPIO ํ•€
      |
     10kฮฉ (๊ฐ ์„ผ์„œ๋‹น)
      |
     GND -----> ์„ผ์„œ๋“ค(GND)
      |
      |-----> MOSFETs(Source)
      
    GPIO 25 -----> MOSFET1(Gate) --> ํก์ž… ํŒฌ
    GPIO 26 -----> MOSFET2(Gate) --> ๋ฐฐ์ถœ ํŒฌ
    GPIO 27 -----> MOSFET3(Gate) --> LED ์กฐ๋ช…
                     
    ์™ธ๋ถ€ ์ „์›(+) ------------------> ํŒฌ๋“ค(+), LED(+)
    ์™ธ๋ถ€ ์ „์›(-) ------------------> MOSFETs(Drain)

๋˜๋Š”

๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ๋กœ("์ œ์–ด ์‹ ํ˜ธ ์–ด๋Œ‘ํ„ฐ" ๋˜๋Š” "์ „๋ ฅ ์ œ์–ด ์–ด๋Œ‘ํ„ฐ") ์ฐจ๋Ÿ‰์šฉ ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ์–ดํ•˜๊ธฐ

-> ํ•ด๋‹น ํ”„๋กœํ† ํƒ€์ž…

๐ŸŒฌ๏ธ ESP32์™€ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์ด์šฉํ•œ ๋ฏธ์„ธ๋จผ์ง€ ๊ฐ์ง€ ์ž๋™ ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ์–ด ์‹œ์Šคํ…œ

๐Ÿ“‹ ์ค€๋น„๋ฌผ

ํ•ญ๋ชฉ ์ˆ˜๋Ÿ‰
๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5 1
ESP32 ๊ฐœ๋ฐœ ๋ณด๋“œ 1
์•„๋‚ ๋กœ๊ทธ ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ (์˜ˆ: GP2Y1010AU0F) 1
๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ (3.3V) 1
์†Œํ˜• ๊ณต๊ธฐ์ฒญ์ •๊ธฐ 1
์ ํผ ์™€์ด์–ด ๋‹ค์ˆ˜
๋งˆ์ดํฌ๋กœ SD ์นด๋“œ (32GB ๊ถŒ์žฅ) 1
USB ์ผ€์ด๋ธ” (ESP32์šฉ) 1
๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด ๋ฐ ESP32 ์ „์› ๊ณต๊ธ‰ ์žฅ์น˜ ๊ฐ 1

๐Ÿ”ง ํ•˜๋“œ์›จ์–ด ์„ค์ •

  1. ESP32์™€ ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ ์—ฐ๊ฒฐ:

    • VCC๋ฅผ ESP32์˜ 3.3V์— ์—ฐ๊ฒฐ
    • GND๋ฅผ ESP32์˜ GND์— ์—ฐ๊ฒฐ
    • ์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ์„ ESP32์˜ GPIO 36 (ADC)์— ์—ฐ๊ฒฐ
  2. ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์™€ ๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ ์—ฐ๊ฒฐ:

    • VCC๋ฅผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ 3.3V์— ์—ฐ๊ฒฐ
    • GND๋ฅผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ GND์— ์—ฐ๊ฒฐ
    • ์‹ ํ˜ธ ํ•€์„ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ GPIO 18์— ์—ฐ๊ฒฐ
  3. ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์—ฐ๊ฒฐ:

    • ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ์ „์› ์ผ€์ด๋ธ”์„ ๋ฆด๋ ˆ์ด์˜ NO(Normally Open)์™€ COM(Common) ๋‹จ์ž์— ์—ฐ๊ฒฐ
  4. ESP32์™€ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด ์—ฐ๊ฒฐ:

    • ESP32์˜ TX๋ฅผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ GPIO 14 (UART RX)์— ์—ฐ๊ฒฐ
    • ESP32์˜ RX๋ฅผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ GPIO 15 (UART TX)์— ์—ฐ๊ฒฐ

ํšŒ๋กœ๋„

[ESP32 ๋ถ€๋ถ„]
     3.3V
      |
      |---> ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ(VCC)
      |
    ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ(์•„๋‚ ๋กœ๊ทธ ์ถœ๋ ฅ) -----> GPIO 36 (ADC)
      |
     GND -----> ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ(GND)

[๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด ๋ถ€๋ถ„]
     3.3V ------> ๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ(VCC)
     
    GPIO 18 ---> ๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ(์‹ ํ˜ธ ํ•€)
     
     GND -----> ๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ(GND)
     
    ๋ฆด๋ ˆ์ด(NO) ---> ๊ณต๊ธฐ์ฒญ์ •๊ธฐ(์ „์›์„ )
    ๋ฆด๋ ˆ์ด(COM) ---> ์ „์› ์ฝ˜์„ผํŠธ

[ESP32์™€ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด ์—ฐ๊ฒฐ]
    ESP32 TX -----> ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด GPIO 14 (UART RX)
    ESP32 RX <----- ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด GPIO 15 (UART TX)

๐Ÿ’จ ์‹œ์Šคํ…œ ๊ตฌ์„ฑ

  1. ESP32

    • ๊ธฐ๋Šฅ: ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ๋ฐ ์ฒ˜๋ฆฌ
    • ์—ฐ๊ฒฐ: ์•„๋‚ ๋กœ๊ทธ ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ, UART ํ†ต์‹ (๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์™€)
  2. ์•„๋‚ ๋กœ๊ทธ ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ

    • ๊ธฐ๋Šฅ: ๋ฏธ์„ธ๋จผ์ง€ ๋†๋„ ์ธก์ •
    • ESP32 ์—ฐ๊ฒฐ: GPIO 36 (ADC)
  3. ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5

    • ๊ธฐ๋Šฅ: ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ์ €์žฅ, ์›น ์ธํ„ฐํŽ˜์ด์Šค ์ œ๊ณต, ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ์–ด
    • ์—ฐ๊ฒฐ: UART ํ†ต์‹ (ESP32์™€), ๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ
  4. ๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ

    • ๊ธฐ๋Šฅ: ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ „์› ์ œ์–ด
    • ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด ์—ฐ๊ฒฐ: GPIO 18 (๋””์ง€ํ„ธ ์ถœ๋ ฅ)
  5. ์†Œํ˜• ๊ณต๊ธฐ์ฒญ์ •๊ธฐ

    • ๊ธฐ์กด ์ œํ’ˆ ์‚ฌ์šฉ, ๋ฆด๋ ˆ์ด๋ฅผ ํ†ตํ•ด ์ „์› ์ œ์–ด

๐Ÿ” ESP32 ์ฝ”๋“œ (Arduino IDE)

#include <HardwareSerial.h>

const int dustPin = 36;
const int ledPin = 22;  // ์„ผ์„œ์˜ LED ์ œ์–ด์šฉ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ)

unsigned long lastTime = 0;
unsigned long sampleTime = 280;
unsigned long deltaTime = 40;
int dustVal = 0;

void setup() {
  Serial.begin(115200);  // ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์™€์˜ ํ†ต์‹ ์šฉ
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if ((millis() - lastTime) > sampleTime) {
    digitalWrite(ledPin, LOW);
    delayMicroseconds(sampleTime);
    dustVal = analogRead(dustPin);
    delayMicroseconds(deltaTime);
    digitalWrite(ledPin, HIGH);
    lastTime = millis();

    // ์„ผ์„œ ๊ฐ’์„ ๋ฏธ์„ธ๋จผ์ง€ ๋†๋„๋กœ ๋ณ€ํ™˜ (์„ผ์„œ ์‚ฌ์–‘์— ๋”ฐ๋ผ ์กฐ์ • ํ•„์š”)
    float voltage = dustVal * (3.3 / 4095.0);
    float dustDensity = 0.17 * voltage - 0.1;
    
    if (dustDensity < 0) {
      dustDensity = 0;
    }

    // ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก
    Serial.println(dustDensity);
  }
}

๐Ÿ” ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด Python ์ฝ”๋“œ

import serial
import time
import RPi.GPIO as GPIO

# GPIO ์„ค์ •
GPIO.setmode(GPIO.BCM)
RELAY_PIN = 18
GPIO.setup(RELAY_PIN, GPIO.OUT)

# UART ์„ค์ •
ser = serial.Serial('/dev/ttyAMA0', 115200)

# ๋ฏธ์„ธ๋จผ์ง€ ๋†๋„ ์ž„๊ณ„๊ฐ’ (ฮผg/mยณ)
DUST_THRESHOLD = 35  # ์˜ˆ์‹œ ๊ฐ’, ์‹ค์ œ ์‚ฌ์šฉ ์‹œ ์กฐ์ • ํ•„์š”

def read_dust_density():
    if ser.in_waiting > 0:
        try:
            return float(ser.readline().decode().strip())
        except ValueError:
            return None
    return None

def control_air_purifier(density):
    if density is not None:
        if density > DUST_THRESHOLD:
            GPIO.output(RELAY_PIN, GPIO.HIGH)  # ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ผœ๊ธฐ
            print(f"๋ฏธ์„ธ๋จผ์ง€ ๋†๋„ ๋†’์Œ ({density:.2f} ฮผg/mยณ). ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ผœ์ง.")
        else:
            GPIO.output(RELAY_PIN, GPIO.LOW)   # ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ๋„๊ธฐ
            print(f"๋ฏธ์„ธ๋จผ์ง€ ๋†๋„ ์ •์ƒ ({density:.2f} ฮผg/mยณ). ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ๊บผ์ง.")

try:
    while True:
        dust_density = read_dust_density()
        if dust_density is not None:
            control_air_purifier(dust_density)
        time.sleep(1)  # 1์ดˆ๋งˆ๋‹ค ์ฒดํฌ

except KeyboardInterrupt:
    GPIO.cleanup()
    ser.close()
    print("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ")

๐Ÿ”— ์‹œ์Šคํ…œ ๊ตฌ์กฐ

graph TD
    A[์•„๋‚ ๋กœ๊ทธ ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ] -->|์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ| B[ESP32]
    B -->|UART| C[๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด 5]
    C -->|GPIO ์ œ์–ด| D[๋ฆด๋ ˆ์ด ๋ชจ๋“ˆ]
    D -->|์ „์› ์ œ์–ด| E[์†Œํ˜• ๊ณต๊ธฐ์ฒญ์ •๊ธฐ]
    C -->|๋ฐ์ดํ„ฐ ์ €์žฅ/๋ถ„์„| F[๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค]
    C -->|์›๊ฒฉ ๋ชจ๋‹ˆํ„ฐ๋ง| G[์›น ์ธํ„ฐํŽ˜์ด์Šค]
Loading

์ด ํ”„๋กœ์ ํŠธ๋Š” ESP32๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋‚ ๋กœ๊ทธ ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ์ด๋ฅผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋Š” ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋ฅผ ์ œ์–ดํ•˜๊ณ , ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐ ์›น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๐Ÿš€ ํ™•์žฅ ๊ฐ€๋Šฅ์„ฑ

  1. ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”: Grafana๋‚˜ Matplotlib์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ์„ธ๋จผ์ง€ ๋†๋„ ์ถ”์ด๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œ์‹œ
  2. ๋จธ์‹ ๋Ÿฌ๋‹ ๊ธฐ๋ฐ˜ ์˜ˆ์ธก: ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฏธ์„ธ๋จผ์ง€ ๋†๋„ ์˜ˆ์ธก ๋ชจ๋ธ ๊ฐœ๋ฐœ
  3. ๋‹ค์ค‘ ์„ผ์„œ ์ง€์›: ์—ฌ๋Ÿฌ ๊ฐœ์˜ ESP32์™€ ์„ผ์„œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„“์€ ๊ณต๊ฐ„์˜ ๋ฏธ์„ธ๋จผ์ง€ ๋ถ„ํฌ ์ธก์ •
  4. ์Šค๋งˆํŠธํ™ˆ ํ†ตํ•ฉ: HomeAssistant ๋“ฑ์˜ ํ”Œ๋žซํผ๊ณผ ์—ฐ๋™ํ•˜์—ฌ ์ข…ํ•ฉ์ ์ธ ์Šค๋งˆํŠธํ™ˆ ์‹œ์Šคํ…œ ๊ตฌ์ถ•

โš ๏ธ ์ฃผ์˜์‚ฌํ•ญ

  • ์ „๊ธฐ ์ž‘์—… ์‹œ ์•ˆ์ „์— ์œ ์˜ํ•˜์„ธ์š”. ํ•„์š”ํ•˜๋‹ค๋ฉด ์ „๋ฌธ๊ฐ€์˜ ๋„์›€์„ ๋ฐ›์œผ์„ธ์š”.
  • ๋ฏธ์„ธ๋จผ์ง€ ์„ผ์„œ์˜ ์ •ํ™•ํ•œ ์บ˜๋ฆฌ๋ธŒ๋ ˆ์ด์…˜์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์„ผ์„œ ๋ฐ์ดํ„ฐ์‹œํŠธ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.
  • ESP32์™€ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ ์ „์› ๊ด€๋ฆฌ์— ์ฃผ์˜ํ•˜์„ธ์š”.
  • ์ •๊ธฐ์ ์œผ๋กœ ์‹œ์Šคํ…œ์„ ์ ๊ฒ€ํ•˜๊ณ , ์„ผ์„œ๋ฅผ ์ฒญ์†Œํ•˜์—ฌ ์ •ํ™•๋„๋ฅผ ์œ ์ง€ํ•˜์„ธ์š”.
  • ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ์–ด ์‹œ ์ œ์กฐ์‚ฌ์˜ ๊ถŒ์žฅ์‚ฌํ•ญ์„ ์ค€์ˆ˜ํ•˜์„ธ์š”.

ํ™ˆ์–ด์‹œ์Šคํ„ดํŠธ ์„ค์น˜

python3 -m venv homeassistant
cd homeassistant
source bin/activate
pip3 install homeassistant
image

ํŒŒ์ด์ฌ 3.12๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ์š”๊ตฌ

์ด๋ ‡๊ฒŒ ๊นŒ๋Š”๊ฑฐ ์•„๋‹˜. Imager ์‚ฌ์šฉํ•˜์ž~

โš ๏ธ **GitHub.com Fallback** โš ๏ธ