-->
Page 1 of 1

Problem with deepsleep() ESP12E

PostPosted: Sat Jun 02, 2018 7:52 am
by henkoegema
.........................
.........................
........................
void setup() {
Serial.begin(9600);
dht.begin();


delay(1000); // Wait a few seconds between measurements.
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("Humidity: "); // show in serial monitor
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: "); // show in serial monitor
Serial.print(t);
Serial.print(" *C \n");

Serial.flush();

ESP.deepSleep(5e6);
}

void loop() {
}

The (part of) above sketck is working fine...........unless the values of the sensor (DHT22) can NOT be read. :(

It prints out: Failed to read from DHT sensor and the deepsleepfunction is stopped.
So the program fails in this part:

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}


What (where ?) is the 'return' doing in this if statement?
How can I solve this problem, so that if the sensor is not connected that the sleep function will still work correctly ?

Re: Problem with deepsleep() ESP12E

PostPosted: Sat Jun 02, 2018 8:41 am
by henkoegema
henkoegema wrote:.........................
.........................
........................
void setup() {
Serial.begin(9600);
dht.begin();


delay(1000); // Wait a few seconds between measurements.
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("Humidity: "); // show in serial monitor
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: "); // show in serial monitor
Serial.print(t);
Serial.print(" *C \n");

Serial.flush();

ESP.deepSleep(5e6);
}

void loop() {
}

The (part of) above sketck is working fine...........unless the values of the sensor (DHT22) can NOT be read. :(

It prints out: Failed to read from DHT sensor and the deepsleepfunction is stopped.
So the program fails in this part:

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}


What (where ?) is the 'return' doing in this if statement?
How can I solve this problem, so that if the sensor is not connected that the sleep function will still work correctly ?


I think I can answer myself. :)

I have replaced this

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return; }

with

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}else{
Serial.print("Humidity: "); // show in serial monitor
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: "); // show in serial monitor
Serial.print(t);
Serial.print(" *C \n");
Serial.flush();
}