Skip to content

Commit

Permalink
Fix #11, refine protocol, stat success rate
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 9, 2017
1 parent 17ddc08 commit 000a52c
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 32 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ This library including the following examples:

1. [DHT11Default](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT11Default): Use DHT11 to sample.
1. [DHT11WithRawBits](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT11WithRawBits): Use DHT11 to sample and get the 40bits RAW data.
1. [TwoSensorsDefault](https://github.com/winlinvip/SimpleDHT/tree/master/examples/TwoSensorsDefault): Use two DHT11 to sample.
1. [DHT11ErrCount](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT11ErrCount): Use DHT11 to sample and stat the success rate.
1. [DHT22Default](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT22Default): Use DHT22 to sample.
1. [DHT22WithRawBits](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT22WithRawBits): Use DHT22 to sample and get the 40bits RAW data.
1. [DHT22Integer](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT22Integer): Use DHT22 to sample and ignore the fractional data.

1. [DHT22ErrCount](https://github.com/winlinvip/SimpleDHT/tree/master/examples/DHT22ErrCount): Use DHT22 to sample and stat the success rate.
1. [TwoSensorsDefault](https://github.com/winlinvip/SimpleDHT/tree/master/examples/TwoSensorsDefault): Use two DHT11 to sample.

## Links

1. [adafruit/DHT-sensor-library](https://github.com/adafruit/DHT-sensor-library)
1. [Arduino #4469: Add SimpleDHT library.](https://github.com/arduino/Arduino/issues/4469)
1. [DHT11 datasheet and protocol.](https://cdn-shop.adafruit.com/datasheets/DHT11-chinese.pdf)
1. [DHT22 datasheet and protoocl.](https://cdn-shop.adafruit.com/datasheets/DHT22.pdf)
1. [DHT11 datasheet and protocol.](https://akizukidenshi.com/download/ds/aosong/DHT11.pdf)
1. [DHT22 datasheet and protoocl.](http://akizukidenshi.com/download/ds/aosong/AM2302.pdf)

Winlin 2016.1
37 changes: 22 additions & 15 deletions SimpleDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]

float temperature = 0;
float humidity = 0;
if ((read2(pin, &temperature, &humidity, pdata)) != SimpleDHTErrSuccess) {
if ((ret = read2(pin, &temperature, &humidity, pdata)) != SimpleDHTErrSuccess) {
return ret;
}

Expand All @@ -45,16 +45,19 @@ int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]
}

int SimpleDHT::confirm(int pin, int us, byte level) {
// wait one more count to ensure.
int cnt = us / 10 + 1;
int cnt = us / 10;
if ((us % 10) > 0) {
cnt++;
}

bool ok = false;
for (int i = 0; i < cnt; i++) {
delayMicroseconds(10);

if (digitalRead(pin) != level) {
ok = true;
break;
}
delayMicroseconds(10);
}

if (!ok) {
Expand Down Expand Up @@ -112,6 +115,11 @@ int SimpleDHT11::read2(int pin, float* ptemperature, float* phumidity, byte pdat
*phumidity = (int)(humidity>>8);
}

// For example, when remove the data line, it will be success with zero data.
if (temperature == 0 && humidity == 0) {
return SimpleDHTErrZeroSamples;
}

return ret;
}

Expand All @@ -130,7 +138,6 @@ int SimpleDHT11::sample(int pin, byte data[40]) {
digitalWrite(pin, HIGH);
pinMode(pin, INPUT);
delayMicroseconds(30);

// DHT11 starting:
// 1. PULL LOW 80us
// 2. PULL HIGH 80us
Expand Down Expand Up @@ -211,31 +218,31 @@ int SimpleDHT22::sample(int pin, byte data[40]) {
// According to protocol: http://akizukidenshi.com/download/ds/aosong/AM2302.pdf
// notify DHT11 to start:
// 1. T(be), PULL LOW 1ms(0.8-20ms).
// 2. T(go), PULL HIGH 30us(20-200us).
// 2. T(go), PULL HIGH 30us(20-200us), use 40us.
// 3. SET TO INPUT.
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(1000);
digitalWrite(pin, HIGH);
pinMode(pin, INPUT);
delayMicroseconds(30);
delayMicroseconds(40);

// DHT11 starting:
// 1. T(rel), PULL LOW 80us(75-85us)
// 2. T(reh), PULL HIGH 80us(75-85us)
if (confirm(pin, 80, LOW)) {
// 1. T(rel), PULL LOW 80us(75-85us), use 90us.
// 2. T(reh), PULL HIGH 80us(75-85us), use 90us.
if (confirm(pin, 90, LOW)) {
return SimpleDHTErrStartLow;
}
if (confirm(pin, 80, HIGH)) {
if (confirm(pin, 90, HIGH)) {
return SimpleDHTErrStartHigh;
}

// DHT11 data transmite:
// 1. T(LOW), 1bit start, PULL LOW 50us(48-55us)
// 1. T(LOW), 1bit start, PULL LOW 50us(48-55us), use 60us.
// 2. T(H0), PULL HIGH 26us(22-30us), bit(0)
// 3. T(H1), PULL HIGH 70us(68-75us), bit(1)
for (int j = 0; j < 40; j++) {
if (confirm(pin, 50, LOW)) {
if (confirm(pin, 60, LOW)) {
return SimpleDHTErrDataLow;
}

Expand All @@ -258,8 +265,8 @@ int SimpleDHT22::sample(int pin, byte data[40]) {
}

// DHT11 EOF:
// 1. T(en), PULL LOW 50us(45-55us).
if (confirm(pin, 50, LOW)) {
// 1. T(en), PULL LOW 50us(45-55us), use 60us.
if (confirm(pin, 60, LOW)) {
return SimpleDHTErrDataEOF;
}

Expand Down
4 changes: 3 additions & 1 deletion SimpleDHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#define SimpleDHTErrDataEOF 104
// Error to validate the checksum.
#define SimpleDHTErrDataChecksum 105
// Error when temperature and humidity are zero, it shouldn't happen.
#define SimpleDHTErrZeroSamples 106

class SimpleDHT {
public:
Expand All @@ -51,7 +53,7 @@ class SimpleDHT {
// For DHT11, in H, such as 35H.
// For DHT22, in RH%, such as 53%RH.
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 1s.
// @remark the min delay for this method is 1s(DHT11) or 2s(DHT22).
// @return SimpleDHTErrSuccess is success; otherwise, failed.
virtual int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
// to get a more accurate data.
Expand Down
4 changes: 2 additions & 2 deletions examples/DHT11Default/DHT11Default.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void loop() {
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(err);
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}

Expand All @@ -30,5 +30,5 @@ void loop() {
Serial.print((int)humidity); Serial.println(" H");

// DHT11 sampling rate is 1HZ.
delay(1000);
delay(1500);
}
40 changes: 40 additions & 0 deletions examples/DHT11ErrCount/DHT11ErrCount.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <SimpleDHT.h>

// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;

void setup() {
Serial.begin(115200);
}

void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11 with error count");

int cnt = 0;
int err_cnt = 0;
for (;;) {
cnt++;

byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);err_cnt++;
}

Serial.print("DHT11, ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.print(" H");
Serial.print(", total: "); Serial.print(cnt);
Serial.print(", err: "); Serial.print(err_cnt);
Serial.print(", success rate: "); Serial.print((cnt - err_cnt) * 100.0 / (float)cnt); Serial.println("%");

delay(1500);
}
}
4 changes: 2 additions & 2 deletions examples/DHT11WithRawBits/DHT11WithRawBits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void loop() {
byte data[40] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(err);
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}

Expand All @@ -40,5 +40,5 @@ void loop() {
Serial.print((int)humidity); Serial.println(" H");

// DHT11 sampling rate is 1HZ.
delay(1000);
delay(1500);
}
4 changes: 2 additions & 2 deletions examples/DHT22Default/DHT22Default.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void loop() {
float humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(err);
Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
return;
}

Expand All @@ -32,5 +32,5 @@ void loop() {
Serial.print((float)humidity); Serial.println(" RH%");

// DHT22 sampling rate is 0.5HZ.
delay(2000);
delay(2500);
}
39 changes: 39 additions & 0 deletions examples/DHT22ErrCount/DHT22ErrCount.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <SimpleDHT.h>

// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 2;
SimpleDHT22 dht22;

void setup() {
Serial.begin(115200);
}

void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT22 with error count");

int cnt = 0;
int err_cnt = 0;
for (;;) {
cnt++;

float temperature = 0;
float humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.println(err);err_cnt++;
}

Serial.print((float)temperature); Serial.print(" *C, ");
Serial.print((float)humidity); Serial.print(" RH%");
Serial.print(", total: "); Serial.print(cnt);
Serial.print(", err: "); Serial.print(err_cnt);
Serial.print(", success rate: "); Serial.print((cnt - err_cnt) * 100.0 / (float)cnt); Serial.println("%");

delay(2500);
}
}
4 changes: 2 additions & 2 deletions examples/DHT22Integer/DHT22Integer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void loop() {
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(err);
Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
return;
}

Expand All @@ -30,5 +30,5 @@ void loop() {
Serial.print((int)humidity); Serial.println(" RH%");

// DHT22 sampling rate is 0.5HZ.
delay(2000);
delay(2500);
}
4 changes: 2 additions & 2 deletions examples/DHT22WithRawBits/DHT22WithRawBits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void loop() {
byte data[40] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(pinDHT22, &temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(err);
Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
return;
}

Expand All @@ -42,5 +42,5 @@ void loop() {
Serial.print((float)humidity); Serial.println(" RH%");

// DHT22 sampling rate is 0.5HZ.
delay(2000);
delay(2500);
}
4 changes: 2 additions & 2 deletions examples/TwoSensorsDefault/TwoSensorsDefault.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void loop() {
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Communication error with Sensor 1, err="); Serial.print(err);
Serial.print("Communication error with Sensor 1, err="); Serial.println(err);delay(1000);
return;
}

Expand All @@ -48,7 +48,7 @@ void loop() {
byte temperature2 = 0;
byte humidity2 = 0;
if ((err = dht11.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Communication error with Sensor 2, err="); Serial.print(err);
Serial.print("Communication error with Sensor 2, err="); Serial.println(err);delay(1000);
return;
}

Expand Down

0 comments on commit 000a52c

Please sign in to comment.