-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (41 loc) · 1.51 KB
/
index.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
/*
* constellation-dht-sensor
* Retrieve temperature & humitidy from a dht sensor
* Push to Constellation platform
*/
var ConstellationHub = require('constellation-nodejs');
var ctx = new ConstellationHub();
var forgeValueObject = (temperature, humidity, room) => {
return {
"Temperature": temperature,
"Humidity" : humidity,
"Room": room,
"Category": "Ambient Sensor",
};
}
var getValues = (ctx) => {
//// Refreshing settings
ctx.hub.server.requestSettings()
.then((settings, err) => {
if (!settings || !settings.dht || !settings.room || !settings.gpio)
{
ctx.hub.server.writeLog("You must set setting values to use this package : dht(11,22) , gpio, room.", "error");
return;
}
sensor.read(settings.dht, settings.gpio, function(err, temperature, humidity) {
if (!err) {
forgeValueObject(temperature.toFixed(1), humidity.toFixed(1), settings.room);
ctx.hub.server.pushStateObject("" + currency, po, "Sensor.DHTAmbientSensor", 0);
} else ctx.hub.server.writeLog("Error while retrieving values from DHT Sensor : ", error);
});
});
}
//// Create a Sentinel Hub
ctx.Sentinel("Corulag")
.then((ctx) => {
ctx.on('connected', () => {
console.log("Connected to platform Constellation");
setInterval(getValues.bind(null, ctx), 10000)
});
return ctx.connect();
});