Krittapon's github web
Zigbee2MQTT เป็นเสมือนตัวกลางในการสื่อสารระหว่างอุปกรณ์ ZigBee และ Protocal MQTT โดยมีอุปกรณ์ประเภท ZigBee Coordinator - USB Adapter / Dongle เป็นอุปกรณ์ที่ทำให้คอมพิวเตอร์สามารถสื่อสารได้
เป็นอุปกรณ์ที่ใช้เชื่อมต่อกับอุปกรณ์ Zigbee เข้ากับคอมพิวเตอร์ โดยบทความนี้จะยกตัวอย่างการใช้งานกับ CC2531 USB Dongle
รูปภาพ CC2531 USB Dongle
$ sudo apt install -y curl
$ curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
$ sudo apt install -y nodejs
# Clone the Zigbee2MQTT repository.
$ git clone https://github.com/Koenkk/zigbee2mqtt.git
# Move the zigbee2mqtt directory to /opt
$ sudo mv zigbee2mqtt /opt
# Change the owner of the /opt/zigbee2mqtt directory.
$ sudo chown -R $USER:$USER /opt/zigbee2mqtt
# Change the current directory to '/opt/zigbee2mqtt'.
$ cd /opt/zigbee2mqtt
# Install package dependencies
$ npm ci
data/configuration.yaml
ภายใน Directory /opt/zigbee2mqtt
เพื่อตั้งค่าการใช้งานสำหรับ Zigbee2MQTT $ nano data/configuration.yaml
เขียนข้อความต่อไปนี้ในไฟล์
homeassistant: false
permit_join: true
mqtt:
base_topic: zigbee2mqtt
## specify the IP address or hostname of your MQTT broker
server: 'mqtt://localhost:1883'
#user: xxxx
#password: xxxx
serial:
## specify the serial COM port (e.g. /dev/ttyACM0, /dev/ttyUSB0)
port: /dev/ttyACM0
advanced:
## specify the ZigBee channel number
channel: 15
frontend:
## enable Web front-end for Zigbee2MQTT on port 8080
port: 8080
# Add the current user to the 'dialout' group
$ sudo usermod -a -G dialout $USER
# Start ZigBee2MQTT
$ npm start
รูปภาพเมื่อเริ่มทำงานเรียบร้อย
การนำอุปกรณ์ Zigbee โดยในบทความนี้จะใช้อุปกรณ์ที่สามารถวัดค่าความสว่างให้แสดงผลที่ Node-red Dashboard โดยที่ Node-red Dashboard เป็น UI ในการแสดงผลต่างๆเบื้องต้น โดยมีขั้นตอนในการติดต้ั้งดังนี้
node-red-dashboard
แล้วกด install-red
msg.payload = msg.payload.illuminance_lux
return msg;
โค้ด Flow การทำงานในไฟล์ json
[
{
"id": "fc0e24ce8c0e468f",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "a461a0248c86f96d",
"type": "mqtt in",
"z": "fc0e24ce8c0e468f",
"name": "Subscriber",
"topic": "zigbee2mqtt",
"qos": "2",
"datatype": "auto-detect",
"broker": "373c28f3ff7a7b84",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 240,
"y": 340,
"wires": [
[
"1f5f03c1a582343d"
]
]
},
{
"id": "1f5f03c1a582343d",
"type": "function",
"z": "fc0e24ce8c0e468f",
"name": "json to guage",
"func": "msg.payload = msg.payload.illuminance_lux;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 480,
"y": 340,
"wires": [
[
"c2de2a025b732fb4"
]
]
},
{
"id": "c2de2a025b732fb4",
"type": "ui_gauge",
"z": "fc0e24ce8c0e468f",
"name": "",
"group": "aa9e992a3df8f0e6",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "Illuminance Lux",
"label": "lux",
"format": "",
"min": 0,
"max": "1000",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"className": "",
"x": 740,
"y": 340,
"wires": []
},
{
"id": "373c28f3ff7a7b84",
"type": "mqtt-broker",
"name": "Mosquitto",
"broker": "localhost",
"port": "1883",
"clientid": "nodered_demo",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "aa9e992a3df8f0e6",
"type": "ui_group",
"name": "Illuminance",
"tab": "3337181993a23654",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "3337181993a23654",
"type": "ui_tab",
"name": "Home",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]