Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By henkoegema
#76228 .........................
.........................
........................
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 ?
User avatar
By henkoegema
#76234
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();
}