-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
executable file
·59 lines (46 loc) · 1.46 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
var mqtt = require('mqtt')
// Solution to bypass firewall restrictions with port 1883
const MQTT_SERVER = '192.168.8.215'
const MQTT_PORT = 80
//const MQTT_SERVER = 'test.mosquitto.org'
//const MQTT_PORT = 1883
const MQTT_TOPIC_TEMPERATURE = 'iescelia/aula1/temperature'
const MQTT_TOPIC_HUMIDITY = 'iescelia/aula1/humidity'
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://' + MQTT_SERVER + ":" + MQTT_PORT)
var fs = require('fs');
var express = require('express');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
fs.readFile(__dirname + '/public/index.html', function (error, data) {
if (error) {
res.writeHead(404);
res.write("Not Found");
res.end();
} else {
res.writeHead(200);
res.write(data, "utf8");
res.end();
}
});
});
io.sockets.on('connection', function(socket) {
});
client.on('connect', function () {
client.subscribe(MQTT_TOPIC_TEMPERATURE);
client.subscribe(MQTT_TOPIC_HUMIDITY);
})
client.on('message', function (topic, message) {
console.log(topic);
console.log(message.toString());
if (topic == MQTT_TOPIC_TEMPERATURE) {
io.sockets.emit('temperature', { raw: message.toString() });
}
if (topic == MQTT_TOPIC_HUMIDITY) {
io.sockets.emit('humidity', { raw: message.toString() });
}
})
console.log('Server app listening on port 3000...');