道理简介
这个智能掌握体系采纳 ZigBee 作为无线通讯协定。在支撑 OpenWRT 体系的路由器上刷入 Ruff,应用 ZigBee-USB dongle 模块,和其他 ZigBee 终端设备通讯,完成遥控功用。
物件清单
主控平台
极路由3 ,已刷入 Ruff。
传感器和实行元件
ZigBee-USB dongle 模块
ZigBee 灯
小米 ZigBee 开关
开辟步骤
初始化 APP $ rap init
增加 zigbee 驱动,id 为 zigbee ,型号挑选 jn5168a $ rap device add zigbee (jn5168a)
编写 src/index.js代码
'use strict';var zigbee;$.ready(function (error) { if (error) { console.log(error); return; } zigbee = $('#zigbee'); zigbee.startup(); zigbee.setTurnLightOn(); zigbee.setTurnLightOff(); zigbee.setToggleLight();});$.end(function () {});
改写板卡形貌 board.json
因为路由器上没有 GPIO 之类的接口,所以我们要改写板卡形貌文件 ruff_modules/ruff-mbd-v1/board.json。
{ "version": "2.0", "id": "ruff-mbd-v1", "model": "ruff-mbd-v1", "preloads": { "uart-0": "uart-0/uart" }, "outputs": { "uart-0": "uart-0/uart", "gnd-0": "ground/gnd-0", "vdd-0": "power/vdd-0" }, "devices": [ { "id": "ground", "outputs": { "gnd-0": { "type": "ground" } } }, { "id": "power", "outputs": { "vdd-0": { "type": "power", "args": { "voltage": "3.3v" } } } }, { "id": "uart-0", "model": "ruff-sys-uart", "driver": "sys-uart", "inputs": { "device": { "type": "string", "args": { "path": "/dev/ttyUSB0" } } }, "outputs": { "uart" : { "type":"uart" } } } ] }
5.布置代码
`$ rap deploy your-router-ip -s`
ZigBee 驱动剖析
以下三个部份分别从
ZigBee 收集包的构成
ZigBee 收集的组建
开关灯掌握
三个方面,来引见 ZigBee 驱动模块。
ZigBee 组包、解包和剖析
以开关灯的掌握协定来看 ZigBee 的组包历程:
start = 0x1 end = 0x3 data = [0x2, 0x44, 0xa6, 0x1, 0x1, 0x1] msgType = 146 = 0x92 (OnOff) msgLen = 0x6 crc = 0x92 ^ 0x6 ^ 0x2 ^ 0x44 ^ 0xa6 ^ 0x1 ^ 0x1 ^ 0x1 = 0x75 ----------------------------------------------------------------------------------- | 0x1 | 0x92 | 0x6 | 0x75 | 0x2, 0x44, 0xa6, 0x1, 0x1, 0x1 | 0x3 | ----------------------------------------------------------------------------------- | start | msgType | msgLen | crc | Data | stop | -----------------------------------------------------------------------------------
包的首尾是最先和完毕标志。前三个字段分别是message type, message length, CRC checksum, 以后就是详细的掌握数据。因为要在包内容里防止运用开首或许末端的标志字段,所以须要对源包内容举行转义,转义表示以下。
0x00 0x92 -> 0x2 0x10^0x00 0x92 ------------------------------------------------------------------------------------------------------------ | 0x1 | 0x2 0x10 0x92 | 0x2 0x10 0x2 0x16 | 0x75 | 0x2 0x12 0x44 0xa6 0x2 0x11 0x2 0x11 0x2 0x11 | 0x3 | ------------------------------------------------------------------------------------------------------------ | start | msgType | msgLen | crc | Data | stop | ------------------------------------------------------------------------------------------------------------
解包是组包的逆历程。剖析而是依据ZigBee协定的数据手册(JN-AN-1194-ZigBee-IoT-Gateway-Control-Bridge pdf),一一对照着剖析包的内容。
ZigBee收集组建代码
依据数据手册,ZigBee初始化组建收集须要经由以下几个步骤:
this.getVersion(); this.setExtendedPANID(); this.setChannelMask(); this.setSecurityStateAndKey(); this.setDeviceType(); this.startNetwork(); this.permitJoiningRequest();
在这以后,ZigBee 收集处于开放状况,能够接收衔接要求。我们就能够接入上面展现的 ZigBee 灯和开关了。
ZigBee 开关灯代码
ZigBee 协定里有直接掌握开关的通讯范例,经由过程查找 ZigBee 数据手册,能够找到以下信息:
应用这个通讯范例,我们写的开关灯的代码是如许的。
ZigBee.prototype.turnLightOn = function () { console.log('turn light on'); var devices = interpreter.getDeviceList(); var msg = new Buffer([0x2, 0xff, 0xff, 0x1, 0x1, 0x1]); if (devices.length !== 0) { msg.writeUInt16BE(devices[0].shortAddress, 1); } this._writeCmd(0x92, msg);}ZigBee.prototype.turnLightOff = function () { console.log('turn light off'); var devices = interpreter.getDeviceList(); var msg = new Buffer([0x2, 0xff, 0xff, 0x1, 0x1, 0x0]); if (devices.length !== 0) { msg.writeUInt16BE(devices[0].shortAddress, 1); } this._writeCmd(0x92, msg);}ZigBee.prototype.toggleLight = function () { console.log('toggle light'); var devices = interpreter.getDeviceList(); var msg = new Buffer([0x2, 0xff, 0xff, 0x1, 0x1, 0x2]); if (devices.length !== 0) { msg.writeUInt16BE(devices[0].shortAddress, 1); } this._writeCmd(0x92, msg);}
演示视频
演示(腾讯)