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!