arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

DHT11ErrCount.ino (1124B)


      1 #include <SimpleDHT.h>
      2 
      3 // for DHT11, 
      4 //      VCC: 5V or 3V
      5 //      GND: GND
      6 //      DATA: 2
      7 int pinDHT11 = 2;
      8 SimpleDHT11 dht11(pinDHT11);
      9 
     10 void setup() {
     11   Serial.begin(115200);
     12 }
     13 
     14 void loop() {
     15   // start working...
     16   Serial.println("=================================");
     17   Serial.println("Sample DHT11 with error count");
     18 
     19   int cnt = 0;
     20   int err_cnt = 0;
     21   for (;;) {
     22     cnt++;
     23 
     24     byte temperature = 0;
     25     byte humidity = 0;
     26     int err = SimpleDHTErrSuccess;
     27     if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
     28       Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
     29       Serial.print(","); Serial.print(SimpleDHTErrDuration(err));
     30       err_cnt++;
     31     } else {
     32       Serial.print("DHT11, ");
     33       Serial.print((int)temperature); Serial.print(" *C, ");
     34       Serial.print((int)humidity); Serial.print(" H");
     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(1500);
     41   }
     42 }