DHT22 with ESP 8266 shows often nan

Hello
I use my ESP8266 for two sensors. BMP180 and DHT22

The BMP180 works great!
but the DHT22 shows often "nan"

Do you know, whats the reason? i red in other threads that can be normal... is that correct?
thank you a lot

i send the data all 1000ms. is this to often?

thank you

You need to give more details of that!

What, exactly, is "it"?
How & where, exactly, does "it" "show" this?

Show your code.

1 Like

Post your annotated schematic as well be sure to show all connections and power connections. Also post links to technical information on each of the hardware devices.

1 Like

If you are buying this from a vendor... search for another vendor.

1 Like

Hello
thanky for yur answers.

My attempts:

  1. change USB-Cable,
  2. check data cable
  3. check pin
  4. tried dhtesp.h
  5. tried DHT sensor library from arduino

The problem is always the same:
When i connect it with my pc and check the serial output, all works.
If i connect it with my usb-charger and check, if the ardunio send the value via http, it also works, BUT
after a few minutes, i always get nan

this is my code:

//WIFI und HTTP ESP8266 Libary ##############################
  #include <ESP8266WiFi.h>
  #include <WiFiClientSecure.h>
  #include "certs.h"
  #include <ESP8266HTTPClient.h>
//WIFI ENDE#################################################


//WLAN und HTTP############################################
//#########################################################
  #ifndef STASSID
  #define STASSID "mynetwork"
  #define STAPSK "mypwd"
  #endif

  const char* ssid = STASSID;
  const char* password = STAPSK;
  X509List cert(cert_DigiCert_Global_Root_CA);
  HTTPClient http; 
  WiFiClient client;
//WLAN und HTTP ENDE#######################################
//#########################################################


//DHT22 Begin ##############################################
//##########################################################
  //#include "DHTesp.h"
  //DHTesp dht;
  #include <Adafruit_Sensor.h>
  #include <DHT.h>

#define DHTPIN 4     // Digital pin connected to the DHT sensor connect it on D2
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
//DHT22 Ende ###############################################
//##########################################################


// ZEITVERZOEGERUNG, d.h. falls bspw. nur alle 10sekunden ein request erfolgen soll
//##########################################################
  //delay Zeit festlegen
  int loop_verzeogerung = 1000;
  //festsetzen wie viel verzoegerung zwischen den http request vergehen soll
  int request_delay_max = 10000;
  //variable welche mitzählt bis request_delay_max erreicht wird
  int request_delay_min = 0;
  //falls staendig error auftaucht
  int count_error = 0;
  bool reset_request_delay_min  = false;
// ZEITVERZOEGERUNG Ende####################################
//##########################################################





void setup() {

//Serial begin##############################
  Serial.begin(115200);

//DHT22
dht.begin();

//WLAN connection aufbauen und ausgeben#####
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  //WLAN ENDE##############################


} 
//void setup ENDE

void loop() {

//ZEITVERZOEGERUNG ###################################################################
//ALLES WAS LANGSAMER ALS NORMALER LOOP GESCHEHEN SOLL################################
//####################################################################################
 if (request_delay_min >= request_delay_max){
//zähler zurücksetzen, 
request_delay_min = 0;
reset_request_delay_min = false;

  //############### Hier alles was Zeitverzoegert erfolgen soll##################
              //DHT22 Begin ##############################################
            //##########################################################
                float luftfeuchtigkeit_dht22 = dht.readHumidity();
                float temperatur_dht22 = dht.readTemperature();

                Serial.print("Temperature_DHT22 = ");
                Serial.print(temperatur_dht22);
                Serial.println(" *C");
                
                Serial.print("Luftfeuchtigkeit = ");
                Serial.print(luftfeuchtigkeit_dht22);
                Serial.println(" %");
            //DHT22 Ende ##############################################
            //##########################################################

            
            // SENDE DHT_22 Temp_aussen und Luftfeuchtigkeit MITTELS HTTP#####################################################  
                //Check WiFi connection status
                if(WiFi.status()== WL_CONNECTED){
                  HTTPClient http;
                   String url = "http://10.0.0.142/terminal/includes/helpers/DHT22_1.php";
                   String temp_url_part = "?temperatur=";
                   String luftfeuchtigkeit_url_part = "&luftfeuchtigkeit=";
                   String serverurl = url + temp_url_part + temperatur_dht22 + luftfeuchtigkeit_url_part + luftfeuchtigkeit_dht22;
                   Serial.println(serverurl);
                 
                  // Your Domain name with URL path or IP address with path
                  http.begin(client, serverurl);
                  
                  // Send HTTP GET request
                  int httpResponseCode = http.GET();
                  
                  if (httpResponseCode>0) {
                    Serial.print("HTTP Response code: ");
                    Serial.println(httpResponseCode);
                    String payload = http.getString();
                    Serial.println(payload);
                  }
                  else {
                    Serial.print("Error code: ");
                    Serial.println(httpResponseCode);
                  }
                  // Free resources
                  http.end();
                }
                else {
                  Serial.println("WiFi Disconnected");
                }
            //SENDE Temp_aussen und Luftfeuchtigkeit ENDE##############################################################


// ENDE VON DEN AUSGABEN - hier/darunter nichts mehr eintragen################################
//###################################################################################
//###################################################################################
//ZEITVERZOEGERUNG ##################################################################
//request_delay_max mit der verzoegerungszeit addieren, falls er nicht zurueckgesetzt wurde
  if (reset_request_delay_min != true){
      request_delay_min = request_delay_min + loop_verzeogerung;
  }

//ZEITVERZOEGERUNG ENDE##########################################
//Delay für jede normale Schleife 
delay(loop_verzeogerung);

}//Void loop Ende

please be not surprised.... the delay is 1000ms. so it replay all 1 second. i use a if clause to check wenn the loop reaches a specific time, becuase i use multiple sensors.
Some sensors need to loop all 1000ms

thank you a l ot

DHT22 has 2000ms refresh.

Yes the delay is 1000ms.
But in the void loop is a if function which check, if 10 000ms are done and than it read the dht Infos.
So I read the dht only all 10 000ms

Still unclear what that actually means!

What, exactly, do you see? Where, exactly, do you see this?

1 Like

in my code, you can see, that i send the data to a php file via http-reuest.

if i plug my wemos d1 with the charger, it will send the data to my file, after the wemos conntected to my network.
that works.

than i get for the first 5minutes the correct data.
After the 5 minutes, it don't work .
I can see this, because the wemos don't send the humaditiy and temperature. i only get nan

"Not a Number"

I know emwhat nan means.
The question is, why i get nan always....
It only works for a few mintues. Than i always get nan....

Today i tested my last ideas.

Changing usb charger plug
Changing usb cable
Check all cables
Use another sensor
(I bought a package with two pieces for only 10€)
Second piece, same problem... now i orderd a htu21d

Every time you read the DHT22, print millis... or better... print the time difference between DHT22 readings.

Here:

                Serial.println(millis() / 1000);
                float luftfeuchtigkeit_dht22 = dht.readHumidity();
                float temperatur_dht22 = dht.readTemperature();

Does your DHT have a built in resistor?
Try adding one.
Link on how:

I have this:

How can I check if I need a extra resistance?
Should i test it with 10k resistance and see what happens?

I think on this plantine is a resistor. But i'm not sure if it has 5ohm or 10kohm
should i try to use an extra resitor with 500ohm or 10kohm?

can i simply add the existent ohm value with my extra resistance????
for e.g.

if on the plantine is a 500ohm resistor and i add a 10kohm... do i have 1,5kohm?

i read, it will be more stable with higher resistance, but it will be need more electricity.

so its better for me to use a 10k resistance, correct? or heat it too much up, if it has 1,5k together?

Not really? It would be 10,500 Ω? I think that since the resistor is a pull up/down it should not be a problem. Just try it.

okay. thank you!
i checked the sensor for a few seconds.
Unfortunately, it doesn't show how much ohm the resistance has.

if the resistance on the platine has 10k and i also use a 10k resistance, i would get 20k.
Is it dangerous becuase it can be burn? so much heat because the high resistance

okay. i use my multimeter and put the pins on the resitance. it shows 3,37k
than i put one pin from the multimeter on the + and the other pin on the data. It shows also 3,37k
does it means, the resitancd hav 3.37k????
is it a possible value?
Thank you

That is very large spread….
There are several resistors on the PCB:

Maybe like this?
I know that DHT22’s usually have a built in one but it should not hurt.

Could you attach a picture of your wiring?
Schematic diagram or several pictures from different angles.

Wiring... is so easy.
Only

  • pin to 3v from wemos
  • pin to gnd from wemos
    Data to D2

That's it
No resistor by me.

I have exactly the dht 22 on the pcb
Should I remove the pcb and set the resistance only by me
Or should I add a extra resistance ?