NodeMCU + ESP8266 + DHT22 + Blynk.run() nan issues

Hi,

I have an issue with a setup which consists of:
NodeMCU 1.0
DHT22 Sensor
Local Raspberry Blynk Server

Versions used:
Blynk library used: 0.3.7
Local Rpi server version: 0.16.2

The following program is from instructables, slightly modified:


If i let without the isnan() check of DHT22 read values, i get Nan for both temperature and humidity as well sometimes, and after a while i get only Nan-s on the virtual ports.

Hardware images:


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

#define DHTPIN 13 //pin gpio 13 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11

DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
      Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, "xxx", "yyy", IPAddress(192,168,0,4));

 
  // Setup a function to be called every 10 seconds
  timer.setInterval(10000L, sendUptime);
}

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  //t or h sometimes is nan, do not send nan value to blynk
  // h and t is nan bc. of the blynk libraty and not from the dht library!!!!!
  // Test out to remove every blynk function call, and check serial port
  if(isnan(h) || isnan(t))
  {
    Serial.print(F("Failed to read from DHT sensor!\r\n"));
  }
  else
  {
    Blynk.virtualWrite(10, t); // virtual pin 
    Blynk.virtualWrite(11, h); // virtual pin 
    Serial.print(F("Everything should be fine, reading transmitted\r\n"));
  }
}

void loop()
{
  Blynk.run(); //When i comment this line, seems like the readHumidity and ReadTemperature works each time
  timer.run();
}

Connecting to the serialPort i saw the following messages:

Serial output with blynk.run() uncommented
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Failed to read from DHT sensor!
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
…
*- After a while i get only
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
…

Serial output with Blynk.run() commented out
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
Everything should be fine, reading transmitted
…

For me it seems like for some reason the Blynk.run() influences the reading from the DHT22 sensor.

1 Like

Hello. Where is dht.begin(); ?

Hi Dimitry,

as far as i know dht.begin() is done in: DHT dht(DHTPIN, DHTTYPE); or it is initialized with thin this call

I also tried to put dht.begin() in setup() function, but did not help. The toggle between the success and failure reads remains.

Basically the success/failure rate at 10 second interval between reads:

11 reading success, 1 fail, 2 sucess, 1 fail. (And repeats some time, after that i get only failed ones)

5V for DHT22 and should be OK.

Should i try this 5V connection, without the 10K resistor? Bc. now i am using 3.3V and a 10K resistor between VCC and DATA ?

Between +5V and PIN 2 on DHT should be 10k resistor, PIN 3 on DHT no connected and PIN 1 on DHT directed to Vin. That’s all.

Thank you @PawKon for your help, but the solution proposed didn’t helped, no changes in success/failure reads

Hmmm… My one minute made circuit working fine. Please use DHTtester from Arduino’s examples.



I wrote sketch for ESP and Blynk (two Labeled text for temp and humidity on V1 and V2) :

/**************************************************************

  • Blynk is a platform with iOS and Android apps to control
  • Arduino, Raspberry Pi and the likes over the Internet.
  • You can easily build graphic interfaces for all your
  • projects by simply dragging and dropping widgets.
  • Downloads, docs, tutorials: http://www.blynk.cc
  • Blynk community: http://community.blynk.cc
  • Social networks: http://www.fb.com/blynkapp
  •                           http://twitter.com/blynk_app
    
  • Blynk library is licensed under MIT license
  • This example code is in public domain.

  • This example runs directly on ESP8266 chip.
  • You need to install this for ESP8266 development:
  • https://github.com/esp8266/Arduino
  • Please be sure to select the right ESP8266 module
  • in the Tools -> Board menu!
  • Change WiFi ssid, pass, and Blynk auth token to run :slight_smile:

**************************************************************/

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Wire.h>
#define DHTTYPE DHT22 // DHT 22
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);

long nt=10000;
int lasttemp=20;
int lasthum=40;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “xxxxxxxxx”;
boolean stan=0;
void setup()
{

Serial.begin(9600);
dht.begin();
Blynk.begin(auth, “xxx”, “xxx”);
pinMode(14, INPUT_PULLUP);

}

void loop()
{
Blynk.run();

Serial.println(millis());
Serial.println(nt);
Serial.println("");

if (millis() > nt)
{
nt = millis()+10000;
dht22();
}
}
void dht22()

{
float t = dht.readTemperature();
if (t<100)
{
Blynk.virtualWrite(1, int(t));
lasttemp=t;

}
else
{
t=lasttemp;
Blynk.virtualWrite(1, int(t));
}

float h = dht.readHumidity();
if (h<99)
{
Blynk.virtualWrite(2,int(h));
lasthum=h;
}
h=lasthum;
Blynk.virtualWrite(2, int(h));
}

Did you try changing the order of void setup() and void senduptime() ?
And dont forget to add dht.begin(); in the void setup

like this:

Libraries here

(#)define DHTPIN 13 //pin gpio 13 in sensor
(#)define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11

<>DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “xxx”; // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void sendUptime()
{
// You can send any value at any time.
// Please don’t send more that 10 values per second.
//Read the Temp and Humidity from DHT
float h = dht.readHumidity();
float t = dht.readTemperature();

//t or h sometimes is nan, do not send nan value to blynk
// h and t is nan bc. of the blynk libraty and not from the dht library!!!
// Test out to remove every blynk function call, and check serial port
if(isnan(h) || isnan(t))
{
Serial.print(F(“Failed to read from DHT sensor!\r\n”));
}
else
{
Blynk.virtualWrite(10, t); // virtual pin
Blynk.virtualWrite(11, h); // virtual pin
Serial.print(F(“Everything should be fine, reading transmitted\r\n”));
}
}

void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, “xxx”, “yyy”, IPAddress(192,168,0,4));
dht.begin();

// Setup a function to be called every 10 seconds
timer.setInterval(10000L, sendUptime);
}
void loop()
{
Blynk.run(); //When i comment this line, seems like the readHumidity and ReadTemperature works each time
timer.run();
} indent preformatted text by 4 spaces

Thank you for your help @sirclecube , but that didn’t helped either.

Thank you @PawKon , the code it seems to be ok, but unfortunately in my view the logic itself, doesn’t provide all the time the latest temperature and humidity.

This is how i interpret your code:

  1. Default you set temp and humidity to initial values: (20/40)
  2. Each 10 seconds you do a reading
  3. Save last measured values, and if measured value is above the limit, send the previously measured value

THE PROBLEM

If the reading is failing, it happens the following: (resulting in fake readings)

if (t<100) // In case t is NAN, it will jump to the else statement and it will copy the previous temperature reading into t and it will transmit again to Blynk. In long term i will get  ONLY fake readings, becouse after a while it comes only NAN values, which means that the last successfull reading i will get each time.... 
{
Blynk.virtualWrite(1, int(t));
lasttemp=t;

}
else
{
t=lasttemp;
Blynk.virtualWrite(1, int(t));
} 

I have tested also with this code, where i saw the issue:

float t = dht.readTemperature();


if(isnan(t))
{
    Blynk.virtualWrite(12, "We are receiving NAN value for temperature, which is not good");
}

if (t<100)
{
Blynk.virtualWrite(10, t);
lasttemp=t;

}
else
{
t=lasttemp;
Blynk.virtualWrite(10, t);
}

My problem persists with the NAN value readings…

Update
I have tested with 2 different AM2302 sensors, and 2 different NodeMCU board

I have also tested with DHTTester (changed the delay to 10 seconds), and i can confirm works without any issue, didn’t got any “Failed to read from DHT sensor!” error messages.

So i think the root cause can be in the blynk library after calling the blynk.run(); function

Try to test your sketch with DHT11. I’v got one solution woth DHT11 and working good few days (of course with Blynk).

Unfortunately i do not have a DHT11 sensor, only AM2302 ones, but still i think there is an issue in blynk.run();
Commenting that line, all readings are successful.

@Dmitriy - Is it possible that some interrupts from blynk library are causing this issue, or something similar?

Kszili, try this: run your ESP and serial monitor. When you’ll see “Failed to read from DHT sensor!” unplug cable 5V from DHT and after 5 second plug in back. You still see “Failed to read from DHT sensor!”?

Kszili, you’re right, but i prefer previous temp and humidity then notification with NAN :slight_smile:

I tried connect DHT22 data to D7 (GPIO 13) like you and I had problem with NAN. After few attempts I changed GPIO to D2 (GPIO 4) and my DHT22 was start to show correct data. For now my Node MCU with DHT22 working more then 3 hour without problems.

Thank you again @PawKon, i will try changing the GPIO port, and i will report back with the results.

I have tried out to put on GPIO 4, as well GPIO 5, but no change in the fail/success responses.
I tried also to connect to blync-cloud, that did not helped neither.

Here is a screenshot of the output messages, no matter on which GPIO port i try:

@PawKon, can you try to run my code on your NodeMCU, to see if you get also failed readings?

just replace:

Blynk.begin(auth, "xxx", "yyy", IPAddress(192,168,0,4));

with

Blynk.begin(auth, "xxx", "yyy", "blynk-cloud.com", 8442); //With your auth token

Thank you!

FYI I have much better luck using rob tillaarts DHT library…

http://playground.arduino.cc/Main/DHTLib