DHT22Default.ino (1046B)
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..."); 18 19 // read without samples. 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 int err = SimpleDHTErrSuccess; 25 if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { 26 Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err)); 27 Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000); 28 return; 29 } 30 31 Serial.print("Sample OK: "); 32 Serial.print((float)temperature); Serial.print(" *C, "); 33 Serial.print((float)humidity); Serial.println(" RH%"); 34 35 // DHT22 sampling rate is 0.5HZ. 36 delay(2500); 37 }