NodeMCU with DHT22 and Blynk reading 255 every 20 seconds

Hi all,

this evening I encountered something I couldn’t fix, and now that I’ve fixed it, I don’t understand the problem.

The code below is a very simple integration of a DHT22 sensor on a NodeMCU writing a humidity and a temperature value to Blynk every 5 seconds.

For visual feedback and debugging I temporarily added a physical LED to the NodeMCU. At first I set the interval to 2 seconds, and I noticed that about every 20 seconds there were three spikes in the reading for both temp and humidity: 255 (the temp and hum were read as byte), normal, 255, normal, 255, normal, and then normal for 20 seconds, three spikes, normal for 20 sec, etc.

I then changed the interval to 5 seconds, which led to a single spike in the reading, still about every 20sec.

I changed the type for the readings from byte, to int (reading 2,147,483,647), to float (reading NaN), which changed the values but not the pattern of a misreading every 20 seconds.

Then I commented out all the stuff relating to the LED code, and that solved the problem.

But why?

Edit: after I added some code for a soil moisture sensor the problem returned, and it remained after I commented out the code that I added.

#define BLYNK_PRINT Serial

// Include DHT22 sensor library
#include <DHT.h>
#include <DHT_U.h>

// Include Blynk libraries
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Get Auth Token in the Blynk App Project Settings
char auth[] = "*removed*";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*removed*";
char pass[] = "*removed*";

//Define variables
DHT dhtA(5, DHT22);
BlynkTimer timer;
//int ledState = LOW;
//int lastLedState = HIGH;

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  dhtA.begin();

  timer.setInterval(5000L, climateRoutine);

  //pinMode(4, OUTPUT);

}

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



void climateRoutine() {
  float h1 = dhtA.readHumidity();
  float t1 = dhtA.readTemperature();
  Blynk.virtualWrite(V0, h1);
  Blynk.virtualWrite(V1, t1);

//  //Write an LED every time data is sent, remove in final build
//  ledState = digitalRead(4);
//  if (ledState != lastLedState) {
//    if (ledState == HIGH) {
//      digitalWrite (4, LOW);
//      lastLedState = HIGH;
//    } else {
//      digitalWrite (4, HIGH);
//      lastLedState = LOW;
//    }
//  }
}

Edit: apparently for some reason the fix stopped working, every 20 seconds the reading drops out to NaN for the DHT22 readings.

The DHT sensors are very poor, and don’t like being read too frequently. You also need to catch the NaN errors, as is done in the example here:

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FDHT11

Also, why do you have two DHT libraries?
I have read that some versions of the DHT libraries are better than others at giving good results.

Pete.

1 Like

Hi Pete,

thanks for the reply, I added the part about catching the NaN readings. A supplementary question though: that only works for float’s right? With int and byte it does give a reading, but that reading is off the chart. So how to fix it for other types?

About the libraries: no idea. When I add the DHT sensor library by Adafruit they both get added to the sketch.

You can filter on the hardware with t > -100 and t < 100 or filter in the graph settings.