DHT11 gone after some time

Hi friends. I have problem with DHT11 humidity reading. the humidity read successfully but after some times gone ! and labeled widget set to NAN

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <BH1750.h>  // https://github.com/claws/BH1750
#define I2C_SCL 12      // D6  Barometric Pressure Sensor (BMP085)
#define I2C_SDA 13      // D7
#include <DHT.h>
#define DHTPIN 4 //pin gpio 12 in sensor
#define DHTTYPE DHT11   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

BH1750 lightMeter;
Adafruit_BMP085 bmp;

char auth[] = "a6ab62a2daf04bdab0d56fa85cd77955";                 //Put your blink token
char ssid[] = "MikroTik Home";                            //Put your SSID of your connection
char pass[] = "12345678";                            //Put your Password of your connection
char server[] = "10.5.51.3";

SimpleTimer timer;

float dst, bt, bp, ba;
char dstmp[20], btmp[20], bprs[20], balt[20];
bool bmp085_present = true;

void setup()
{
  WiFi.mode(WIFI_STA);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass, server, 8080); //insert here your SSID and password
 Wire.begin(I2C_SDA, I2C_SCL);
  delay(10);
  
  if (!bmp.begin()) 
  {
      Serial.println("Could not find a valid BMP085 sensor, check wiring!");
      //while (1) {}
  }

  timer.setInterval(1000L, sendWifi);
  timer.setInterval(1000L, sendUptime);
  lightMeter.begin();


}


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



void sendUptime()
{
  float h = dht.readHumidity();
  Blynk.virtualWrite(6, h); // virtual pin  
  
  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(4, lux);

  float bp =  bmp.readPressure()/1;
  Blynk.virtualWrite(9, bp); // virtual pin

  float ba =  bmp.readAltitude();
  Blynk.virtualWrite(7, ba); // virtual pin

  float bt =  bmp.readTemperature();
  Blynk.virtualWrite(12, bt); // virtual pin

  float dst =  bmp.readSealevelPressure(529)/100;
  Blynk.virtualWrite(13, dst); // virtual pin 
}

void loop()
{
  Blynk.run();
  timer.run();
}

Ask Google about DHT and NAN.

you are super hero :smiley: ( HULK )

the problem is solved by adding below code

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

This is generally accepted solution which tries to handle erroneous results from DHT. But as one of recent discussion on this forum showed - it might be misleading :wink: IF the DHT constantly sends NaN you will only see old, probably not actual data.

Of course it’s sending NAN with 1 second reading rate. Also, reading wifi signal strength every second makes no sense at all.

1 Like

no its working without any delay or old data. the edited code below:

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>;
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 4 //pin gpio 12 in sensor
#define DHTTYPE DHT11   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

#include <Wire.h>
#include <Adafruit_BMP085.h>

#include <BH1750.h>  // https://github.com/claws/BH1750
BH1750 lightMeter;


char auth[] = "a6ab62a2daf04bdab0d56fa85cd77955";                 //Put your blink token
char ssid[] = "MikroTik Home";                            //Put your SSID of your connection
char pass[] = "12345678";                            //Put your Password of your connection
char server[] = "10.5.51.3";

SimpleTimer timer;

#define I2C_SCL 12      // D6  Barometric Pressure Sensor (BMP085)
#define I2C_SDA 13      // D7
Adafruit_BMP085 bmp;

float dst, bt, bp, ba;
char dstmp[20], btmp[20], bprs[20], balt[20];
bool bmp085_present = true;

void setup()
{
  WiFi.mode(WIFI_STA);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass, server, 8080); //insert here your SSID and password
 Wire.begin(I2C_SDA, I2C_SCL);
  delay(10);
  
  if (!bmp.begin()) 
  {
      Serial.println("Could not find a valid BMP085 sensor, check wiring!");
      //while (1) {}
  }

  timer.setInterval(1000L, sendWifi);
  timer.setInterval(1000L, sendUptime);
  lightMeter.begin();


}


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



void sendUptime()
{
  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(4, lux);

  float bp =  bmp.readPressure()/1;
  Blynk.virtualWrite(9, bp); // virtual pin

  float ba =  bmp.readAltitude();
  Blynk.virtualWrite(7, ba); // virtual pin

  float bt =  bmp.readTemperature();
  Blynk.virtualWrite(12, bt); // virtual pin

  float dst =  bmp.readSealevelPressure(529)/100;
  Blynk.virtualWrite(13, dst); // virtual pin 

  float h = dht.readHumidity();
  float t = dht.readTemperature();

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

void loop()
{
  Blynk.run();
  timer.run();
}

You are trying to run two separate timers (and their functions) at the SAME time!!

The DHT11 timer is too fast anyhow (very slow sensor)… change it to something like 5015L. 5 second reads plus a 15ms offset to keep from bumping into the uptime timer.

1 Like