-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubscriber.js
45 lines (33 loc) · 1.25 KB
/
subscriber.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
const mqtt = require('mqtt')
var client = mqtt.connect('http://vpn.ce.pdn.ac.lk:8883');
client.on('connect', function() {
client.subscribe('326/sensor/temp');
console.log('CLient has subscribed successfully!');
});
const tempControlTopic = '326/control/temp';
const tempCanChange = 2;
const tempThreashold = 32;
client.on('message', function(topic, message) {
var data = JSON.parse(message);
// data validation
dataSize = Object.keys(data).length;
if (dataSize =! 2 || Object.keys(data)[0] != 'time' || Object.keys(data)[1] != 'temp') {
return
}
else {
console.log('Do process');
temperature = data['temp'];
if (temperature < (tempThreashold - tempCanChange)) {
client.publish(tempControlTopic, "Provide Hot Air")
console.log("published 'Provide Hot Air' to topic " + tempControlTopic)
}
else if (temperature > (tempThreashold + tempCanChange)) {
client.publish(tempControlTopic, "Provide Cold Air")
console.log("published 'Provide Cold Air' to topic " + tempControlTopic)
}
else {
client.publish(tempControlTopic, "Turn OFF")
console.log("Maintainig current temperature levels")
}
}
});