DHT11 chip not wanting to read sensor data with the ESP8266

Hello people,

I got myself an arduino uno a couple months ago and i've been really enjoying making things with it. In my new project i'm trying to read the temperature and humidity from the DHT11 sensor and displaying them on a simple webserver hosted on the ESP8266 (ESP-01 ESP8266)

But for the life of me I cant read the sensor data when I have the ESP board active. I get the error "Failed to read from DHT sensor!" When I switch to the arduino board it works perfectly fine and prints the sensor data to the serial monitor.

Here is the code snippet i'm talking about (from an example online):

#include "DHT.h"
#define DHTPIN 4     
#define DHTTYPE DHT11   

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println("DHT11 test!");

  dht.begin();
}

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

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

I also know that the ESP is working fine since I already have the simple webserver code setup and working. It is very frustrating because they both work alone, but I when I try to combine them with this code, it just doesn't work:

//DHT weather sensor
#include "DHT.h"
#define DHTPIN 4    
#define DHTTYPE DHT11   

//ESP8266 Wifi webserver
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

bool startServerOnAwake = true;
const char* ssid = "redacted"; //Enter Wi-Fi SSID
const char* password = "redacted"; //Enter Wi-Fi Password

//time loop
unsigned long previousMillis = 0;
const long sensorInterval = 1000;

DHT dht(DHTPIN, DHTTYPE);
ESP8266WebServer server(80);
float h, t, f;

void setup() {
  Serial.begin(115200);
  while (!Serial.available());

  dht.begin();
  if (!startServerOnAwake) return;
  StartServer();
}

void loop() {
  server.handleClient(); //Handling of incoming client requests
 
 //sampling rate of dht11 is 1hz
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= sensorInterval) {
    previousMillis = currentMillis;
    HandleWeatherData();
  }
}
void StartServer(){
  WiFi.begin(ssid, password);  //Connect to the WiFi network

  while (WiFi.status() != WL_CONNECTED) {  //Wait for connection
    delay(500);
    Serial.println("Waiting to connect...");
  }

  Serial.println("Webserver hosted on IP address: ");
  Serial.print(WiFi.localIP());  //Print the local IP
  Serial.println("");

  server.on("/", OnLoadClient); //Handle Index page
  server.onNotFound(HandleNotFound);

  server.begin(); //Start the server
  Serial.println("Server listening \n");
}

void OnLoadClient() {
  server.send(200, "text/html", SendSensorDataHTML(t, h, "18:22", "02/07/2022"));
}

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

void HandleWeatherData(){
  h = dht.readHumidity();
  t = dht.readTemperature();
  f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C \n");
}

String SendSensorDataHTML(float TemperatureWeb, float HumidityWeb, String TimeWeb, String DateWeb) {
  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 Global Server</title>\n";

  ptr += "</head>\n";
  ptr += "<body>\n";
  ptr += "<div id=\"webpage\">\n";
  ptr += "<h1>ESP8266 Global Server</h1>\n";

  ptr += "<p>Date: ";
  ptr += (String)DateWeb;
  ptr += "</p>";
  ptr += "<p>Time: ";
  ptr += (String)TimeWeb;
  ptr += "</p>";
  ptr += "<p>Temperature: ";
  ptr += TemperatureWeb;
  ptr += "C</p>";
  ptr += "<p>Humidity: ";
  ptr += HumidityWeb;
  ptr += "%</p>";

  ptr += "</div>\n";
  ptr += "</body>\n";
  ptr += "</html>\n";
  return ptr;
}

My dht11 connections are:
DATA => Arduino PIN 4
Power => Arduino 5V
GND => Arduino GND

ESP connections:
Power => Arduino 3V
GND => Arduinio GND
TX=> Arduino RX
RX=> Arduino TX

So in conclusion im trying to read the sensor data with the ESP8266 board active. Maybe its a wiring issue? Because I cant see potential errors in the code.

Thanks in advance!

1 Like

Hi,
as you mentioned i think its something with wiring. I found an Projekt with an resistor in it, maybe it helps... https://lastminuteengineers.com/esp8266-dht11-dht22-web-server-tutorial/

kind regards
finn

1 Like

Thanks! I will take a look at it.

This would be much easier to answer if it were in schematic form, not a list of numbers. I will take a SWAG and say I believe you have the wrong pin on the ESP as the pin numbers do not match the Arduinos. I also know the DHT11 has an open drain as an output so it will do exactly nothing until you put a pull up resistor something in the 4K range should work just fine. Many of the Arduinos have pull up resistors built in, probably (not sure) off by default on the inputs when initialized. The part is not super complicated to use but its timing is critical.

Try this link: ESP8266 DHT11/DHT22 Web Server Arduino IDE | Random Nerd Tutorials

1 Like

Take care. You are connecting a 5v device (Arduino UNO) to a 3.3v device (ESP8266).
According to your wiring you have to connect Arduino RX to ESP8266 TX and
Arduino TX to ESP8266 RX - BUT take care of the voltage 5v versus 3.3v.
Can be done by resistors or a chip.
I think you need to supply power (3.3v) to the ESP8266 fro a separate powersource.
The power regulator on Arduino UNO is too small for powering the ESP8266.

1 Like

Oops I totally didn't know that. I think will just redo the connections one at the time. And I will use one of those little power supply modules. Thanks for sharing!

I did the rewiring and it is now working. I'm so happy, this was so frustrating for me. Thank you for the guidance!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.