ESP32-C6 Zigbee Coordinator: ควบคุม Smart Home แบบ Local

สร้างระบบควบคุมอุปกรณ์ smart home ด้วย Zigbee protocol แบบ local control ไม่ต้องพึ่งพา cloud ด้วย ESP32-C6 ที่รองรับ IEEE 802.15.4 radio สำหรับ Zigbee และ Thread

12 มีนาคม 2026 15 นาที ระดับกลาง Zigbee, ESP32-C6, Home Assistant

ข้อควรระวัง

บทความนี้ต้องการความรู้พื้นฐานเกี่ยวกับ ESP32, MQTT และ Home Assistant หากคุณเป็นมือใหม่แนะนำให้อ่านบทความ ESP32 getting started ก่อน

ภาพรวม Zigbee Coordinator

ทำไมต้องใช้ Zigbee Coordinator?

  • Local Control: ควบคุมอุปกรณ์ได้โดยไม่ต้องเชื่อมต่อ internet
  • ความเป็นส่วนตัว: ข้อมูลไม่ถูกส่งไปยัง cloud server
  • เร็วขึ้น: การตอบสนองที่เร็วกว่าการส่งผ่าน cloud
  • ประหยัดค่าใช้จ่าย: ไม่ต้องจ่ายค่า subscription service
  • ทำงานได้แม้ไม่มี internet: ระบบ automation ยังทำงานได้

Zigbee Coordinator คืออุปกรณ์หลักใน network ของ Zigbee ที่ทำหน้าที่ควบคุมและจัดการอุปกรณ์ Zigbee อื่นๆ โดย ESP32-C6 มีความสามารถพิเศษคือรองรับ IEEE 802.15.4 radio ซึ่งเป็นมาตรฐานสำหรับ Zigbee และ Thread

ESP32-C6 Features

  • • WiFi 6 (802.11ax)
  • • Bluetooth 5.0
  • Zigbee/Thread (802.15.4)
  • • RISC-V Processor
  • • Low Power Consumption

Use Cases

  • • Smart Lighting
  • • Sensor Networks
  • • Smart Switches
  • • Climate Control
  • • Security Systems

อุปกรณ์ที่ต้องใช้

📦 Hardware

อุปกรณ์รายละเอียดราคาประมาณ
ESP32-C6 Boardรองรับ Zigbee และ WiFi (เช่น XIAO ESP32-C6)฿300-500
USB Cableสำหรับ flash firmware฿50
Zigbee Devicesหลอดไฟ, sensor, switch ฯลฯแล้วแต่รุ่น

💻 Software

  • Zigbee2MQTT:Software สำหรับแปลง Zigbee signals เป็น MQTT messages
  • MQTT Broker:เช่น Mosquitto หรือ EMQX (Home Assistant มี built-in)
  • Home Assistant (optional):สำหรับ dashboard และ automation
  • Python 3.x:สำหรับไฟล์ configuration.py

ทำความเข้าใจ Zigbee Network

ก่อนเริ่มต้น จำเป็นต้องเข้าใจ Zigbee Network Topology กันก่อน

👑

Coordinator

จุดศูนย์กลางของ network ควบคุมทุกอย่าง มีเพียง 1 ตัว ต่อ 1 network

🔄

Router

ขยายระยะสัญญาณและเชื่อมต่ออุปกรณ์อื่น มีได้หลายตัว

💡

End Device

อุปกรณ์ที่ต่อเข้ากับ network ประหยัดพลังงาน

ตัวอย่าง Zigbee Network จริง:

Coordinator: ESP32-C6 (1 ตัว)
    ├─ Router: Smart Bulb #1
    │   └─ End Device: Temperature Sensor
    ├─ Router: Smart Switch
    │   └─ End Device: Motion Sensor
    └─ End Device: Door Sensor

การติดตั้ง Zigbee2MQTT

📥 Step 1: ติดตั้ง Zigbee2MQTT

ใช้ Docker เพื่อการติดตั้งที่ง่ายที่สุด:

# สร้าง docker-compose.yml
version: '3.8'
services:
  zigbee2mqtt:
    container_name: zigbee2mqtt
    image: koenkk/zigbee2mqtt
    restart: unless-stopped
    volumes:
      - ./zigbee2mqtt-data:/app/data
      - /run/udev:/run/udev:ro
    devices:
      # แก้ไขให้ตรงกับ ESP32-C6 port
      - /dev/ttyUSB0:/dev/ttyUSB0
    environment:
      - TZ=Asia/Bangkok

หมายเหตุ: แก้ไข /dev/ttyUSB0 ให้ตรงกับ port ของ ESP32-C6 ของคุณ ใช้คำสั่ง ls /dev/ttyUSB* หรือ ls /dev/ttyACM* เพื่อตรวจสอบ

⚙️ Step 2: ตั้งค่า Configuration

สร้างไฟล์ configuration.yaml ในโฟลเดอร์ zigbee2mqtt-data:

# zigbee2mqtt-data/configuration.yaml

# Home Assistant integration (optional)
homeassistant: true

# MQTT settings
mqtt:
  base_topic: zigbee2mqtt
  server: 'mqtt://localhost'  # แก้ไขถ้า MQTT broker อยู่คนละเครื่อง
  user: mqtt_user  # optional
  password: mqtt_password  # optional

# Serial settings (ESP32-C6)
serial:
  port: /dev/ttyUSB0
  # ใช้ EZSP/EMP สำหรับ ESP32-C6
  adapter: ezsp

# Frontend settings (optional)
frontend:
  port: 8080

# Advanced settings
advanced:
  log_level: info
  pan_id: 0x1234
  channel: 11

# Device whitelist (optional - เฉพาะอุปกรณ์ที่อนุญาต)
permit_join: true  # อนุญาตให้เพิ่มอุปกรณ์ใหม่

การตั้งค่า ESP32-C6

🔧 Flash Firmware สำหรับ Zigbee

สำหรับ ESP32-C6 ต้อง flash firmware ที่รองรับ Zigbee (ใช้ ESP-Zigbee-SDK):

1

ดาวน์โหลด ESP-Zigbee-SDK

git clone --recursive https://github.com/espressif/esp-zigbee-sdk.git
cd esp-zigbee-sdk
2

Flash Zigbee Coordinator Firmware

# ใช้ examples/ zigbee_coordinator
cd examples/zigbee_coordinator

# Set target
idf.py set-target esp32c6

# Flash firmware
idf.py flash
idf.py monitor

💡 Tip: หากต้องการใช้ Zigbee2MQTT กับ ESP32-C6 โดยตรง ให้ใช้ firmware จาก Zigbee2MQTT firmware builder หรือใช้ USB Coordinator แทน

✅ ตรวจสอบการเชื่อมต่อ

เริ่ม Zigbee2MQTT container และตรวจสอบ logs:

# เริ่ม container
docker-compose up -d zigbee2mqtt

# ตรวจสอบ logs
docker logs -f zigbee2mqtt

# ควรเห็น output ประมาณนี้:
# INFO: Currently connected to 'mqtt://localhost'.

การจับคู่อุปกรณ์ Zigbee

🔗 Pairing Mode

ตั้งค่าให้ Zigbee2MQTT อยู่ใน pairing mode:

# แบบที่ 1: ผ่าน MQTT
mosquitto_pub -t 'zigbee2mqtt/bridge/request/permit_join' -m '{"value": true}'

# แบบที่ 2: ผ่าน frontend (เปิด port 8080)
# เข้าไปที่ http://localhost:8080 แล้วกด "Enable Join"

# แบบที่ 3: แก้ไข configuration.yaml
permit_join: true
# แล้ว restart container

ขั้นตอนการจับคู่:

  1. เปิด pairing mode บน Zigbee2MQTT
  2. กดปุ่มค้าง 3-5 วินาที บนอุปกรณ์ Zigbee (หลอดไฟ, sensor, etc.)
  3. รอสักครู่ อุปกรณ์จะปรากฏใน logs
  4. ปิด pairing mode หลังจากเสร็จสิ้น

📱 ตรวจสอบอุปกรณ์ที่เชื่อมต่อ

# ดูอุปกรณ์ทั้งหมด
mosquitto_sub -t 'zigbee2mqtt/bridge/devices'

# ตัวอย่าง response:
{
  "ieeeAddr": "0x00158d000123456",
  "type": "EndDevice",
  "nwkAddr": 12345,
  "model": "TS0201",
  "friendly_name": "0x00158d000123456"
}

✏️ ตั้งชื่อ Friendly Name

แก้ไข configuration.yaml เพื่อตั้งชื่อที่จำง่าย:

devices:
  '0x00158d000123456':
    friendly_name: living_room_temp_sensor
    retain: true

เชื่อมต่อ Home Assistant

🏠 Step 1: เปิดใช้งาน Home Assistant Integration

ใน configuration.yaml ของ Zigbee2MQTT:

homeassistant: true

จากนั้น restart Zigbee2MQTT:

docker-compose restart zigbee2mqtt

⚙️ Step 2: ตั้งค่า MQTT Integration ใน Home Assistant

  1. 1
    Settings → Devices & Services → Add Integration
  2. 2
    ค้นหา "MQTT" และคลิก
  3. 3
    กรอกข้อมูล:
    • Broker: localhost (หรือ IP ของ MQTT broker)
    • Port: 1883
    • Username/Password: (ถ้ามี)
  4. 4
    อุปกรณ์ Zigbee จะปรากฏอัตโนมัติ ใน Home Assistant

✅ ตรวจสอบอุปกรณ์ใน Home Assistant

ไปที่ Settings → Devices & Services คุณจะเห็นอุปกรณ์ Zigbee ทั้งหมด:

  • Temperature Sensor (ตรวจจับอุณหภูมิ)
  • Smart Bulb (หลอดไฟอัจฉริยะ)
  • Smart Switch (สวิตช์อัจฉริยะ)

ตัวอย่าง Automation

💡 Automation 1: เปิดไฟอัตโนมัติเมื่อเคลื่อนไหว

ใน Home Assistant (configuration.yaml หรือ UI):

automation:
  - alias: "เปิดไฟเมื่อมีคนเคลื่อนไหว"
    trigger:
      - platform: state
        entity_id: sensor.living_room_motion
        to: 'on'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
      - service: light.turn_on
        entity_id: light.living_room_bulb
        data:
          brightness_pct: 80

🌡️ Automation 2: แจ้งเตือนเมื่ออุณหภูมิสูง

automation:
  - alias: "แจ้งเตือนอุณหภูมิสูง"
    trigger:
      - platform: numeric_state
        entity_id: sensor.living_room_temperature
        above: 30
    action:
      - service: notify.mobile_app
        data:
          message: "อุณหภูมิในห้องนั่งเล่นสูงกว่า 30°C"

🎯 Automation 3: ควบคุมผ่าน MQTT โดยตรง

สามารถส่งคำสั่ง MQTT โดยตรงโดยไม่ผ่าน Home Assistant:

# เปิดไฟ
mosquitto_pub -t 'zigbee2mqtt/living_room_bulb/set' -m '{"state": "ON"}'

# ปิดไฟ
mosquitto_pub -t 'zigbee2mqtt/living_room_bulb/set' -m '{"state": "OFF"}'

# ปรับความสว่าง
mosquitto_pub -t 'zigbee2mqtt/living_room_bulb/set' -m '{"brightness": 200}'

การแก้ปัญหาที่พบบ่อย

อุปกรณ์ Zigbee เชื่อมต่อไม่ได้

อาการ: Device ไม่ปรากฏใน logs หลังจากกดปุ่มค้าง

สาเหตุ: Battery หมด, อุปกรณ์อยู่ไกลเกินไป, หรือ interference

วิธีแก้: แน่ใจว่า device อยู่ใกล้ coordinator, เปลี่ยน battery, ลอง restart device

Zigbee2MQTT เชื่อมต่อ MQTT ไม่ได้

อาการ: Logs แสดง "MQTT connection failed"

สาเหตุ: MQTT broker ไม่ทำงาน, username/password ผิด

วิธีแก้: ตรวจสอบว่า MQTT broker ทำงานอยู่, แก้ไข credentials ใน configuration.yaml

Home Assistant ไม่เห็นอุปกรณ์

อาการ: Device ไม่ปรากฏใน HA แม้ว่าจะมีใน Zigbee2MQTT

สาเหตุ: Home Assistant integration ไม่ได้เปิด, MQTT broker config ผิด

วิธีแก้: ตรวจสอบ homeassistant: true, restart Zigbee2MQTT

สรุป

สิ่งที่เรียนรู้

  • ติดตั้งและตั้งค่า Zigbee2MQTT บน Docker
  • เข้าใจ Zigbee Network Topology (Coordinator, Router, End Device)
  • จับคู่อุปกรณ์ Zigbee กับ ESP32-C6
  • เชื่อมต่อ Zigbee2MQTT เข้ากับ Home Assistant
  • สร้าง automation ด้วย MQTT และ Home Assistant

🚀 ถัดไป

พร้อมสร้าง Smart Home ของคุณแล้วหรือยัง?

สำรวจบทความเพิ่มเติมและเริ่มต้นสร้างโปรเจกต์ IoT ของคุณกับ CynoIoT

ดูบทความทั้งหมด →