DHT22ErrCount.ino (1133B)
1 #include <SimpleDHT.h> 2 3 // for DHT22, 4 // VCC: 5V or 3V 5 // GND: GND 6 // DATA: 2 7 int pinDHT22 = 2; 8 SimpleDHT22 dht22(pinDHT22); 9 10 void setup() { 11 Serial.begin(115200); 12 } 13 14 void loop() { 15 // start working... 16 Serial.println("================================="); 17 Serial.println("Sample DHT22 with error count"); 18 19 int cnt = 0; 20 int err_cnt = 0; 21 for (;;) { 22 cnt++; 23 24 float temperature = 0; 25 float humidity = 0; 26 int err = SimpleDHTErrSuccess; 27 if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { 28 Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err)); 29 Serial.print(","); Serial.print(SimpleDHTErrDuration(err)); 30 err_cnt++; 31 } else { 32 Serial.print("DHT22, "); 33 Serial.print((float)temperature); Serial.print(" *C, "); 34 Serial.print((float)humidity); Serial.print(" RH%"); 35 } 36 Serial.print(", total: "); Serial.print(cnt); 37 Serial.print(", err: "); Serial.print(err_cnt); 38 Serial.print(", success rate: "); Serial.print((cnt - err_cnt) * 100.0 / (float)cnt); Serial.println("%"); 39 40 delay(2500); 41 } 42 }