arduinoprojects

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

dhtnew_test.ino (3358B)


      1 //
      2 //    FILE: dhtnew_test.ino
      3 //  AUTHOR: Rob Tillaart
      4 // VERSION: 0.1.6
      5 // PURPOSE: DHTNEW library test sketch for Arduino
      6 //     URL: https://github.com/RobTillaart/DHTNew
      7 //
      8 // HISTORY:
      9 // 0.1.0    2017-07-24 initial version
     10 // 0.1.1    2017-07-29 added begin();
     11 // 0.1.2    2018-01-08 removed begin();
     12 // 0.1.3    2020-04-30 replaced humidity and temperature with functions
     13 // 0.1.4    2020-06-08 improved error handling
     14 // 0.1.5    2020-06-15 match 0.3.0 error handling
     15 // 0.1.6    2020-11-09 wait for read handling
     16 //
     17 // DHT PIN layout from left to right
     18 // =================================
     19 // FRONT : DESCRIPTION
     20 // pin 1 : VCC
     21 // pin 2 : DATA
     22 // pin 3 : Not Connected
     23 // pin 4 : GND
     24 
     25 #include <dhtnew.h>
     26 
     27 DHTNEW mySensor(16);
     28 
     29 void setup()
     30 {
     31   Serial.begin(115200);
     32   Serial.println("dhtnew_test.ino");
     33   Serial.print("LIBRARY VERSION: ");
     34   Serial.println(DHTNEW_LIB_VERSION);
     35   Serial.println();
     36 
     37   Serial.println("\n1. Type detection test, first run might take longer to determine type");
     38   Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE");
     39   test();
     40   test();
     41   test();
     42   test();
     43 
     44   Serial.println("\n2. Humidity offset test");
     45   Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE");
     46   mySensor.setHumOffset(2.5);
     47   test();
     48   mySensor.setHumOffset(5.5);
     49   test();
     50   mySensor.setHumOffset(-5.5);
     51   test();
     52   mySensor.setHumOffset(0);
     53   test();
     54 
     55   Serial.println("\n3. Temperature offset test");
     56   Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE");
     57   mySensor.setTempOffset(2.5);
     58   test();
     59   mySensor.setTempOffset(5.5);
     60   test();
     61   mySensor.setTempOffset(-5.5);
     62   test();
     63   mySensor.setTempOffset(0);
     64   test();
     65 
     66   Serial.println("\n4. LastRead test");
     67   mySensor.read();
     68   for (int i = 0; i < 20; i++)
     69   {
     70     if (millis() - mySensor.lastRead() > 1000)
     71     {
     72       mySensor.read();
     73       Serial.println("actual read");
     74     }
     75     Serial.print(mySensor.getHumidity(), 1);
     76     Serial.print(",\t");
     77     Serial.println(mySensor.getTemperature(), 1);
     78     delay(250);
     79   }
     80 
     81   Serial.println("\nDone...");
     82 }
     83 
     84 void loop()
     85 {
     86 
     87 }
     88 
     89 void test()
     90 {
     91   // READ DATA
     92   uint32_t start = micros();
     93   int chk = mySensor.read();
     94   uint32_t stop = micros();
     95 
     96   switch (chk)
     97   {
     98     case DHTLIB_OK:
     99       Serial.print("OK,\t");
    100       break;
    101     case DHTLIB_ERROR_CHECKSUM:
    102       Serial.print("Checksum error,\t");
    103       break;
    104     case DHTLIB_ERROR_TIMEOUT_A:
    105       Serial.print("Time out A error,\t");
    106       break;
    107     case DHTLIB_ERROR_TIMEOUT_B:
    108       Serial.print("Time out B error,\t");
    109       break;
    110     case DHTLIB_ERROR_TIMEOUT_C:
    111       Serial.print("Time out C error,\t");
    112       break;
    113     case DHTLIB_ERROR_TIMEOUT_D:
    114       Serial.print("Time out D error,\t");
    115       break;
    116     case DHTLIB_ERROR_SENSOR_NOT_READY:
    117       Serial.print("Sensor not ready,\t");
    118       break;
    119     case DHTLIB_ERROR_BIT_SHIFT:
    120       Serial.print("Bit shift error,\t");
    121       break;
    122     case DHTLIB_WAITING_FOR_READ:
    123       Serial.print("Waiting for read,\t");
    124       break;
    125     default:
    126       Serial.print("Unknown: ");
    127       Serial.print(chk);
    128       Serial.print(",\t");
    129       break;
    130   }
    131 
    132   // DISPLAY DATA
    133   Serial.print(mySensor.getHumidity(), 1);
    134   Serial.print(",\t");
    135   Serial.print(mySensor.getTemperature(), 1);
    136   Serial.print(",\t");
    137   uint32_t duration = stop - start;
    138   Serial.print(duration);
    139   Serial.print(",\t");
    140   Serial.println(mySensor.getType());
    141   delay(500);
    142 }
    143 
    144 
    145 // END OF FILE