ESP32

ESP32 DHT Server: Monitoring Temperature and Humidity

ESP32 DHT Server: Monitoring Temperature and Humidity

The “ESP32 DHT Server: Monitoring Temperature and Humidity” project uses an ESP32 microcontroller and a DHT11 sensor to track environmental data. For example, the ESP32 connects to a Wi-Fi network and hosts a simple web page. As a result, users can see real-time temperature and humidity readings from any device on the same network. In addition, this approach makes it easy to monitor conditions remotely and improves overall accessibility.

First, you’ll set up the ESP32 to connect to Wi-Fi using your SSID and password. Next, it launches an HTTP server and creates a route to handle incoming requests. Then, the server reads temperature and humidity values from the DHT11 sensor and generates an HTML page with the latest readings. Because the web page refreshes automatically every four seconds, users always get up-to-date information without needing to reload manually.

Finally, this project shows how to use the ESP32 to build a simple and effective IoT device for monitoring the environment. Moreover, by connecting to the internet and displaying data on a web page, it offers a practical and user-friendly way to check real-time conditions. Therefore, even beginners can learn to create useful projects that make everyday monitoring easier and more reliable.

Components

// Live from circuit.rocks shop

Connection:

  • VCC (+ symbol) → 3.3V on ESP32
  • GND (– symbol) → GND on ESP32
  • DHT11 data pin(out symbol) → GPIO 26 on ESP32
See also  Arduino GPS Location Tracker Using NEO6MV2

Note:

  • Install the “Esp32” in board manager (click the board NodeMCU-32S)
  • Install the “DHT sensor library” by Adafruit
  • Change the SSID according to the Wifi name and password of networks of yours.

Code:

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <DHT.h> const char *ssid = "CIRCUIT.ROCKS"; /*Change according to your WIFI name network*/
const char *password = "abcdefghij"; /*Change according to your WIFI pass network*/ WebServer server(80);
DHT dht(26, DHT11); void handleRoot() { char msg[1500]; snprintf(msg, 1500, "<html> <head> <meta http-equiv='refresh' content='4'/> <meta name='viewport' content='width=device-width, initial-scale=1'> <link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.7.2/css/all.css' integrity='sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr' crossorigin='anonymous'> <title>ESP32 DHT Server</title> <style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center;} h2 { font-size: 3.0rem; } p { font-size: 3.0rem; }.units { font-size: 1.2rem; }.dht-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px;} </style> </head> <body> <h2>ESP32 DHT Server: Monitoring Temperature and Humidity</h2> <p> <i class='fas fa-thermometer-half' style='color:#ca3517;'></i> <span class='dht-labels'>Temperature</span> <span>%.2f</span> <sup class='units'>&deg;C</sup> </p> <p> <i class='fas fa-tint' style='color:#00add6;'></i> <span class='dht-labels'>Humidity</span> <span>%.2f</span> <sup class='units'>&percnt;</sup> </p> <h23>@ circuitrocks</h3> </body>
</html>", readDHTTemperature(), readDHTHumidity() ); server.send(200, "text/html", msg);
} void setup(void) { Serial.begin(115200); dht.begin(); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status()!= WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("circuitrocks")) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.begin(); Serial.println("HTTP server started");
} void loop(void) { server.handleClient(); delay(2);//allow the cpu to switch to other tasks
} float readDHTTemperature() { // Sensor readings may also be up to 2 seconds // Read temperature as Celsius (the default) float t = dht.readTemperature(); if (isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return -1; } else { Serial.println(t); return t; }
} float readDHTHumidity() { // Sensor readings may also be up to 2 seconds float h = dht.readHumidity(); if (isnan(h)) { Serial.println("Failed to read from DHT sensor!"); return -1; } else { Serial.println(h); return h; }
}

Troubleshooting:

  • Double-check your hardware connections to ensure they are correct. Verify that the DHT11 sensor is connected properly to the ESP32 board and that there are no loose connections or wiring.
  • Look for any error messages or warnings that are output to the Arduino IDE’s Serial Monitor when you run your code. These messages can provide valuable information about what might be going wrong.
  • If possible, test each component of your setup individually to ensure they are functioning correctly. For example, you can write a separate test sketch to verify that the DHT11 sensor is reading temperature and humidity data accurately.
  • Review your code logic to ensure it is correct and follows the intended flow. Check for any logical errors or typos.
  • Ensure that you are using any required libraries correctly and that they are compatible with your hardware and software setup. Double-check library documentation and usage examples for any specific requirements or configuration settings.
See also  Smoke Detector CIRCUIT

GITHUB reference

Frequently Asked Questions

What does this ESP32 DHT Server: Monitoring Temperature and Humidity tutorial cover?

The “ESP32 DHT Server: Monitoring Temperature and Humidity” project uses an ESP32 microcontroller and a DHT11 sensor to track environmental data.

Why does my ESP32 reset randomly during the ESP32 DHT Server: Monitoring Temperature and Humidity sketch?

Usually brownout from a weak USB supply. Use a 5V/2A wall adapter or add a 470uF cap across VIN/GND. Second cause: WDT timeout — sprinkle yield() or vTaskDelay() in long loops.

Can I run ESP8266 code on an ESP32?

Mostly yes. ESP32 is API-compatible with most ESP8266 Arduino libraries plus dual core, BLE, and more I/O. Watch for analogRead range (ESP32 is 0-4095, 12-bit) and pin numbering.

// written by jomar

Jomar Zabala builds robots, line-followers, and microcontroller projects at Circuitrocks. He writes hands-on guides covering sensors, motor control, and embedded systems — the kind of bench-tested walkthroughs he wishes existed when he started with Arduino and ESP32.