• Home
  • About
    • PI photo

      PI

      Beginner's Blog

    • Learn More
    • Github
  • Posts
    • All Posts
    • All Tags
    • All Categories
  • Projects

[ESW] 스마트가전 통합 제어 엔지니어 기술 정리

📆 Created: 2025.05.25 Sun

Reading time ~2 minutes

1. Python

  • 개념
    • 범용 스크립팅 언어로, REST API 호출·데이터 처리·자동화 스크립트 작성에 최적화
    • 풍부한 라이브러리 생태계(requests, influxdb-client 등)
  • 구현 방법
    1. 가상환경 생성
        python3 -m venv venv
        source venv/bin/activate
      
    2. 필수 패키지 설치
       pip install requests pyyaml influxdb-client pythinqconnect
      
    3. ThinQ API 호출 예시 ThinQ API 기기별로 정리하기!

       from pythinqconnect import ThinQ
      
       thinQ = ThinQ(client_id, client_secret, redirect_uri)
       thinQ.authenticate()  
       fridge = thinQ.device('DEVICE_ID')
       status = fridge.get_status()
       print("온도:", status['temperature'])
      

2. REST API

  • 개념
    • HTTP 메서드(GET/POST 등)로 “리소스” 조회·제어
    • 상태 코드·JSON 페이로드 기반 경량 통신
  • 구현 방법
    1. 엔드포인트 설계

       GET  /api/v1/devices/{id}/status
       POST /api/v1/devices/{id}/actions
      
    2. Python 호출 예시

       import requests
      
       BASE = "https://localhost:5000/api/v1"
       headers = {"Authorization": f"Bearer {token}"}
      
       # 상태 조회
       r = requests.get(f"{BASE}/devices/{device_id}/status", headers=headers)
       data = r.json()
      
       # 제어 명령
       cmd = {"action":"turn_on","params":{"mode":"turbo"}}
       requests.post(f"{BASE}/devices/{device_id}/actions", json=cmd, headers=headers)
      

3. InfluxDB

  • 개념
    • 시계열 데이터베이스(TSDB)
    • 센서·전력 소비량 등 시간 축 데이터 저장·쿼리에 최적
  • 구현 방법
    1. Docker로 설치
       docker run -d --name influxdb -p 8086:8086 influxdb:2.7
       influx setup
      
    2. Python 쓰기/조회 예시
       from influxdb_client import InfluxDBClient, Point
      
       client = InfluxDBClient(url="http://localhost:8086", token=token, org="org")
       write = client.write_api()
      
       # 데이터 쓰기
       p = Point("power").tag("device","vacuum").field("watt",45.2)
       write.write(bucket="home", record=p)
      
       # 평균 조회
       query = '''
       from(bucket:"home")
           |> range(start:-7d)
           |> filter(fn: (r) => r._measurement=="power")
           |> mean()
       '''
       tables = client.query_api().query(query)
       for table in tables:
           for rec in table.records:
               print(rec.get_value())
      

4. ThinQ API (pythinqconnect)

  • 개념
    • LG ThinQ 가전을 제어·모니터링하는 공식 Python 모듈
    • OAuth2 기반 인증 흐름 내장
  • 구현 방법
    1. 설치
       pip install pythinqconnect
      
    2. OAuth2 인증
       from pythinqconnect import ThinQ
      
       thinQ = ThinQ(client_id, client_secret, redirect_uri)
       thinQ.authenticate()  # 브라우저로 로그인 → 코드 교환 → 토큰 저장
      
    3. 디바이스 제어
       devices = thinQ.get_devices()
       ac = next(d for d in devices if d.type=="air_conditioner")
       ac.set_mode("cool")
       status = ac.get_status()
      

5. Node-RED

  • 개념
    • 시각적 플로우 기반 이벤트 처리·자동화 툴
    • MQTT, HTTP, Function 노드로 손쉬운 로직 구성
  • 구현 방법
    1. 설치
       npm install -g --unsafe-perm node-red
       node-red
      
    2. 플로우 예시
      • MQTT In: home/power 구독
      • Function:
         if (msg.payload > 100) {
          msg.payload = {action:"on", device:"air_purifier"};
          return msg;
         }
         return null;
        
      • HTTP Request: ThinQ API 호출

6. MQTT

  • 개념
    • 게시-구독(pub/sub) 경량 메시지 프로토콜
    • IoT 디바이스 간 비동기 통신 최적화(QoS 0/1/2)
  • 구현 방법
    1. Mosquitto 브로커 설치
       sudo apt install mosquitto
       mosquitto_passwd -c /etc/mosquitto/passwd user
      
    2. ESP32 예시 (Arduino)
       #include <WiFi.h>
       #include <PubSubClient.h>
      
       WiFiClientSecure net;
       PubSubClient client(net);
      
       void setup() {
           net.setCACert(root_ca);
           client.setServer("broker.domain",8883);
           WiFi.begin(ssid,pwd);
           while(!WiFi.connected());
           client.connect("esp32","user","pass");
       }
      
       void loop() {
           float watt = analogRead(A0)*(5.0/1023.0);
           client.publish("home/power", String(watt).c_str());
           delay(5000);
       }
      

참고

  • Github: LG ThinQ
  • Homebridge - LG ThinQ 연동하기 (애플 홈 연동)
  • LG ThinQ REST API::파이썬 연동
  • LG ThinQ REST API::공기청정기 제어
  • mqtt실습(1)
  • mqtt실습(2)
  • mqtt실습(3)


Share Tweet +1
/#disqus_thread