Reading DHT22 sensor with interruption

Add details :
• Hardware model + communication type: WEMOS D1 mini connected to Home Wi-Fi Router
• Smartphone: Google Pixel 3
• Blynk server

Problem description:
• Reading DHT22 sensor with interruption. First, I believe that the sensor have problems, I change the sensor with one identical and the result are the same. I think that the code is the problem, please if someone with experience can help me to identify the problem. I add also the measurement from Serial Monitor.

Serial Monitor Result:

Code:

/*ESP8266 PIN:           VIRTUAL PIN
 * D0-->Relay_Fan           V0
 * D1-->Relay_Hot           V1
 * D2-->DHT Sensor          V2 -t, V3 -h
 * D4-->LED bord            V4
 * 
 * Setare Temperatura       V5
 * Manual/Automat           V6
 * Led Ventilatie           V7
 * Led Incalzire            V8
 * Ventilator               V9
 * Calorifer                V10
*/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

///////////////////////////////////////////////////////////////////////

char auth[] = ;
char ssid[] = ;
char pass[] = ;

///////////////////////////////////////////////////////////////////////

#define DHTPIN D3          
#define DHTTYPE DHT22  
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetLED ledV(V7);
WidgetLED ledC(V8);

int relayPinC = D7; 
int relayPinV = D8;
int ledBlink = D4; //LED Blink
int temp_threshold;
int manual_auto;
int pinCalorifer;
int pinVentilator;

////////////////////////////////////////////////////////////////////////

void printInformation()
{
  Serial.print("Temperatura= ");
  Serial.print(dht.readTemperature());
  Serial.print("°C");
  Serial.print("\t");
  Serial.print("Umiditate= ");
  Serial.print(dht.readHumidity());
  Serial.print("%");
  Serial.println();
}

////////////////////////////////////////////////////////////////////////

void sendAwekening()
{
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);
}

/////////////////////////////////////////////////////////////////////////

void sendAlarmInfo()
{
   if (isnan(dht.readTemperature()) || isnan(dht.readHumidity())) 
      {
       Serial.println("Eroare la citire Senzor !");
       Blynk.notify("Eroare la citire Senzor !");
       return;
       }

   if ((dht.readTemperature()) < 16) 
      {
       Serial.println("ATENTIE, temperatura scazuta !");
       Blynk.notify("ATENTIE, temperatura scazuta !");
       return;
       }

   if ((dht.readTemperature()) > 28) 
      {
       Serial.println("ATENTIE, temperatura crescuta !");
       Blynk.notify("ATENTIE, temperatura crescuta !");
       return;
       }

   if (manual_auto == 0) 
      {
       Serial.println("ATENTIE, mod MANUAL selectat !");
       Blynk.notify("ATENTIE, mod MANUAL selectat !");
       return;
       }
}

////////////////////////////////////////////////////////////////////////

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  Blynk.virtualWrite(V3, h);
  Blynk.virtualWrite(V2, t);

if(manual_auto == 1)
     {
        if ((t) > temp_threshold) 
            {
            digitalWrite(relayPinV, HIGH); 
            ledV.on();
            }
        else 
            {
            digitalWrite(relayPinV, LOW); 
            ledV.off();
            }

        if ((t) < temp_threshold) 
            {
            digitalWrite(relayPinC, HIGH); 
            ledC.on();
            } 
        else {
            digitalWrite(relayPinC, LOW); 
            ledC.off();
             }
      } 
 else
      {
        if (pinVentilator == 1) 
           {
           digitalWrite(relayPinV, HIGH); 
           pinCalorifer=0;  
           } 
        else 
           {
           digitalWrite(relayPinV, LOW);
           pinCalorifer=1; 
           }

        if (pinCalorifer == 1) 
         {
         digitalWrite(relayPinC, HIGH);
         pinVentilator=0;
         } 
        else 
         {
         digitalWrite(relayPinC, LOW);
         pinVentilator=1; 
         }
      }
}

///////////////////////////////////////////////////////////////////////////

//Releu Calorider/Incalzire
BLYNK_WRITE(V9) 
     {
      pinCalorifer = param.asInt(); 
     }

//Releu Ventilator/Racire
BLYNK_WRITE(V10) 
     {
      pinVentilator = param.asInt(); 
     }

//Citire Valoare de Referinta
BLYNK_WRITE(V5)   
    {
    temp_threshold = param.asInt();
    }
    
//Setare Manual-Automat
BLYNK_WRITE(V6)   
    {
    manual_auto = param.asInt();
    }

///////////////////////////////////////////////////////////////////////////

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  
  pinMode(relayPinC, OUTPUT);
  pinMode(relayPinV, OUTPUT);
  pinMode(ledBlink, OUTPUT);
  digitalWrite(relayPinC, LOW);
  digitalWrite(relayPinV, LOW);
  digitalWrite(ledBlink, LOW);
  manual_auto=1;
  
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendAwekening);
  timer.setInterval(1000L, sendAlarmInfo);
  timer.setInterval(1000L, printInformation);
}

////////////////////////////////////////////////////////////////////////////

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

Thanks a lot !

What happens if you comment/out the first three timers in your void setup, and change the interval for the printInformation timer to 5000L ?

Pete.

Hi Pete,

This is the result of modification, nothing is change, the value is send with interruption:
‘’
///////////////////////////////////////////////////////////////////////////

void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();

pinMode(relayPinC, OUTPUT);
pinMode(relayPinV, OUTPUT);
pinMode(ledBlink, OUTPUT);
digitalWrite(relayPinC, LOW);
digitalWrite(relayPinV, LOW);
digitalWrite(ledBlink, LOW);
manual_auto=1;

// timer.setInterval(1000L, sendSensor);
// timer.setInterval(1000L, sendAwekening);
// timer.setInterval(1000L, sendAlarmInfo);
timer.setInterval(5000L, printInformation);
}

////////////////////////////////////////////////////////////////////////////

‘’
image

You’re using pin D3 (GPIO 0) whic isnt reccomended for sensors, see this:

Also, can you check that your DHT library is up to date?

Pete.

I think the problem might be you repeatedly and unnecessarily read the DHT sensor in high speed. You also might change the pin from D3 to D1, as suggested by @PeteKnight, if you still experience the issue.

Try this version of your sketch

/*ESP8266 PIN:           VIRTUAL PIN
 * D0-->Relay_Fan           V0
 * D1-->Relay_Hot           V1
 * D2-->DHT Sensor          V2 -t, V3 -h
 * D4-->LED bord            V4
 * 
 * Setare Temperatura       V5
 * Manual/Automat           V6
 * Led Ventilatie           V7
 * Led Incalzire            V8
 * Ventilator               V9
 * Calorifer                V10
*/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

///////////////////////////////////////////////////////////////////////

char auth[] = "";
char ssid[] = "";
char pass[] = "";
///////////////////////////////////////////////////////////////////////

#define DHTPIN              D1        //D3          
#define DHTTYPE             DHT22  
DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;

WidgetLED ledV(V7);
WidgetLED ledC(V8);

#define relayPinC     D7 
#define relayPinV     D8
#define ledBlink      D4    //LED Blink

int temp_threshold;
int manual_auto;
int pinCalorifer;
int pinVentilator;

float DHT_temp;
float DHT_humid;

////////////////////////////////////////////////////////////////////////

void printInformation()
{
  Serial.println("Temperatura= " + String(DHT_temp, 1) + "°C \t Umiditate= " + String(DHT_humid, 1) + "%");
}

////////////////////////////////////////////////////////////////////////

void sendAwekening()
{
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);
}

/////////////////////////////////////////////////////////////////////////

void sendAlarmInfo()
{
  if (isnan(DHT_temp) || isnan(DHT_humid)) 
  {
    Serial.println("Eroare la citire Senzor !");
    Blynk.notify("Eroare la citire Senzor !");
    return;
  }
  
  if ((DHT_temp) < 16) 
  {
    Serial.println("ATENTIE, temperatura scazuta !");
    Blynk.notify("ATENTIE, temperatura scazuta !");
    return;
  }
  
  if ((DHT_temp) > 28) 
  {
    Serial.println("ATENTIE, temperatura crescuta !");
    Blynk.notify("ATENTIE, temperatura crescuta !");
    return;
  }
  
  if (manual_auto == 0) 
  {
    Serial.println("ATENTIE, mod MANUAL selectat !");
    Blynk.notify("ATENTIE, mod MANUAL selectat !");
    return;
  }
}

////////////////////////////////////////////////////////////////////////

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

  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Failed to read from DHT sensor!");
  }
  else
  {
    DHT_temp  = t;
    DHT_humid = h;
    
    Blynk.virtualWrite(V3, DHT_humid);
    Blynk.virtualWrite(V2, DHT_temp);
  }

  sendAlarmInfo();
  sendAwekening();
  printInformation();

  if(manual_auto == 1)
  {
    if ((DHT_temp) > temp_threshold) 
    {
      digitalWrite(relayPinV, HIGH); 
      ledV.on();
    }
    else 
    {
      digitalWrite(relayPinV, LOW); 
      ledV.off();
    }
    
    if ((DHT_temp) < temp_threshold) 
    {
      digitalWrite(relayPinC, HIGH); 
      ledC.on();
    } 
    else 
    {
      digitalWrite(relayPinC, LOW); 
      ledC.off();
    }
  } 
  else
  {
    if (pinVentilator == 1) 
    {
      digitalWrite(relayPinV, HIGH); 
      pinCalorifer=0;  
    } 
    else 
    {
      digitalWrite(relayPinV, LOW);
      pinCalorifer=1; 
    }
    
    if (pinCalorifer == 1) 
    {
      digitalWrite(relayPinC, HIGH);
      pinVentilator=0;
    } 
    else 
    {
      digitalWrite(relayPinC, LOW);
      pinVentilator=1; 
    }
  }
}

///////////////////////////////////////////////////////////////////////////

//Releu Calorider/Incalzire
BLYNK_WRITE(V9) 
{
  pinCalorifer = param.asInt(); 
}

//Releu Ventilator/Racire
BLYNK_WRITE(V10) 
{
  pinVentilator = param.asInt(); 
}

//Citire Valoare de Referinta
BLYNK_WRITE(V5)   
{
  temp_threshold = param.asInt();
}
    
//Setare Manual-Automat
BLYNK_WRITE(V6)   
{
  manual_auto = param.asInt();
}

///////////////////////////////////////////////////////////////////////////

void setup()
{
  //Serial.begin(9600);
  // Use faster erial speed
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  
  pinMode(relayPinC, OUTPUT);
  pinMode(relayPinV, OUTPUT);
  pinMode(ledBlink, OUTPUT);
  digitalWrite(relayPinC, LOW);
  digitalWrite(relayPinV, LOW);
  digitalWrite(ledBlink, LOW);
  manual_auto=1;
  
  timer.setInterval(5000L, readdSensor);
}

////////////////////////////////////////////////////////////////////////////

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

@khoih thanks a lot for code update. I will try and I will write the feedback.

@PeteKnight thanks a lot for your observation regarding the Pin that I use, I’ve change the Pin to D1, and also I’ve update the library because are old.

First steps I've update the library and the result was successful without any interruption, with the same code. So, the main problems with interruption came from here, old version of library.
Second step I change the Pin. And also in this case is working without interruption.

I really appreciate your help !

Good.

There are some issues with your code though, and I’ll explain them here so that you understand how to fix them, and so that you know for future use…

The DHT sensors don’t like to be read too often, once every second for the DHT22 is about the most frequent, and every 5 seconds for the DHT11.

The most sensible approach, if you want to do multiple things with the temp and humidity data, is to take one reading, say every 5 seconds, then save the result to a Global variable (one that is declared at the top of your code).

What you are doing (and I know that some of this was a temporary measure) was to take a reading in many places:

It would be much better to do this:

temperature = (dht.readTemperature());
humidity = (dht.readHumidity());

then:

if (temperature < 16)
if (temperature > 28)
Serial.print(temperature);

Also, these timers are all trying to run at EXACTLY the same time:

Your MCU cant handle that, and at some point it will throw its toys out of the pram and stop cooperating.

There are many example on this foum about how to stagger timers, but in reality you only really need one timed function which takes a temperature reading, evaluates the data and sends it to Blynk and the serial monitor.

Pete.