forked from bbx10/node-htu21d
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (115 loc) · 4.56 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
The MIT License (MIT)
Copyright (c) 2014-2015 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var fs=require('fs');
var I2c = require('color-sensor-js/lib/i2c.js');
var MAX_TEMP_CONVERSION = 50; // milliseconds
var MAX_HUMI_CONVERSION = 16; // ms
var MAX_RESET_DELAY = 15; // ms
var HTU21D_I2CADDR = 0x40;
var HTU21D_READTEMP_NH = 0xF3;
var HTU21D_READHUMI_NH = 0xF5;
var HTU21D_WRITEREG = 0xE6;
var HTU21D_READREG = 0xE7;
var HTU21D_RESET = 0xFE;
// The regular versions of the READTEMP and READHUMI commands depend
// on clock stretching/hold which seems to be a problem the
// raspberry pi i2c controller.
var htu21d = function (options) {
this.config = options || {}
if (this.config.device === undefined) {
this.config.device = '/dev/i2c-1'
}
this.i2c = new I2c({ address: HTU21D_I2CADDR,
device: this.config.device }
);
};
htu21d.prototype.readTemperature = function(callback) {
var that = this;
this.i2c.write([HTU21D_READTEMP_NH], function(err, data) {
if (err) {
return callback(err, null);
} else {
setTimeout(function() {
that.i2c.read(3, function(err, data) {
if (err) {
return callback(err, null);
} else {
if ((data.length === 3) && calc_crc8(data, 3)) {
var rawtemp = ((data[0] << 8) | data[1]) & 0xFFFC;
var temperature = ((rawtemp / 65536.0) * 175.72) - 46.85;
//console.log("Temperature, C:", temperature.toFixed(1));
return callback(null, temperature.toFixed(1));
}
}
});
}, MAX_TEMP_CONVERSION);
}
});
};
htu21d.prototype.readHumidity = function(callback) {
var that = this;
this.i2c.write([HTU21D_READHUMI_NH], function(err) {
if (err) {
return callback(err, null);
}
else {
setTimeout(function() {
that.i2c.read(3, function(err, data) {
if (err) {
return callback(err, null);
} else {
if ((data.length === 3) && calc_crc8(data, 3)) {
var rawhumi = ((data[0] << 8) | data[1]) & 0xFFFC;
var humidity = ((rawhumi / 65536.0) * 125.0) - 6.0;
//console.log("Relative Humidity, %:", humidity);
return callback(null, humidity.toFixed(1));
}
}
});
}, MAX_HUMI_CONVERSION);
}
});
};
// buf = 3 bytes from the HTU21D-F for temperature or humidity
// 2 data bytes and 1 crc8 byte
// len = number of bytes in buf but it must be 3.
// return value < 0 error
// return value = 0 CRC good
// return value > 0 CRC bad
function calc_crc8(buf, len)
{
var dataandcrc;
// Generator polynomial: x**8 + x**5 + x**4 + 1 = 1001 1000 1
var poly = 0x98800000;
var i;
if (len === null) return -1;
if (len != 3) return -1;
if (buf === null) return -1;
// Justify the data on the MSB side. Note the poly is also
// justified the same way.
dataandcrc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8);
for (i = 0; i < 24; i++) {
if (dataandcrc & 0x80000000)
dataandcrc ^= poly;
dataandcrc <<= 1;
}
return (dataandcrc === 0);
}
module.exports = htu21d;