Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLinden committed Sep 27, 2017
1 parent d7cb4b5 commit b52bd61
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ typings/
# dotenv environment variables file
.env

package-lock.json
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# xiaomi-zb2mqtt
Xiaomi Zigbee to MQTT bridge using zigbee-shepherd

### To run the bridge

* Install
```sh
$ git clone https://github.com/AndrewLinden/xiaomi-zb2mqtt.git
$ cd xiaomi-zb2mqtt
/xiaomi-zb2mqtt$ npm install
```

* Run it
```sh
/xiaomi-zb2mqtt$ node index.js
```

* To see whats happening behind the scenes run it with debug enabled:
```sh
/xiaomi-zb2mqtt$ DEBUG=* node index.js
```
### Supports
* WXKG01LM
* WXKG02LM


### Notes
* You need to flash your CC2531 with CC2531ZNP-Pro-Secure_LinkKeyJoin.hex from here: https://github.com/mtornblad/zstack-1.2.2a.44539/tree/master/CC2531
* Zigbee shepherd's pairing process can take quite a while (more than a minute).
* When pairing WXKG01LM, after reset you need to toggle (short keypress) the reset button every couple of seconds to keep the switch from going to sleep until the pairing is complete.
72 changes: 72 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var util = require("util");
var perfy = require('perfy');
var ZShepherd = require('zigbee-shepherd');
var mqtt = require('mqtt')

var client = mqtt.connect('mqtt:https://192.168.1.10')

var shepherd = new ZShepherd('/dev/ttyACM3', {
net: {
panId: 0x1a61
}
});

shepherd.on('ready', function () {
console.log('Server is ready.');

// allow devices to join the network within 60 secs
shepherd.permitJoin(60, function (err) {
if (err)
console.log(err);
});
});

shepherd.on('permitJoining', function (joinTimeLeft) {
console.log(joinTimeLeft);
});

shepherd.on('ind', function (msg) {
switch (msg.type) {
case 'devIncoming':
console.log('Device: ' + msg.data + ' joining the network!');
break;
case 'attReport':
console.log('attreport: ' + msg.endpoints[0].device.ieeeAddr +' '+ msg.endpoints[0].devId +' '+ msg.endpoints[0].epId +' '+util.inspect(msg.data, false, null));
var topic = 'xiaomiZb/' + msg.endpoints[0].device.ieeeAddr.substr(2) + '/' + msg.endpoints[0].epId;
var pl = '1';
switch (msg.endpoints[0].devId) {
case 260: // WXKG01LM
if (msg.data.data['onOff'] == 0) { // click down
perfy.start(msg.endpoints[0].device.ieeeAddr); // start timer
pl = ''; // do not send mqtt message
} else if (msg.data.data['onOff'] == 1) { // click release
var clicktime = perfy.end(msg.endpoints[0].device.ieeeAddr); // end timer
if (clicktime.seconds > 0 || clicktime.milliseconds > 240) { // seems like a long press so ..
topic = 'xiaomiZb/' + msg.endpoints[0].device.ieeeAddr.substr(2) + '/2'; //change topic to 2
pl = clicktime.seconds+Math.floor(clicktime.milliseconds)+''; // and payload to elapsed seconds
}
} else if (msg.data.data['32768']) { // multiple clicks
pl = ''+msg.data.data['32768'];
}
}
if (pl.length > 0) { // only publish message if we have not set payload to null
client.publish(topic, pl);
}
break;
default:
// console.log(util.inspect(msg, false, null));
// Not deal with other msg.type in this example
break;
}
});

client.on('connect', function () {
client.publish('xiaomiZb', 'Bridge online')
})


shepherd.start(function (err) { // start the server
if (err)
console.log(err);
});

30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "xiaomi-zb2mqtt",
"version": "1.0.0",
"description": "Xiaomi Zigbee to MQTT bridge using zigbee-shepherd",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AndrewLinden/xiaomi-zb2mqtt.git"
},
"keywords": [
"xiaomi",
"zigbee",
"mqtt",
"cc2531"
],
"author": "Andrew Linden",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/AndrewLinden/xiaomi-zb2mqtt/issues"
},
"homepage": "https://github.com/AndrewLinden/xiaomi-zb2mqtt#readme",
"dependencies": {
"mqtt": "^2.13.0",
"perfy": "^1.1.2",
"zigbee-shepherd": "git+https://github.com/AndrewLinden/zigbee-shepherd.git"
}
}

0 comments on commit b52bd61

Please sign in to comment.