---
id: wiki-2026-0508-iot-and-ai-integration
title: IoT and AI Integration
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Edge AI, TinyML, IoT AI, AIoT]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [iot, edge-ai, tinyml, embedded, mqtt, sensor-fusion]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: cpp
  framework: tflite-micro
---

# IoT and AI Integration

## 매 한 줄
> **"매 센서 옆에서 즉시 추론"**. 수 KB-수 MB 모델을 ESP32/Cortex-M/Coral 같은 edge 기기에 올려 latency, privacy, bandwidth 를 동시에 잡는다. 2026 현재 TinyML + MQTT 가 표준.

## 매 핵심

### 매 3 layer 아키텍처
1. **Edge (sensor)**: TinyML 추론, 이상 감지, 게이팅.
2. **Fog (gateway)**: 다중 센서 통합, heavier 모델, 로컬 의사결정.
3. **Cloud**: 학습, fleet 모니터링, OTA 모델 업데이트.

### 매 모델 압축
- **Quantization**: float32 → int8/int4, 4-8x 작아짐.
- **Pruning**: weight magnitude 기준 sparsify.
- **Knowledge distillation**: large teacher → tiny student.
- **NAS for edge**: MCUNet, MobileNet.

### 매 통신
- **MQTT**: pub/sub, QoS 0/1/2.
- **CoAP**: REST-over-UDP, 더 가벼움.
- **LoRaWAN**: km 단위, 수백 byte/min.
- **BLE**: 근거리, 저전력.

### 매 응용
1. Predictive maintenance (vibration anomaly).
2. Vision: door cam person detection, defect inspection.
3. Voice wakeword (Alexa, "OK Google").
4. Smart agriculture (soil moisture + weather).
5. Health wearable (HRV, fall detection).

## 💻 패턴

### TFLite Micro 추론 (ESP32, C++)
```cpp
#include "tensorflow/lite/micro/micro_interpreter.h"

const tflite::Model* model = tflite::GetModel(g_model_data);
static tflite::MicroMutableOpResolver<5> resolver;
resolver.AddConv2D(); resolver.AddMaxPool2D();
resolver.AddReshape(); resolver.AddFullyConnected(); resolver.AddSoftmax();

constexpr int kArena = 60 * 1024;
uint8_t arena[kArena];
tflite::MicroInterpreter interp(model, resolver, arena, kArena);
interp.AllocateTensors();
TfLiteTensor* in = interp.input(0);
memcpy(in->data.int8, sensor_buf, in->bytes);
interp.Invoke();
int8_t* out = interp.output(0)->data.int8;
```

### Post-training quantization (Python)
```python
import tensorflow as tf

conv = tf.lite.TFLiteConverter.from_saved_model("model")
conv.optimizations = [tf.lite.Optimize.DEFAULT]
conv.target_spec.supported_types = [tf.int8]
conv.representative_dataset = lambda: (
    [x.astype("float32")] for x in calib_samples[:200]
)
open("model_int8.tflite", "wb").write(conv.convert())
```

### MQTT publish 추론 결과 (MicroPython)
```python
from umqtt.simple import MQTTClient

c = MQTTClient("esp32-01", "broker.local")
c.connect()
while True:
    feat = read_imu()
    label = tinyml_infer(feat)
    if label != "normal":
        c.publish(b"factory/line1/anomaly",
                  ujson.dumps({"label": label, "ts": time.time()}))
    time.sleep(0.1)
```

### Sensor fusion (Kalman, complementary)
```cpp
// 6DOF IMU complementary filter
float alpha = 0.98f;
roll  = alpha * (roll + gyro_x * dt) + (1 - alpha) * accel_roll;
pitch = alpha * (pitch + gyro_y * dt) + (1 - alpha) * accel_pitch;
```

### Edge Impulse SDK (anomaly)
```cpp
ei_impulse_result_t result;
signal_t signal;
numpy::signal_from_buffer(features, EI_FEATURE_COUNT, &signal);
run_classifier(&signal, &result, false);
if (result.anomaly > 0.5) trigger_alert();
```

### OTA 모델 업데이트
```cpp
HTTPClient http;
http.begin("https://cdn/model_v3.tflite");
if (http.GET() == 200) {
  File f = SPIFFS.open("/model.tflite", "w");
  http.writeToStream(&f); f.close();
  ESP.restart();
}
```

### Coral Edge TPU (Python)
```python
from pycoral.utils.edgetpu import make_interpreter
from pycoral.adapters import classify

it = make_interpreter("model_edgetpu.tflite")
it.allocate_tensors()
it.set_tensor(it.get_input_details()[0]["index"], img)
it.invoke()
print(classify.get_classes(it, top_k=1))
```

## 매 결정 기준
| 제약 | 권장 |
|---|---|
| MCU < 1MB RAM | TFLite Micro int8, MCUNet |
| 배터리 1년+ | LoRaWAN + duty cycle |
| Vision realtime | Coral / Jetson Nano |
| Privacy critical | edge inference, no cloud raw |
| Fleet 수만+ | MQTT broker cluster, OTA |

**기본값**: ESP32 + TFLite Micro int8 + MQTT QoS 1.

## 🔗 Graph
- 부모: [[Edge Computing|Edge-Computing]]
- 변형: [[TinyML]], [[Federated-Learning]]
- 응용: [[Predictive Maintenance]], [[Wearables]]
- Adjacent: [[LLM_Optimization_and_Deployment_Strategies|Quantization]], [[MQTT]]

## 🤖 LLM 활용
**언제**: latency 100ms 이하 필요, 네트워크 불안정, privacy 규제, bandwidth 비싼 환경.
**언제 X**: 모델 100MB+, 잦은 재학습 필요, 입력이 크고 다양한 multimodal.

## ❌ 안티패턴
- **Cloud 의존 edge**: 인터넷 끊기면 동작 안 함 → fallback 필수.
- **Float32 모델 그대로 배포**: RAM 부족, 발열.
- **OTA 미고려**: 모델 버그 fix 불가.
- **Sensor 단일**: 노이즈에 취약, fusion 으로 견고화.
- **MQTT QoS 0 + 중요 alert**: 패킷 유실 가능.

## 🧪 검증 / 중복
- TFLite Micro, Edge Impulse, MQTT 5.0 spec, Coral docs.
- Warden & Situnayake "TinyML" (O'Reilly).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — 3-layer 구조, TFLite Micro/Edge Impulse/MQTT/OTA 패턴 |
