DHT11 + ESP01 weird Output

Hello!

First I would like to introduce myself, my name is Chris, working at a bigger Express Company as an IT guy. I have no experience with coding, thats why Im
looking for some help here.

Im 55 and my hobbies, besides playing with swords and things like that is now building a power wall for home use.

About my project:

I want to measure Temperature and humidity wireless, this is a project for Kenya where my mother in law tries to open a mushroom farm.

Hardware is a esp01 with a dht11 sensor shield. Powered at the moment by 3,3 V.

So far the wireless stuff works, also the webserver but I cannot find the mistake why Temperature and Humidity is shown wrongly:

Temperature: 2147483647°C

Humidity: 2147483647%

Maybe someone here can find the mistake in the code, maybe the sensor is not working properly:


#include <DHTesp.h>

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"

// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

/*Put your SSID & Password*/
const char* ssid = "XXXXXX";  // Enter SSID here
const char* password = "XXXXXX";  //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = 2; 
               
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);                

float Temperature;
float Humidity;
 
void setup() {
  Serial.begin(115200);
  delay(100);
  
  pinMode(DHTPin, INPUT);

  dht.begin();              

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

}
void loop() {
  
  server.handleClient();
  
}

void handle_OnConnect() {

 Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity 
  server.send(200, "text/html", SendHTML(Temperature,Humidity)); 
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float Temperaturestat,float Humiditystat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>ESP8266 Weather Report</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<div id=\"webpage\">\n";
  ptr +="<h1>ESP8266 NodeMCU Weather Report</h1>\n";
  
  ptr +="<p>Temperature: ";
  ptr +=(int)Temperaturestat;
  ptr +="°C</p>";
  ptr +="<p>Humidity: ";
  ptr +=(int)Humiditystat;
  ptr +="%</p>";
  
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}

Thank you in advance.

Chris

First before checking your code you probably should power it with 5V cause sometimes 3.3V is not enough for the DHT11 and the ESP01. That's the first step of advice from me.

Please do not copy paste your code. You can learn to post code in the right way:-
https://forum.arduino.cc/index.php?topic=333464.0

Hello!

Thanks for the answer, Problem the esp8266 is directly connected to the dht11 and it cannot stand 5 Volt.

The dht11 is specified between 3 and 5 V.

Let me check with a seperate sensor.

Brgds

Chris

Has anyone had any luck with this? I am doing a similar project--DHT11 with ESP8266. As such, the DHT11 has to be powered by 3.3 volts (there are no 5V ports on the ESP8266).

I'm trying to set it up as a temperature/humidity sensor with email alerts when it crosses a certain threshold. I've got the WiFi and email functions nailed down just fine. It also prints to the serial monitor which is read through TeraTerm and logged in a CSV file so I can keep track of the environmental conditions over time.

The DHT11 returns a good set of values on the first loop (Fahrenheit, Celsius, and humidity), but as it loops through, the numbers become erratic and very high; they don't progressively get worse, they just either throw irrationally high numbers (humidity of 153%) or wildly high numbers in the millions, or nan. I put a nan check in the code, which is below.

I've removed the sections of code that refer to my WiFi and the email server.

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

#define Type DHT11
int sensePin=D3;
int cycle=15;
DHT HT (sensePin, Type, cycle);

int tempF;
int tempC;
int humidity; 
int setTime=500;
int dt=1000;
int longDelay=3000; 

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

void loop (); {

  delay (longDelay); 
  tempF = HT.readTemperature(true); 
  tempC = HT.readTemperature();
  delay (dt);
  humidity = HT.readHumidity(); 

  while((tempF>=212) && (humidity>=100) && (tempC>=100)) {
    delay (longDelay);
    tempF = HT.readTemperature(true);
    tempC = HT.readTemperature();
    delay (dt);
    humidity = HT.readHumidity();
    Serial.println("Failed to read from sensor. Rereading environment. ");

  }
  
if (isnan(humidity) || isnan(tempF) || isnan(tempC)) {
  Serial.println("Failed to read from DHT sensor!");
  return; 
  }
 
Serial.print(" , ");  
Serial.print(tempF);
Serial.print(" F , ");
Serial.print(tempC);
Serial.print(" C, ");
Serial.print(humidity); 
Serial.println(" % "); 

}

I've tried using various sensePins for reading the DHT11 (D2, D3, and D5) all with the same result. I have multiple DHT11's, so I have tested it across several sensors with the same result. I assume the wiring is good, since I'm able to get a good reading the first loop through. Also, the DHT11s I'm using have an integrated pullup resistor, though I tried adding one just in case when it wasn't working, and that didn't work. I have tried messing with the "cycle" value in my code, anywhere between 15 and 30 which seemed to be the normal values, and I couldn't find a number that made it work.

I assume there's some sort of degradation I'm missing in the code as it loops through, but I'm a beginner to Arduino and am at my wit's end with this.

Any help would be very much appreciated!!!

I think I found my issue--I hadn't initialized the HT object in the setup with HT.begin() . That seems to have it working now. Hope this helps someone else.

1 Like