Skip to content

Commit

Permalink
implemented dht11 sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
depuits committed Feb 28, 2018
1 parent bd6a4f2 commit 0fa9396
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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();
Expand All @@ -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.

Expand Down
29 changes: 22 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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');
}
Expand Down

0 comments on commit 0fa9396

Please sign in to comment.