Edge AI - 3. Edge AI 프로그래밍 실습 (3-3. IoT + Edge AI 연동)

2025. 3. 10. 18:21AI/AI

📌 3-3. IoT + Edge AI 연동

IoT와 Edge AI를 결합하면 센서 데이터를 실시간으로 분석하고 스마트홈, 산업 자동화, 보안 시스템 등에 적용할 수 있습니다.
특히, ESP32 + AI 모델을 활용한 센서 데이터 분석, Edge AI 기반 스마트홈 프로젝트, 음성 명령을 통한 자동 조명 제어, 사람 감지 후 알림 전송과 같은 기능이 주요 활용 사례입니다.


🌟 1. IoT 센서 데이터 분석 (ESP32 + AI 모델)

ESP32는 Wi-Fi & Bluetooth 기능을 내장한 저전력 IoT 디바이스로, Edge AI 모델을 실행하여 센서 데이터를 실시간 분석하고 예측할 수 있습니다.

(1) ESP32에서 센서 데이터 수집

ESP32는 온도, 습도, 조도, 가속도 센서 등의 데이터를 AI 모델로 분석할 수 있습니다.

📌 DHT22 온습도 센서 데이터 수집 코드 (ESP32 + Arduino)

#include <DHT.h>

#define DHTPIN 2      // DHT22 센서 핀
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print("°C, Humidity: ");
    Serial.print(humidity);
    Serial.println("%");
    
    delay(2000);
}

(2) AI 모델을 활용한 센서 데이터 분석 (ESP32 + TensorFlow Lite for Microcontrollers)

  • 센서 데이터를 활용해 환경 예측, 이상 감지, 자동화 시스템을 구축할 수 있습니다.
  • 예를 들어 온습도 데이터를 분석하여 화재 감지, 기기 고장 예측이 가능합니다.

📌 ESP32에서 AI 모델 실행 코드 (TensorFlow Lite for Microcontrollers)

#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"

extern const unsigned char model_data[];  // TinyML 모델 데이터

void setup() {
    Serial.begin(115200);
    Serial.println("Starting TinyML Sensor Analysis...");
    
    // AI 모델 로드
    tflite::MicroInterpreter interpreter(model, resolver, tensor_arena, tensor_arena_size, &error_reporter);
    interpreter.AllocateTensors();
}

void loop() {
    float sensor_data = read_sensor(); // 센서 데이터 읽기
    interpreter.input(0)->data.f[0] = sensor_data;
    interpreter.Invoke();
    
    float prediction = interpreter.output(0)->data.f[0];
    Serial.println("AI Prediction: " + String(prediction));
}

📌 활용 사례

  • 스마트 농업: 토양 습도 예측 후 자동 관개 시스템 제어
  • 공장 자동화: 센서 데이터를 AI로 분석하여 기계 고장 예측
  • 실내 환경 모니터링: AI 기반 온습도 조절 시스템

🌟 2. Edge AI 기반 스마트홈 프로젝트

Edge AI와 IoT를 활용하면 스마트홈 기기를 자동화할 수 있습니다.
특히, 음성 명령을 통한 조명 제어, 사람 감지 후 알림 전송 등의 기능이 대표적입니다.


(1) 음성 명령으로 자동 조명 제어

ESP32는 음성 인식을 활용하여 스마트 조명을 제어할 수 있습니다.
이를 위해 MQTT 프로토콜을 사용하여 IoT 디바이스와 연결합니다.

📌 ESP32 + MQTT 기반 스마트 조명 제어 코드

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);

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

    client.setServer(mqtt_server, 1883);
    client.connect("ESP32_Client");
}

void loop() {
    if (WakeWordDetected()) {  // 음성 인식 AI 실행 결과
        client.publish("home/light", "ON");
        Serial.println("Light Turned ON!");
    }
}

📌 활용 사례

  • "불 켜줘" 음성 명령을 통해 조명을 제어
  • 음성으로 스마트 가전제품 제어 (TV, 에어컨, 커튼 등)
  • 스마트홈 자동화 시스템 구축

(2) 사람 감지 후 알림 전송 (ESP32 + PIR 센서 + Telegram Bot)

Edge AI와 IoT 센서를 활용하면 사람이 감지될 경우 알림을 전송하는 보안 시스템을 구축할 수 있습니다.

📌 ESP32 + PIR 센서 기반 사람 감지 코드

#include 
#include 

const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
const char* telegram_bot_token = "YOUR_TELEGRAM_BOT_TOKEN";
const char* chat_id = "YOUR_CHAT_ID";

#define PIR_SENSOR 2

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);

    pinMode(PIR_SENSOR, INPUT);
}

void loop() {
    if (digitalRead(PIR_SENSOR) == HIGH) {
        Serial.println("Motion Detected!");
        sendTelegramMessage("🚨 Motion Detected at Home!");
    }
    delay(1000);
}

void sendTelegramMessage(String message) {
    HTTPClient http;
    String url = "https://api.telegram.org/bot" + String(telegram_bot_token) + "/sendMessage?chat_id=" + String(chat_id) + "&text=" + message;
    http.begin(url);
    http.GET();
    http.end();
}

📌 활용 사례

  • 집 안에 침입자 감지 후 사용자에게 알림 전송
  • 사람 감지를 기반으로 스마트 조명 자동 제어
  • 공장 및 창고 보안 시스템 구축

📌 요약 정리

활용 방식 주요 기술 활용 사례
IoT 센서 데이터 분석 ESP32 + TinyML + 센서 스마트 농업, 환경 모니터링
Edge AI 기반 스마트홈 MQTT, 음성 인식, AI 가속기 스마트 조명, 가전제품 제어
음성 명령으로 조명 제어 ESP32 + AI + MQTT 스마트홈 자동화
사람 감지 후 알림 전송 ESP32 + PIR 센서 + Telegram 보안 시스템, 공장 감시