forked from fabaff/mqtt-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (71 loc) · 2.11 KB
/
server.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
/**
* Copyright (c) 2013-2016, Fabian Affolter <[email protected]>
* Released under the MIT license. See LICENSE file for details.
*/
var mqtt = require('mqtt');
var socket = require('socket.io');
//var firmata = require('firmata');
var mqttbroker = 'localhost';
var mqttport = 1883;
var io = socket.listen(3000);
var mqttclient = mqtt.createClient(mqttport, mqttbroker);
// Subscribe to topic
io.sockets.on('connection', function (socket) {
socket.on('subscribe', function (data) {
mqttclient.subscribe(data.topic);
});
});
// Push the message to socket.io
mqttclient.on('message', function(topic, payload) {
io.sockets.emit('mqtt',
{'topic' : topic,
'payload' : payload
}
);
});
// Setup the arduino
//var board = new firmata.Board('/dev/ttyACM0', function(err) {
// if (err) {
// console.log(err);
// return;
// }
// board.pinMode(0, board.MODES.INPUT);
// board.pinMode(1, board.MODES.INPUT);
// board.pinMode(2, board.MODES.INPUT);
// board.pinMode(5, board.MODES.INPUT);
// // Analog pin 0
// var oldVal1;
// board.analogRead(0, function(val) {
// // Reduce the amount of messages
// if (val != oldVal1 && val != oldVal1 + 1 && val != oldVal1 - 1) {
// mqttclient.publish('home/living/temp', String(val));
// }
// oldVal1 = val;
// });
// // Analog pin 5
// var oldVal1;
// board.analogRead(5, function(val) {
// // Reduce the amount of messages
// if (val != oldVal1 && val != oldVal1 + 1 && val != oldVal1 - 1) {
// mqttclient.publish('home/basement/temp', String(val));
// }
// oldVal1 = val;
// });
// // Digital pin 2
// board.digitalRead(2, function(val) {
// mqttsend('home/front/door', val);
// });
// // Digital pin 3
// board.digitalRead(3, function(val) {
// mqttsend('home/back/door', val);
// });
//});
//function mqttsend(topic, val) {
// if (val == 1) {
// state = 'true';
// } else {
// state = 'false';
// }
// mqttclient.publish(topic, state);
//};