#include #include #include #include #include #include #include using namespace std; //--------------------------------------------- //definitions #define MAXTIMINGS 85 int DHTPIN = 7; int dht22_dat[5] = {0,0,0,0,0}; //--------------------------------------------- static int read_dht22_dat() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; // pull pin down for 18 milliseconds pinMode(DHTPIN, OUTPUT); digitalWrite(DHTPIN, HIGH); delay(10); digitalWrite(DHTPIN, LOW); delay(18); // then pull it up for 40 microseconds digitalWrite(DHTPIN, HIGH); delayMicroseconds(40); // prepare to read the pin pinMode(DHTPIN, INPUT); // detect change and read data for ( i=0; i< MAXTIMINGS; i++) { counter = 0; while ((digitalRead(DHTPIN)) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = (digitalRead(DHTPIN)); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes dht22_dat[j/8] <<= 1; if (counter > 16) dht22_dat[j/8] |= 1; j++; } } // check we read 40 bits (8bit x 5 ) + verify checksum in the last byte // print it out if data is good if ((j >= 40) && (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) { float t, h; h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1]; h /= 10; t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3]; t /= 10.0; if ((dht22_dat[2] & 0x80) != 0) t *= -1; printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t ); return 1; } else { printf("Data not good, skip\n"); return 0; } } //--------------------------------------------- int main (int argc, char *argv[]) { if(wiringPiSetup () == -1) exit(1); while(1) { while(read_dht22_dat() == 0) delay(1000); // wait 1sec to refresh delay(1500); } return 0 ; }