[Solved] DHT11 some time status NaN

Hi

have a nodemcu connected with a dht11.

in the serial log it sometimes shows:

[601332] >vr[00]1
[602560] <[14|00]d[00|08]vw[00]4[00]nan
[602648] <[14|00]e[00|08]vw[00]5[00]nan
[602688] >[14|1B]][00|04]

and in blynk the gauges goes blank.

Could it be something to do with timer setinterval?
And whats the difference for “20000” and “20000L” in setInterval?

here is my code:

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SimpleTimer.h>
#define DHTPIN D5
#define DHTTYPE DHT11

int setTemp = 23;

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;

float humidity, temp_f; // Values read from sensor

char auth[] = "xxxx"; //insert here your token generated by Blynk

void setup()

{

WiFi.mode(WIFI_STA);

Serial.begin(9600); // See the connection status in Serial Monitor

delay(10);

Blynk.begin(auth, "xxxx", "xxxx"); //insert here your SSID and password

timer.setInterval(20000, sendData);

timer.setInterval(100000L, sendWifi);

}


void sendWifi() 
{
  Blynk.virtualWrite(1, map(WiFi.RSSI(), -105, -40, 0, 100) );
}



BLYNK_WRITE(V6) //temperature slider in app
{ 
  setTemp = param.asInt();
  
}


void sendData()

{

//Read the Temp and Humidity from DHT

float h = dht.readHumidity();

float t = dht.readTemperature();
if(t < setTemp) {Blynk.notify("for lav temp");}

//Write values to V04 and V05

Blynk.virtualWrite(4, h);

Blynk.virtualWrite(5, t);

}

void loop()

{

Blynk.run();

timer.run();

}

Nan is fine. DHT may fail readings sometimes. You need a check in code

if (isnan(t)) {
    return;
  }

Thx for a quick answer.

looked up your code.
NaN means “not a number”

Use it like the code below?
Seems to work… “return” does that mean its starts from the beginning again?

(Not a coder but here to learn :slight_smile: )

 void sendData()

{

//Read the Temp and Humidity from DHT

float h = dht.readHumidity();

float t = dht.readTemperature();
if(t < setTemp) {Blynk.notify("for lav temp");}

if (isnan(t)) {
    return;
  }

//Write values to V04 and V05

Blynk.virtualWrite(4, h);

Blynk.virtualWrite(5, t);

}

return; mean return nothing. In other works - stop execution of method.

You should place check before this :

I see, so it wont see a NaN as lower than setTemp.

thx again

It is more complicated. Nan - not a number as you said. This mean behaviour may be not predictable. So t < setTemp could be true or could be false :slight_smile:.