forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti-light-switch.js
executable file
·50 lines (42 loc) · 1.12 KB
/
multi-light-switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var mqtt = require('mqttjs'),
Gpio = require('onoff').Gpio,
button = new Gpio(14, 'in', 'both'),
led1 = new Gpio(23, 'out'),
led2 = new Gpio(24, 'out'),
state = 1;
var serverIP = '192.168.1.245';
mqtt.createClient(2883, serverIP, function(err, client) {
if (err) process.exit(1);
function setLight() {
console.log('switch ' + state);
led1.writeSync(state % 2 ? 0 : 1);
led2.writeSync(state % 3 ? 0 : 1);
client.publish({topic: 'switch state', payload: JSON.stringify(state)} );
button.watch(function (err, value) {
if (err) throw err;
state++;
if (state > 3)
state = 0;
setLight();
});
}
client.connect({keepalive: 3000});
console.log('connect');
client.on('connack', function(packet) {
if (packet.returnCode === 0) {
console.log('connected '+JSON.stringify(packet));
}
else {
console.log('connack error %d', packet.returnCode);
process.exit(-1);
}
});
client.on('close', function() {
process.exit(0);
});
client.on('error', function(e) {
console.log('error %s', e);
process.exit(-1);
});
setLight();
});