I am getting a NaN return and i don't know why

Hey all

So i am using blynk to monitor a temp/humidity sensor that was built for me. I am a little familiar with working with Arduino stuff, but i am not super experienced so i apologize if this is a dumb question.

Once connected, the sensor array works well, but at ~12 hours after connecting, i get a NaN reading on my Blynk GUI. On the advice of a more technical friend, i inseted the following line into the code:

    float h = dht.readHumidity();
    delay(200);
    float t = dht.readTemperature();
    // fahrenheit = t * 1.8 + 32.0;
    if (t == NAN){
      Blynk.email("EMAILADDRESS", "ALARM", "Temp is NaN");
      Blynk.email("EMAILADDRESS", "ALARM", "Temp is NaN");
    }
    if (h == NAN){
      Blynk.email("EMAILADDRESS", "ALARM", "Humid is NaN");
      Blynk.email("EMAILADDRESS", "ALARM", "Humid is NaN");
    }

If i understand this correctly, i should get an “ALARM” email if the board gets a NaN. I received no email. The lads at Contact Blynk said i would only get a NaN from the sensors themselves. Can anyone help me out? Thanks in advance.

Here follows the code flashed to the device in its entirety:

    // display blynk information on serial monitor output
    #define BLYNK_PRINT Serial 

    // set up esp, blynk, dht and timer libraries
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    char auth[] = "authcode"; //insert here your token generated by Blynk

    #include <SimpleTimer.h>
    SimpleTimer timer;

    #include <DHT.h>
    #define DHTPIN 14  //dht22 data in
    #define DHTTYPE DHT22
    DHT dht(DHTPIN, DHTTYPE);
    float humidity, temp_f; // Values read from sensor

    // create a function called "sendData" and repeat it every x minutes, refer to line 162
    void sendData(){

        

        float h = dht.readHumidity();
        delay(200);
        float t = dht.readTemperature();
        // fahrenheit = t * 1.8 + 32.0;
        if (t == NAN){
          Blynk.email("email", "ALARM", "Temp is NaN");
          Blynk.email("email", "ALARM", "Temp is NaN");
        }
        if (h == NAN){
          Blynk.email("email", "ALARM", "Humid is NaN");
          Blynk.email("email", "ALARM", "Humid is NaN");
        }
        Blynk.virtualWrite(11, h);
        delay(200);
        Blynk.virtualWrite(10, t);
        delay(300);
        // Blynk.virtualWrite(12, fahrenheit);
        // delay(300);
        
        // blink green LED on/off after upload to blynk server
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);

     delay(200);

      // print on serial monitor temp and humid values
        Serial.print("temperature C");
        Serial.println( t );
        delay(500);
        Serial.print("humidity %");
        Serial.println( h );

     delay(500);

      // checking for too lows and highs
      // email if necessary
      // blink the yellow alarm led if necessary
      if (t < 5){
      Blynk.email("email", "ALARM", "Temp is below 5C");
        
        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);
        digitalWrite(4, HIGH);
      }

    delay(1000);

      if (t > 40){
      Blynk.email("email", "ALARM", "Temp is above 40C");  
        
        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);
        digitalWrite(4, HIGH);
        }

    delay(1000);

        if (h < 20){
        Blynk.email("email", "ALARM", "Humid is below 20%"); 

        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);
        digitalWrite(4, HIGH);
        }

    delay(1000);  

       if (h > 90){
        Blynk.email("email", "ALARM", "Humid is above 90%"); 

        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);
        digitalWrite(4, HIGH);
        }

    }

    void setup(){

      Serial.begin(9600); 
      dht.begin();

      pinMode(4, OUTPUT);   //yellow LED to test power
        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);
        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);
        digitalWrite(4, HIGH);
        delay(300);
        digitalWrite(4, LOW);
        delay(300);

      pinMode(5, OUTPUT);   //green LED for online
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
     
    delay(500);

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

    delay(500);

      Serial.print("device alive, go  to phone app");
        
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);
        delay(300);
        digitalWrite(5, LOW);
        delay(300);
        digitalWrite(5, HIGH);
     
    delay(500);
      
      // trigger sendData function above every x seconds
      timer.setInterval(10000, sendData);  // 10000 = 10 seconds  (x/1000=seconds)

    }


    // everything is setup, now the system starts until power off
    void loop(){

      Blynk.run();
      timer.run();


    }

you are using too many delays and they are long. It breaks connection to Blynk server. Please refer to Troubleshooting guide:
http://docs.blynk.cc/#troubleshooting-delay

Also, E-mails are limited to 1 per minute, so this code won’t work: [quote=“classicsnoot, post:1, topic:5611”]

if (t == NAN){
          Blynk.email("xxx@gmail.com", "ALARM", "Temp is NaN");
          Blynk.email("yyy@gmail.com", "ALARM", "Temp is NaN");
        }
        if (h == NAN){
          Blynk.email("xxx@gmail.com", "ALARM", "Humid is NaN");
          Blynk.email("yyy@gmail.com", "ALARM", "Humid is NaN");
        }

[/quote]

It is better to use

if (isnan(t)) {

Great! So shorten the delays i get, but what should i add to the email line to get it to work? Thanks for the help, Pavel. I really enjoy your services!

Solid copy. Thanks Dmitriy!

Overall there are many bugs in your code. I suggest you start simple and then complicate things.
First, make Timer work. Understand how to perform actions in intervals manner. Check the examples included in Blynk library ->>> PushData.ino. Then check the Email sketch.

Make sure it works - then add your own logic