0

I have DHT22 sensor attached to an ESP8266. When running a sketch that read the sensor values, it suffer from frequent hangs (something like each 1 second).

All outputs are stopped (Sound from speaker or even the Serial output) during those hangs.

Here is a small Sketch to reproduce:

#include "DHT.h"
DHT dht(D1,DHT22);
void setup(){
  Serial.begin(115200);
}    
void loop(){
  Serial.println(dht.readTemperature(true));
  Serial.println(dht.readHumidity());
  delay(1);
}

P.S. it is not about the small delay(1). Even if I read the sensor values each 1 second, this problem will occur. To make sure, you may run this example:

#include "DHT.h"
DHT dht(D1,DHT22);

void setup(){
  Serial.begin(115200);
}

unsigned long last = 0UL;
void loop(){
  if(millis() - last > 1000){
      Serial.println("#########" + String(dht.readTemperature(true)));
      Serial.println("#########" + String(dht.readHumidity()));
      last = millis();
  }
  Serial.println(String(rand()));
  delay(10);
}

After flashing it, opens serial monitor and you can see clearly the hangs (I put random number printing so your eyes can not cheat you by an cashed old value;) )

  • I have something with my nodemcu. Every day, at a random time, I get a shift in the values reported to thinger. Very odd, and I haven't got to the bottom of it yet. – Majenko May 5 '18 at 15:04
  • @Majenko Does your setup also include a DHT22 with this library? – TisteAndii May 5 '18 at 18:02
  • Certainly a dh22 though I don't know if it's the same library. – Majenko May 5 '18 at 18:05
  • 1
    I just checked the library and it seems it disables all interrupts briefly while reading. That would explain the Serial (and other) blips, I think. It's not only blocking but also prevents others from interrupting it. – TisteAndii May 5 '18 at 18:08
  • does it hang if you explicitly disable wifi (or use a brand-new ESP)? – dandavis May 6 '18 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.