diff --git a/README.md b/README.md index dc7c572..f45b935 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pigpio-dht -Dht22 control using node.js and pigpio. +Dht sensor control using node.js and pigpio. Supported sensors are DHT11, DHT22 and AM2302. ## Installation @@ -9,11 +9,24 @@ Dht22 control using node.js and pigpio. ## Usage +```javascript +const dht = require('dht'); +const sensor = dht(dataPin, dhtType); +``` + +| Sensor | dhtType | +|-----------------|:----------------:| +| DHT11 | 11 | +| DHT22 or AM2302 | 22 | + +### Example + ```javascript const dht = require('dht'); const dataPin = 5; -const sensor = dht(dataPin); +const dhtType = 22; //optional +const sensor = dht(dataPin, dhtType); setInterval(() => { sensor.read(); @@ -29,22 +42,28 @@ sensor.on('badChecksum', () => { }); ``` +### Methods +#### `read()` + +Start a new reading of the sensor. This can't be called more then once every second for the DHT11 sensor or once every 2 seconds for the DHT22 sensor. + ### Events -#### start +#### `start` Emitted when starting to read a value. -#### end +#### `end` -Emitted when the reading stops. This because it was complete, an error occured or anything else. +Emitted when the reading stops. This because it was complete, an error occurred or anything else. -#### result +#### `result` - result object containing temperature and humidity + Emitted when the reading was completed successful. -#### badChecksum +#### `badChecksum` Emitted when finished reading but the checksum was invalid. diff --git a/index.js b/index.js index f3d9adb..912f4b0 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,9 @@ module.exports = function(pin, type) { var tempLow = 0; var checksum = 0; + // default type is dht22 + type = type || 22; + function startReading() { if (dht.reading) { // cancel out if we are already reading @@ -50,6 +53,23 @@ module.exports = function(pin, type) { dht.emit('end'); } + function interpretDht11() { + let rhum = rhumHigh; + let temp = tempHigh; + + return { temperature: temp, humidity: rhum }; + } + function interpretDht22() { + let rhum = ((rhumHigh << 8) + rhumLow) * 0.1; + + // check the temperature sign + let mult = (tempHigh & 128) ? -0.1 : 0.1; + tempHigh = tempHigh & 127; // strip the sign bit + let temp = ((tempHigh << 8) + tempLow) * mult; + + return { temperature: temp, humidity: rhum }; + } + gpio.on('alert', (level, tick) => { // Accumulate the 40 data bits. Format into 5 bytes, humidity high, // humidity low, temperature high, temperature low, checksum. @@ -97,14 +117,9 @@ module.exports = function(pin, type) { // Is checksum ok? if ((total & 255) == checksum) { - let rhum = ((rhumHigh << 8) + rhumLow) * 0.1; - - // check the temperature sign - let mult = (tempHigh & 128) ? -0.1 : 0.1; - tempHigh = tempHigh & 127; // strip the sign bit - let temp = ((tempHigh << 8) + tempLow) * mult; + let res = (type == 11) ? interpretDht11() ? interpretDht22(); - dht.emit('result', { temperature: temp, humidity: rhum }); + dht.emit('result', res); } else { dht.emit('badChecksum'); }