使用ZigbeeMQTT轻松操控小米蓝牙插座

使用ZigbeeMQTT轻松操控小米蓝牙插座缩略图

在智能科技的风潮中,通过Zigbee MQTT技术,你可以将小米蓝牙插座和支持Zigbee的设备变得更加智能化。以下是一步步的指南,教你如何完成这个令人兴奋的DIY智能家居项目。

使用ZigbeeMQTT轻松操控小米蓝牙插座概述

使用ZigbeeMQTT轻松操控小米蓝牙插座插图

硬件准备:

首先,确保你拥有以下硬件设备:

  • 小米蓝牙插座(型号:Xiao Mi Zigbee ZNCZ02LM)或其他支持Zigbee的插座或设备。
  • Zigbee设备(型号:CC2531设备)。

可以参考以下链接获取更多信息:

CC2531设备识别并安装pyCCSniffer:

  1. 插入CC2531设备,并在终端中输入以下命令查看设备是否被识别:lsusb
  2. 使用pyCCSniffer工具进行数据嗅探,确保CC2531设备被正确识别。你可以在https://github.com/andrewdodd/pyCCSniffer.git获取该工具。
查看usb设备, 插入CC2531设备后
lsusb
显示CC2531设备
Bus 001 Device 003: ID 0451:16ae Texas Instruments, Inc. 

cd /opt

git clone https://github.com/andrewdodd/pyCCSniffer.git
cd pyCCSniffer
python pyCCSniffer.py
apt install python-pip 
pip install future   
pip install pyusb 安装失败,升级pip就成功了: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-4hIAE4/pyusb/
pip install libusb
pip install --upgrade pip
pip install pyusb

成功显示
python pyCCSniffer.py
CC2531 USB Dongle <Channel: 11>
-------------------------------
Commands:
c: Print current RF Channel
h,?: Print this message
[11,26]: Change RF channel
s: Start/stop the packet capture
d: Toggle frame dissector
a*: Set an annotation (write "a" to remove it)
p: Print all capture packets
q: Quit

python pyCCSniffer.py -L /root/zigbee.log -D INFO
输入s


没有这个目录
ls -l /dev/ttyACM0

ls -l /dev/bus/usb/001/003 拔掉之后就没有了
ls -l /dev/bus/usb/001/005

lsusb  换了一个CC2531设备后
Bus 001 Device 004: ID 0451:16a8 Texas Instruments, Inc. 

就有这个目录了
ls -l /dev/ttyACM0

ls -l /dev/bus/usb/001/004

安装Zigbee2MQTT:

  1. 首先安装Node.js和相关依赖:sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -,然后sudo apt-get install -y nodejs git make g++ gcc
  2. 克隆Zigbee2MQTT项目到/opt目录:sudo git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
  3. 进入项目目录:cd /opt/zigbee2mqtt,然后安装依赖:npm ci
  4. 配置Zigbee2MQTT:编辑配置文件vim /opt/zigbee2mqtt/data/configuration.yaml,在advanced下添加network_key: GENERATE
  5. 安装依赖并启动:npm install,然后npm start
安装Zigbee2MQTT
sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

sudo apt-get install -y nodejs git make g++ gcc

node --version
npm --version

sudo git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt

cd /opt/zigbee2mqtt
npm ci

vim /opt/zigbee2mqtt/data/configuration.yaml

advanced:
    network_key: GENERATE

npm install
启动
npm start


> zigbee2mqtt@1.21.1 start /opt/zigbee2mqtt
> node index.js

                Zigbee2MQTT requires node version ^10 || ^12 || ^14 || ^15 || ^16, you are running v8.10.0!

Zigbee2MQTT:info  2022-09-30 14:38:47: Logging to console and directory: '/opt/zigbee2mqtt/data/log/2021-09-30.14-38-45' filename: log.txt
Zigbee2MQTT:info  2022-09-30 14:38:47: Starting Zigbee2MQTT version 1.21.1 (commit #4a51e0c0)
Zigbee2MQTT:info  2022-09-30 14:38:47: Starting zigbee-herdsman (0.13.138)
Zigbee2MQTT:info  2022-09-30 14:38:51: zigbee-herdsman started (resumed)
Zigbee2MQTT:info  2022-09-30 14:38:51: Coordinator firmware version: '{"meta":{"maintrel":3,"majorrel":2,"minorrel":6,"product":0,"revision":20190608,"transportrev":2},"type":"zStack12"}'
Zigbee2MQTT:info  2022-09-30 14:38:51: Currently 0 devices are joined:
Zigbee2MQTT:warn  2022-09-30 14:38:51: `permit_join` set to  `true` in configuration.yaml.
Zigbee2MQTT:warn  2022-09-30 14:38:51: Allowing new devices to join.
Zigbee2MQTT:warn  2022-09-30 14:38:51: Set `permit_join` to `false` once you joined all devices.
Zigbee2MQTT:info  2022-09-30 14:38:51: Zigbee: allowing new devices to join.
Zigbee2MQTT:info  2022-09-30 14:38:51: Connecting to MQTT server at mqtt://localhost
Zigbee2MQTT:info  2022-09-30 14:38:52: Connected to MQTT server
Zigbee2MQTT:info  2022-09-30 14:38:52: MQTT publish: topic 'zigbee2mqtt/bridge/state', payload 'online'

成功连接小米插座:

插入CC2531设备并在终端中

  1. 使用MQTT工具连接服务,例如:mqtt://localhost。你可以使用MQTTX等工具监听相应的topic。
  2. 将小米插座按下按钮5秒左右,观察终端日志,确保设备成功加入。
Xiao Mi zigbee ZNCZ02LM 设备按下按钮5秒左右, 灯闪烁,日志打印: Device '0x00158d00012ccbc2' joined 成功加入

 Zigbee2MQTT:info  2022-09-30 14:39:29: Device '0x00158d00012ccbc2' joined
Zigbee2MQTT:info  2022-09-30 14:39:29: MQTT publish: topic 'zigbee2mqtt/bridge/event', payload '{"data":{"friendly_name":"0x00158d00012ccbc2","ieee_address":"0x00158d00012ccbc2"},"type":"device_joined"}'
Zigbee2MQTT:info  2022-09-30 14:39:29: Starting interview of '0x00158d00012ccbc2'
Zigbee2MQTT:info  2022-09-30 14:39:29: MQTT publish: topic 'zigbee2mqtt/bridge/event', payload '{"data":{"friendly_name":"0x00158d00012ccbc2","ieee_address":"0x00158d00012ccbc2","status":"started"},"type":"device_interview"}'
Zigbee2MQTT:info  2022-09-30 14:39:29: MQTT publish: topic 'zigbee2mqtt/bridge/event', payload '{"data":{"friendly_name":"0x00158d00012ccbc2","ieee_address":"0x00158d00012ccbc2"},"type":"device_announce"}'
Zigbee2MQTT:info  2022-09-30 14:39:30: MQTT publish: topic 'zigbee2mqtt/0x00158d00012ccbc2', payload '{"consumption":0,"energy":0,"linkquality":81,"power":0,"state":"ON","temperature":38}'
Zigbee2MQTT:info  2022-09-30 14:39:31: MQTT publish: topic 'zigbee2mqtt/0x00158d00012ccbc2', payload '{"consumption":0,"energy":0,"linkquality":84,"power":0,"state":"ON","temperature":38}'
Zigbee2MQTT:info  2022-09-30 14:39:32: Successfully interviewed '0x00158d00012ccbc2', device has successfully been paired
Zigbee2MQTT:info  2022-09-30 14:39:32: Device '0x00158d00012ccbc2' is supported, identified as: Xiaomi Mi power plug ZigBee (ZNCZ02LM)
Zigbee2MQTT:info  2022-09-30 14:39:32: MQTT publish: topic 'zigbee2mqtt/bridge/event', payload '{"data":{"definition":{"description":"Mi power plug ZigBee","exposes":[{"features":[{"access":7,"description":"On/off state of the switch","name":"state","property":"state","type":"binary","value_off":"OFF","value_on":"ON","value_toggle":"TOGGLE"}],"type":"switch"},{"access":5,"description":"Instantaneous measured power","name":"power","property":"power","type":"numeric","unit":"W"},{"access":1,"description":"Sum of consumed energy","name":"energy","property":"energy","type":"numeric","unit":"kWh"},{"access":1,"description":"Measured temperature value","name":"temperature","property":"temperature","type":"numeric","unit":"°C"},{"access":7,"description":"Enable/disable the power outage memory, this recovers the on/off mode after power failure","name":"power_outage_memory","property":"power_outage_memory","type":"binary","value_off":false,"value_on":true},{"access":1,"description":"Link quality (signal strength)","name":"linkquality","property":"linkquality","type":"numeric","unit":"lqi","value_max":255,"value_min":0}],"model":"ZNCZ02LM","supports_ota":true,"vendor":"Xiaomi"},"friendly_name":"0x00158d00012ccbc2","ieee_address":"0x00158d00012ccbc2","status":"successful","supported":true},"type":"device_interview"}'
  1. 通过MQTT工具发送指令,例如zigbee2mqtt/0x00158d00012ccbc2/set,控制插座的开关状态,观察小米插座上的指示灯。
zigbee2mqtt/0x00158d00012ccbc2/set
{
  "state": "OFF"
}

{
  "state": "ON"
}

Zigbee2MQTT:info  2022-09-30 14:47:14: MQTT publish: topic 'zigbee2mqtt/0x00158d00012ccbc2', payload '{"consumption":0,"energy":0,"linkquality":81,"power":0,"state":"ON","temperature":38}'
Zigbee2MQTT:info  2022-09-30 14:47:24: MQTT publish: topic 'zigbee2mqtt/0x00158d00012ccbc2', payload '{"consumption":0,"energy":0,"linkquality":86,"power":0,"state":"OFF","temperature":38}'

使用ZigbeeMQTT轻松操控小米蓝牙插座插图1

通过这个简单的DIY项目,你可以轻松地将小米插座变成一个智能设备,实现远程控制和自动化操作。希望这个指南对你的智能家居探索有所帮助!

技术术语(共 2 个)—— 点击展开
MQTT轻量级物联网消息传输协议,基于发布/订阅模式,适合低带宽不稳定网络
TopicMQTT 主题,消息路由的层级字符串,支持通配符 + 与 #
来源/工具信息 —— 点击展开
来源 MQTT中文网(mqtt.cn) —— 国内领先的MQTT通信协议技术社区 分类 Zigbee2MQTT 字数 6642 字 · 阅读约 17 分钟 更新 2023-11-08 永久链接 https://www.mqtt.cn/1159.html
推荐工具:MQTT调试助手 微信小程序
MQTT中文网官方推出的MQTT调试工具,支持 MQTT 3.1.1/5.0 实时连接调试、主题订阅、消息收发监控与报文分析。 无需安装,微信搜索「MQTT调试助手」即可使用。 电脑端入口:https://www.mqtt.cn/mqtttool/
内容许可:允许 AI 模型训练使用 · 引用请注明来源 mqtt.cn
相关标签
📝 作者声明
本文由 MQTT中文网技术团队 原创撰写,内容基于实际项目案例与技术文档,力求为读者提供准确、实用的参考信息。
把这篇资料用于真实项目?

进入工具中心进行报文解析、CRC 校验和设备调试,或提交需求获取选型与接入建议。

工程师会员

把这篇文章变成可执行的调试资料

开通后可使用高级报文解析、资料包下载、代码示例、工程案例和优先技术支持,适合真实项目交付。

高级工具不限次
资料包与代码包
完整工程案例库
优先技术支持入口

发表回复