arduinoprojects

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

DHT22WithRawBits.ino (1288B)


      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 RAW bits...");
     18   
     19   // read with raw sample data.
     20   // @remark We use read2 to get a float data, such as 10.1*C
     21   //    if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
     22   float temperature = 0;
     23   float humidity = 0;
     24   byte data[5] = {0};
     25   int err = SimpleDHTErrSuccess;
     26   if ((err = dht22.read2(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
     27     Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
     28     Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
     29     return;
     30   }
     31   
     32   Serial.print("Sample RAW Bits: ");
     33   for (int i = 0; i < 5; i++) {
     34       for (int n = 0; n < 8; n++)
     35           Serial.print(bitRead(data[i], n));
     36       Serial.print(' ');
     37   }
     38   Serial.println("");
     39   
     40   Serial.print("Sample OK: ");
     41   Serial.print((float)temperature); Serial.print(" *C, ");
     42   Serial.print((float)humidity); Serial.println(" RH%");
     43   
     44   // DHT22 sampling rate is 0.5HZ.
     45   delay(2500);
     46 }