Non Blocking DHT21 sensor

Hi,
i am trying to program DHT21 sensors with Nodemcu 12E
i power two sensors by 3.3V, with the following code using PietteTech library
the sensors connected to Pin 4 and 5
initially code working fine, display first reading, and some time two then stop working i.e. display (Is dht21 connected?)
any helps?

#include "PietteTech_DHT.h"

// system defines
#define DHTTYPE  DHT21           // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN1   4              // Digital pin for communications
#define DHTPIN2   5              // Digital pin for communications
#define REPORT_INTERVAL 2000 

// to check dht
unsigned long startMills1;
unsigned long startMills2;
float t1, h1, d1;
float t2, h2, d2;
int acquireresult1;
int acquirestatus1;
int acquireresult2;
int acquirestatus2;

//declaration
void dht_wrapper1(); // must be declared before the lib initialization
void dht_wrapper2(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT1(DHTPIN1, DHTTYPE, dht_wrapper1);
PietteTech_DHT DHT2(DHTPIN2, DHTTYPE, dht_wrapper2);
// globals
bool bDHTstarted1;       // flag to indicate we started acquisition
bool bDHTstarted2;
bool active_wrapper;
// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper1() {
  active_wrapper = false;
  DHT1.isrCallback();
}
void dht_wrapper2() {
  active_wrapper = true;
  DHT2.isrCallback();
}
void setup() {
  startMills1 = millis();
  startMills2 = millis();
  Serial.begin(115200);

  /*
    while (!Serial) {
    yield(); // wait for serial port to connect.
    }
  */

  Serial.println("");
  Serial.println("DHT Example program using DHT.acquire and DHT.acquireAndWait");
  Serial.println("");
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  // delay 2 sec before start acquire
  while ( (millis() - startMills1) < 2000 ) {
    yield();
  }

  // blocking method
  acquirestatus1 = 0;
  acquireresult1 = DHT1.acquireAndWait(1000);
  if ( acquireresult1 == 0 ) {
    t1 = DHT1.getCelsius();
    h1 = DHT1.getHumidity();
    d1 = DHT1.getDewPoint();
  } else {
    t1 = h1 = d1 = 0;
  }
    acquirestatus2 = 0;
  acquireresult2 = DHT2.acquireAndWait(1000);
  if ( acquireresult2 == 0 ) {
    t2 = DHT2.getCelsius();
    h2 = DHT2.getHumidity();
    d2 = DHT2.getDewPoint();
  } else {
    t2 = h2 = d2 = 0;
  }
}

void loop() {
  if (bDHTstarted1) {
    acquirestatus1 = DHT1.acquiring();
    if (!acquirestatus1) {
      acquireresult1 = DHT1.getStatus();
      if ( acquireresult1 == 0 ) {
        t1 = DHT1.getCelsius();
        h1 = DHT1.getHumidity();
        d1 = DHT1.getDewPoint();
      }
      bDHTstarted1 = false;
    }
  }

  if ((millis() - startMills1) > REPORT_INTERVAL) {
    if ( acquireresult1 == 0 ) {
      Serial.println("");

      Serial.print("1- Humidity (%): ");
      Serial.println(h1);

      Serial.print("Temperature (oC): ");
      Serial.println(t1);

      Serial.print("Dew Point (oC): ");
      Serial.println(d1);

    } else {
      Serial.println("Is dht21 connected?");
    }
    startMills1 = millis();

    // to remove lock
    if (acquirestatus1 == 1) {
      DHT1.reset();
    }

    if (!bDHTstarted1) {
      // non blocking method
      DHT1.acquire();
      bDHTstarted1 = true;
    }
  }

    if (bDHTstarted2) {
    acquirestatus2 = DHT2.acquiring();
    if (!acquirestatus2) {
      acquireresult2 = DHT2.getStatus();
      if ( acquireresult2 == 0 ) {
        t2 = DHT2.getCelsius();
        h2 = DHT2.getHumidity();
        d2 = DHT2.getDewPoint();
      }
      bDHTstarted2 = false;
    }
  }

  if ((millis() - startMills2) > REPORT_INTERVAL) {
    if ( acquireresult2 == 0 ) {
      Serial.println("");

      Serial.print("2- Humidity (%): ");
      Serial.println(h2);

      Serial.print("Temperature (oC): ");
      Serial.println(t2);

      Serial.print("Dew Point (oC): ");
      Serial.println(d2);

    } else {
      Serial.println("Is dht21 connected?");
    }
    startMills2 = millis();

    // to remove lock
    if (acquirestatus2 == 1) {
      DHT2.reset();
    }

    if (!bDHTstarted2) {
      // non blocking method
      DHT2.acquire();
      bDHTstarted2 = true;
    }
  }
}

You need to post a link to the documentation for the library you are using.

...R

Thanks Mr Robin,
The i try many labraries and similar issue, but what I found from searching many people think this best library,
This is the link of the library that used
https://github.com/chaeplin/PietteTech_DHT-8266

Where i didnt need any significant delay more than few ms in the sketch

@Sayed, I don't know anything about ESP8266 programming. Hopefully someone who does will come along. Or ask your question on the ESP8266 Forum.

I also suggest you edit your title (modify your Original Post) to include Nodemcu 12E so that people like me don't think yours is an Arduino board problem.

...R

Which pins are you connecting the dht sensors to? It's confusing, but on NodeMCU boards, if you refer to pins 4 and 5 in an Arduino sketch, this refers to the pins marked D1 and D2 on the board itself, not the pins marked D4 and D5.

How far from the board are the sensors placed? If using INPUT_PULLUP instead of external 4K7 pull-up resistors, they may not be strong enough if using long wires to the sensors, e.g. 0.5m or more. So maybe try external 4K7 or even 3K3 pull-up resistors.

Robin, thank youfor your advise, Yes Mr paul it is D1, D2, i use it as pull up in the sketch so i didn't use external pull-up ( i try to use 10k to connect dht pin to 3.3V) and it was same result The sensor about 15 cm from board, So code is ok? Timing and order?

I use 4.7kohm pull up to 3.3V it work fine with one sensor that seems to not fail, but the other one read one reading and next time fail and so on,

cant remeber what rezistor i used but i used:

#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>

#define DHTTYPE DHT21

diffrent lib as you can see and 5s delay after each reading ,it works to this day with no NULL spaces or any trouble ,was working with node MCU v3 pin 5

const int DHTPin = 5;