#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void getSensorData();
void setup() {
Serial.begin(9600);
pinMode(DHTPIN, INPUT_PULLUP);
dht.begin();
}
void loop() {
Serial.println("Woke up!");
// Wait a few seconds between measurements.
delay(5000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
// try the an other methode ..
getSensorData();
Serial.println("Sleep now!");
//delay(20 * 1000); // Send data every 20 seconds
ESP.deepSleep(0.1 * 60000000); // Send to sleep for 35 minutes
}
void getSensorData() {
int retry_limit = 50; // Limit read attempts.
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
while ((isnan(temperature) || isnan(humidity))
|| (temperature == 0 && humidity == 0)) {
if (--retry_limit < 1) {
temperature = -1;
humidity = -1;
Serial.println(F("No DHT sensor detected."));
return;
}
#ifdef DEBUG
RFlash();
Serial.print(F("!"));
#endif
delay(5000);
temperature = dht.readTemperature();
humidity = dht.readHumidity();
}
#ifdef DEBUG
Serial.println("");
RFlash();
#endif
}
And that's the result
0�~�4�!�{�OAa�Woke up!
Humidity: 47.20 % Temperature: 21.10 *C Sleep now!
0�~?�4�!�{�OA,�Woke up!
Failed to read from DHT sensor!
Woke up!
Failed to read from DHT sensor!
Woke up!
Failed to read from DHT sensor!
Woke up!
Failed to read from DHT sensor!
Woke up!
Failed to read from DHT sensor!
Woke up!
Failed to read from DHT sensor!
Woke up!
Failed to read from DHT sensor!
I am unable to read data from the dht22, even if I loop through the sensor reading.