DHT-11 ESP8266-01 Erratic Temperature spikes

Any ideas as to why i get such wild temp readings?
it drops then reads correctly and then repeats, i have 4 DHT11 sensors and they all act the same.

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>
    #include <dht11.h>
    dht11 DHT;
    #define DHT11_PIN 2
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "*************"; //insert here your token generated by Blynk

    SimpleTimer timer;

    void setup()
    {
      Serial.begin(115200); // See the connection status in Serial Monitor
       Blynk.begin(auth, "*************", "*************", IPAddress(*************));
     
      // Setup a function to be called every second
      timer.setInterval(6000L, sendUptime);
    }

    // This function sends Arduino's up time every second to Virtual Pin (5).
    // In the app, Widget's reading frequency should be set to PUSH. This means
    // that you define how often to send data to Blynk App.
    void sendUptime()
    {
      // You can send any value at any time.
      // Please don't send more that 10 values per second.
      Blynk.virtualWrite(10, DHT.temperature); //virtual pin
      Blynk.virtualWrite(11, DHT.humidity); // virtual pin 
    }

    void loop()
    {
      Blynk.run(); // Initiates Blynk
      timer.run(); // Initiates SimpleTimer
      int chk; 
      chk = DHT.read(DHT11_PIN);    // READ DATA
    }

change the timer.setinterval to 2000L or greater.

Edit - and remove everything from loop() except Blynk.run() and timer.run().

1 Like

EDIT - removed evidence of massive brain fart of a statement :blush:


A DHT is a very slow sensor, and not very accurate.

1 Like

agree, if you need to know the exact temperature ever second, you are using the wrong sensor…

exactly, so put this:

  int chk; 
  chk = DHT.read(DHT11_PIN);    // READ DATA

in the senduptime function :wink:

Here’s mine for a DHT22,



//#define BLYNK_PRINT Serial    // Comment this out to     disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "AUTH";   // Cloud Server
//char auth[] = "OTHER AUTH"; // Local Server
char ssid[] = "NetworkName";
char pass[] = "NetworkPassword";
byte arduino_mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress device_ip ( 10,   0,   0,  80);
IPAddress server_ip ( 10, 0, 0, 130 );      //IP of machine server runs on
IPAddress gateway_ip ( 10,   0,   0,   1);
IPAddress subnet_mask(255, 255, 255, 0);

#include <DHT.h>
#define DHTPIN 2 //pin gpio 12 in sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void readSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true);

  Blynk.virtualWrite(V0, t); // virtual pin
  Blynk.virtualWrite(V1, h); // virtual pin
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442); // Cloud Server
  //Blynk.begin(auth, ssid, pass, server_ip, 8442); // Local Server
  
    // Setup WiFi network
  WiFi.config(device_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, pass);

  // Setup Blynk
  Blynk.config(auth);
  while (Blynk.connect() == false) {
  }
  dht.begin();
  timer.setInterval(3000, readSensor);     //Read every 3 secs
}

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

you can also use the RunningMedian library to smooth out readings:

  int chk;
  //roof DHT22 section
  chk = DHT.read22(ROOF_DHT22_PIN);
  roofHum = DHT.humidity;
  medianRoofHum = samplesRoofHum.getMedian();
  samplesRoofHum.add(roofHum); // this is the Running Median command
  roofTemp = DHT.temperature;
  medianRoofTemp = samplesRoofTemp.getMedian();
  samplesRoofTemp.add(roofTemp); // this is the Running Median command
  roofDewPoint = dewPoint(DHT.temperature, DHT.humidity);
  medianRoofDewPoint = samplesRoofDewPoint.getMedian();
  samplesRoofDewPoint.add(roofDewPoint); // this is the Running Median command

  Blynk.virtualWrite(V16, medianRoofTemp); // this sends the calculation to the Blynk virtual pin
  Blynk.virtualWrite(V18, medianRoofHum); // this sends the reading to the Blynk virtual pin
  Blynk.virtualWrite(V30, medianRoofDewPoint); // this sends the calculation to the Blynk virtual pin

1 Like

Virtual pin numbers not ms.

V10 and V11 are virtual pins.

Soooo sorry… and a bit embarrassed… dyslexic tendency gets the best of me when I am bleary headed…I had just woke up and clearly wasn’t seeing straight :roll_eyes: :blush:

However, like me at times… this statement holds true :stuck_out_tongue_winking_eye:

1 Like

thanks for all of the comments, i will try later and report back.

thanks to everyone for commenting.
thanks to @Skybound your answer worked a treat,
here is a screen shot showing the constant red temp line named “outside” in comparison to the other 3 not yet edited. i chose a 6 second interval as it is not important for it’s purpose.

revised code

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 2
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*************"; //insert here your token generated by Blynk

SimpleTimer timer;

void setup()
{
  Serial.begin(115200); // See the connection status in Serial Monitor
   Blynk.begin(auth, "*************", "*************", IPAddress(*************));
 
  // Setup a function to be called every second
  timer.setInterval(6000L, sendUptime);
}

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(10, DHT.temperature); //virtual pin
  Blynk.virtualWrite(11, DHT.humidity); // virtual pin 
int chk; 
  chk = DHT.read(DHT11_PIN);    // READ DATA
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer

}
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(10, DHT.temperature); //virtual pin
  Blynk.virtualWrite(11, DHT.humidity); // virtual pin 
int chk; 
  chk = DHT.read(DHT11_PIN);    // READ DATA
}

i think it should be in this order:

{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
int chk; 
  chk = DHT.read(DHT11_PIN);    // READ DATA

  Blynk.virtualWrite(10, DHT.temperature); //virtual pin
  Blynk.virtualWrite(11, DHT.humidity); // virtual pin 
}
1 Like

thanks @Dave1829 i tried both ways and the result is exactly the same

Are you aware the the accuracy of a DHT11 is ±2°C

In-other words they can and will “spike” at least 2° either way at times.

I fail to see when and where the variable chk is used :thinking:

Are you sure its not something external, like having the sensors where the sun shines directly on them from time to time, causing a temperature spike? OK, wrong time of day, but maybe a central heating radiator or some such?

Hey Guys, the problem is solved. @Dave1829 solution solved the problem 100%

personally i think by changing the code to 6 seconds was what made the difference

timer.setInterval(6000L, sendUptime);

1 Like

i think it causes the DHT.read() instruction that is specified in the library to operate:

Arduino Playground - HomePage
The interface of the class supports only one function for reading the humidity and temperature from the sensors and store it in two members of the class. The read() function verifies the checksum of the data transmission and it has a time out function. If there is a checksum error the values of temperature and/or humidity might still be valid.

The class has 6 read functions read11(PIN), read(PIN) and readxx(PIN) which have essentially the same interface. They read the DHT connected to PIN, and fill the two class members temperature and humidity. Multiple reads from these class members (H & T) will return the same (previous) values until a new read is done.

The readXX() functions return:

DHTLIB_OK (0) : if the sensor sample and its checksum is OK.
DHTLIB_ERROR_CHECKSUM (-1) : if the checksum test failed. This means that data was received but may be incorrect.
DHTLIB_ERROR_TIMEOUT (-2) : if a timeout occurred, communication failed.
In case of a DHTLIB_ERROR_TIMEOUT, humidity and temperature will both get the value DHTLIB_INVALID_VALUE. In case of DHTLIB_ERROR_CHECKSUM the values of humidity and temperature are left unchanged as it is impossible to determine which byte failed in the checksum. It is up to the programmer to decide what to do. One can compare with previous value, but better reread the sensor.

The class is kept simple and with one instance it is possible to read multiple, even different sensors, provided each sensor has a separate pin. Please note that the values of temperature and humidity will be overwritten with every call. So either copy those values asap of use an instance of the class per sensor. Since version 0.1.04 the temperature and humidity are set to DHTLIB_INVALID_VALUE if there is a DHTLIB_ERROR_TIMEOUT to make explicit there is something wrong.

the DHT.read() is in the library dht.cpp:

Just to end with, above screensot shows 3 DHT11 sensors with OTA modified code with 6 second intervals in different rooms at different temperatures. The erratic yellow line is the unmodified DHT11 sensor with only a one second interval. All 4 ESP’s used were ESP-01 using GPIO2 as the data pin.
There is one strange anomaly, all 4 sensors give very odd Humidity readings, i mean like from 0, 25, 45, 100.
The DHT11 modules were bought in one batch as were the ESP-01’s as were the 240v to 5v usb wall socket adapters, the wiring and cables are exactly the same gauge and lengths, the SOT223 5v to 3.3V are soldered to the same batch of protoboards in exactly the same way.

so i think that when @Gunner commented that the DHT11’s are not very accurate at the beginning of this topic just about sums it up.

Any ideas why the humidity readings are so not right?

Technically a DHT11 requires a min 1 second wait between reads, but I recommend min 2-5 seconds

I don’t see any Humidity readings on your graph… but inaccurate is inaccurate…

- Good for 20-80% humidity readings with 5% accuracy

…All the tweaking of your settings will not change that outcome much.

What is that saying,… “Like putting lipstick on a pig” :pig:

At least ‘upgrade’ to DHT22 :stuck_out_tongue: if you want semi-accurate (I have had OK results with mine)… or discrete temp and humidity sensors of other types.