DHT11 (Coding) fail

Hy guys, I need some help. I want meassure the temperature and the humidity with my DHT11 but it does not work. First of al I wrote a little program to watch the data with ma wemos d1 mini to see if it works. It works well I found out. After I would transfer the data to Blynk-App. For this I used the example from the sketch generator. My skatch looks now llike this:

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

char auth[] = "b3c2ae22684a4e7ba53f45714b2.....";

char ssid[] = "mySSID";
char pass[] = "mykeword";

#define DHTPIN 3                           // I connected the data pin on D3 on my wemos d1 mini
#define DHTTYPE DHT11    
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V8, h);
  Blynk.virtualWrite(V9, t);
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(1000L, sendSensor);
} 

After I uploaded the sketch and there I got no fault indication. In App I use two GAUGE. One for humidity (V8) and one for temperature (V9). Al data I would show also in a History Graph. When I start I do not receive any data although I am connected with WIFI and wemos gets power. The example pushDataonRequest worked so I think it locates somewhere other. In Serial Monitor I get the massage: Failed to read from DHT sensor!
Could someone help me. Now I paid also 5€ for mor energie now I want also realize my project.
Thanks for you and sorry for my disturbance.
Keep programming! :slight_smile:

Write in line 30
DHT dht(DHTPIN, DHTTYPE, 30);

You might want to add a loop to get things started! :wink:

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

I had a void loop(). I only cut :blush:

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "b3c2ae22684a4e7ba53f45714b2623e1";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Fritzbox3272";
char pass[] = "67033709486218110510";

#define DHTPIN 3         // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE,30);
BlynkTimer timer;

// 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 sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V8, h);
  Blynk.virtualWrite(V9, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

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

Do you meant like this? It also does not work. :frowning:
Could be an error that al included librarys are orange but only the library #<BlynkSimpleEsp8266.h> stands in black? I do not understand…I installed al required librarys. Can there be the fault?

I don’t think anyone understand the hows and whys that Arduino IDE displays stuff :wink: … but it is NOT an issue to worry about.

Also, the DHT (particularly the 11) is a really slow sensor… it needs time to process a reading and doesnt react to change quick. Typically no sense in reading it any quicker then 5 second intervals… or even higher.

And depending n the sensor you have, if it doesn’t come mounted to a PCB, then you may need to add in a 10k resistor between VCC and Signal pins

Finaly, please remember to format your pasted code for proper forum viewing…

Blynk - FTFC

I have compared this code with one I used, and it’s basically the same, except I have used GPIO2 and V5/6, so it should work out of the box. What MCU are you using? And what readings are you getting in your serial monitor? I think something is wrong with your hardware setup, as code is fine…

What does work? I normally add a lot of Serial.println(); when trying to pinpoint what’s wrong. I’d probably add something like this for the sensors:

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

  Serial.print("Got humidity? - ");
  Serial.println(h);
  Serial.print("Got temp? - ");
  Serial.println(t);

  ...
}

I’m not sure what the DHT returns if the reading is faulty, but if it is a number, isnan() would returns false (=OK) I guess…

As @Gunner mentioned, loosen up the timer. If you are using Adafruit’s library, they don’t allow you to read it faster than once every 2 seconds. If you read it faster, it will return the last reading instead. For this reason it’s kinda stupid of the to have a timer set to 1 second in their example code…

I wrote in another thread that the Arudino libraries seems to be a better choice. I don’t have any DHT sensors so can’t confirm it myself, but take a look at my two posts here:

I tried your approach to solving my problem but in serial monitor I get the message.
Got temp? - nan
Got humidity? - nan
Got temp? - nan

and so on. That means that I get no right data from the sensor right?
I do not understand because if I try to watch the results only with the wemos d1 mini I get results. There I use the following sketch.

#include “DHT.h” //bibrary
#define DHTPIN D2 //Pin der mit Microcontroller kommuniziert
//#define DHTPIN D2 bei Messung mit Wemos D1 mini
//#define DHTPIN 2 bei Messung mit Arduino UNO R3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(“DHT11 test!”);
dht.begin();
}

void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);

if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Error”);
return;
}
Serial.print(" Humidity in %:");
Serial.print(h);
Serial.println(" % “);
Serial.print(” Temperature in Grad:");
Serial.print(t);
Serial.println(" *C “);
Serial.print(” Temperature in Fahrenheit:");
Serial.print(f);
Serial.println(" F “);
Serial.println(”");
}

It works well only with the communication with Blynk I have proplems
:persevere: A problem with the hardware I can suspend

Got code formatted for forum viewing? - nan

Using timer function instead of delay? - nan

1 Like

:laughing: :stuck_out_tongue_winking_eye: :rofl:

Btw, doesn’t isnan() requires math.h to compile?!

DHT dht(DHTPIN, DHTTYPE, 30);
Give a space between , 30

The spacing is for visual clarity. It will not affect functionality… in fact the 3rd parameter (30) is ignored as it is in reference to timing in a much older library and it now taken care of in the library itself. The allowance of it, if presented, is just for backward compatibility.