-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht22.js
40 lines (31 loc) · 917 Bytes
/
dht22.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
'use strict';
var ChildProcess = require('child_process');
function DHT22(pin){
this.pin = pin;
this.cmd = "./AdafruitDHT.py 22 " + this.pin;
}
DHT22.prototype.readSensor = function() {
var result = ChildProcess.execSync(this.cmd).toString().trim();
// Temp=16.7* Humidity=64.0%
// 'Temp=' is 5 characters
var c = result.substr(5, result.indexOf('*')-5);
this.tempC = parseFloat(c);
// 'Humidity=' is 9 characters
var beg = result.indexOf('Humidity=') + 9;
var n = result.indexOf('%') - beg;
var h = result.substr(beg, n);
this.humid = parseFloat(h);
}
DHT22.prototype.getHumid = function(){
return this.humid;
};
DHT22.prototype.getTempC = function(){
return this.tempC;
};
DHT22.prototype.getTempF = function(){
var t = this.getTempC();
t = (t * 1.8) + 32;
t = parseFloat(t.toFixed(1));
return t;
};
module.exports = DHT22;