forked from momenso/node-dht-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
34 lines (30 loc) · 976 Bytes
/
test.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
// Module node-dht-sensor demo
// Reads relative air humidity from DHT sensor
var fs = require('fs');
var sensorLib = require('./build/Release/node-dht-sensor');
var sensor = {
initialize: function() {
this.totalReads = 0;
return sensorLib.initialize(11, 17);
},
read: function() {
var readout = sensorLib.read();
this.totalReads++;
console.log('Temperature: '+readout.temperature.toFixed(1)+'C, humidity: '+readout.humidity.toFixed(1)+'%'+
', valid: '+readout.isValid+
', errors: '+readout.errors);
fs.appendFile('log.csv',
new Date().getTime()+','+readout.temperature+','+readout.humidity+',"'+(readout.checksum ? 'Ok' : 'Failed')+'",'+readout.errors+'\n',
function (err) { });
if (this.totalReads < 300) {
setTimeout(function() {
sensor.read();
}, 500);
}
}
};
if (sensor.initialize()) {
sensor.read();
} else {
console.warn('Failed to initialize sensor');
}