-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.js
82 lines (52 loc) · 2 KB
/
temp.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
var log = require('logule').init(module, 'temp');
var HumixSense = require('node-humix-sense')
var child = require('child_process');
var proc;
var config = {
"moduleName":"dht22",
"commands" : [],
"events" : ["temp","humid"],
"debug": true
}
var humix = new HumixSense(config);
var humixSensor;
humix.on('connection', function(humixSensorModule){
humixSensor = humixSensorModule;
console.log('Communication with humix-sense is now ready. hsm:'+humixSensor);
});
function getSensorData(){
proc = child.exec("sudo ./loldht 7", function(err,data){
if(err){
log.error("Error:"+err.toString());
}
var str = data.toString(), lines = str.split(/(\r?\n)/g);
for (var i=0; i<lines.length; i++) {
if(lines[i].indexOf("Humidity") != -1){
log.info('Parsed Data: '+ lines[i]);
var s = lines[i];
var hindex = s.indexOf('=');
var pindex = s.indexOf('%');
var humid_str = s.substring(hindex+1,pindex).trim();
log.info("humid:"+humid_str);
s = s.substring(pindex);
hindex = s.indexOf('=');
pindex = s.indexOf('*');
var temp_str = s.substring(hindex+1,pindex).trim();
log.info("temp:"+temp_str);
var temp_data = {
"temp" : parseInt(temp_str)
};
var humid_data = {
"humid" : parseInt(humid_str)
};
log.info("Temperature:"+JSON.stringify(temp_data));
log.info("Humidity:"+JSON.stringify(humid_data));
humixSensor.event('temp',JSON.stringify(temp_data));
humixSensor.event('humid',JSON.stringify(humid_data));
}
}
});
}
setInterval(function(){
getSensorData();
},5000);