35 lines
880 B
C++
35 lines
880 B
C++
#include <SimpleDHT.h>
|
|
|
|
// for DHT22,
|
|
// VCC: 5V or 3V
|
|
// GND: GND
|
|
// DATA: 2
|
|
int pinDHT22 = 2;
|
|
SimpleDHT22 dht22(pinDHT22);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
}
|
|
|
|
void loop() {
|
|
// start working...
|
|
Serial.println("=================================");
|
|
Serial.println("Sample DHT22...");
|
|
|
|
// read without samples.
|
|
byte temperature = 0;
|
|
byte humidity = 0;
|
|
int err = SimpleDHTErrSuccess;
|
|
if ((err = dht22.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
|
|
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
|
|
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
|
|
return;
|
|
}
|
|
|
|
Serial.print("Sample OK: ");
|
|
Serial.print((int)temperature); Serial.print(" *C, ");
|
|
Serial.print((int)humidity); Serial.println(" RH%");
|
|
|
|
// DHT22 sampling rate is 0.5HZ.
|
|
delay(2500);
|
|
}
|