-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
42 lines (35 loc) · 1.1 KB
/
app.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
var sensorLib = require('node-dht-sensor');
var mqtt = require('mqtt');
var client = mqtt.connect({
port: 1883,
host: 'app.connectingthings.io',
keepalive: 10000,
protocolId: 'MQIsdp',
protocolVersion: 3
});
var sensor = {
initialize: function () {
return sensorLib.initialize(11, 4);
},
readTemperature: function () {
var readout = sensorLib.read();
var temp = readout.temperature.toFixed(0);
client.publish('/device/xxxx/key/xxxx', "{\"value\": \""+temp+"\",\"tag\": \"temperature\"}");
setTimeout(function () {
sensor.readHumidity();
}, 5000);
},
readHumidity: function () {
var readout = sensorLib.read();
var humidity = readout.humidity.toFixed(0) ;
client.publish('/device/xxxx/key/xxxx', "{\"value\": \""+humidity+"\",\"tag\": \"humidity\"}");
setTimeout(function () {
sensor.readTemperature();
}, 5000);
}
};
if (sensor.initialize()) {
sensor.readTemperature();
} else {
console.warn('Failed to initialize sensor');
}