Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mqtt=require('mqtt');
- var client = mqtt.connect('mqtt://127.0.0.1')
- client.on('connect', function () {
- client.subscribe('presence', function (err) {
- if (!err) {
- client.publish('presence', 'Hello mqtt')
- }
- })
- // client.subscribe('zigbee2mqtt/0x00158d0002008a2c', function (err) {
- client.subscribe('zigbee2mqtt/0x00158d0002008a2c', function (err) {
- if (!err) {
- console.log("sub");
- }
- })
- })
- BEDROOM="0xb0ce181403361b26"
- LIVINGROOM="0xb0ce1814031ff1e1"
- COMPUTER_A="0xb0ce18140334aabd"
- COMPUTER_B="0xb0ce181403361097"
- computer_brightness=255
- msg = null
- client.on('message', function (topic, message) {
- // message is Buffer
- console.log(message.toString())
- try {
- msg = JSON.parse(message.toString())
- } catch(e){
- console.log(e)
- return
- }
- if(msg.click == "single"){
- console.log("click")
- if(computer_brightness == 255){
- computer_brightness = 0;
- } else {
- computer_brightness = 255;
- }
- client.publish('zigbee2mqtt/'+COMPUTER_A+"/set", JSON.stringify({ "brightness": computer_brightness, "transition": 1 }))
- }
- // client.end()
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement