ESP32 DHT Server: Monitoring Temperature and Humidity

The “ESP32 DHT Server: Monitoring Temperature and Humidity” project relies on the ESP32 microcontroller, known for its strong Wi-Fi abilities. Through taking the advantage of the ESP32 possibilities, the code will construct a framework for remote environmental surveillance. By adding a DHT sensor, the project becomes a simple way to keep track of temperature and humidity in real-time. Inside the handleRoot() function, HTML content is created with spots for temperature and humidity values.

Once the Wi-Fi connection is established, the server starts responding to HTTP requests and shows the HTML content with the temperature and humidity info. This web server is developed to assist monitoring temperature and humidity by incorporating a DHT11 sensor.  The page that is being generated by the server dynamically provides real-time temperature and humidity readings collected from the DHT11 sensor. Anyone on the same local network can view this interface from their device, making it convenient for monitoring the environment. Adding MDNS makes it even simpler to find the device on the network. It emphasizes making code that’s easy to understand and use.



  • DHT11 VCC(+ symbol) → 3.3V on ESP32
  • DHT11 GND(– symbol) → GND on ESP32
  • DHT11 data pin(out symbol) → GPIO 26 on ESP32


  • 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.


#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,
    <meta http-equiv='refresh' content='4'/>\
    <meta name='viewport' content='width=device-width, initial-scale=1'>\
    <link rel='stylesheet' href='' integrity='sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr' crossorigin='anonymous'>\
    <title>ESP32 DHT Server</title>\
    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;}\
      <h2>ESP32 DHT Server: Monitoring Temperature and Humidity</h2>\
        <i class='fas fa-thermometer-half' style='color:#ca3517;'></i>\
        <span class='dht-labels'>Temperature</span>\
        <sup class='units'>&deg;C</sup>\
        <i class='fas fa-tint' style='color:#00add6;'></i>\
        <span class='dht-labels'>Humidity</span>\
        <sup class='units'>&percnt;</sup>\
      <h23>@ circuitrocks</h3>\
           readDHTTemperature(), readDHTHumidity()
  server.send(200, "text/html", msg);

void setup(void) {

  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {

  Serial.print("Connected to ");
  Serial.print("IP address: ");

  if (MDNS.begin("circuitrocks")) {
    Serial.println("MDNS responder started");
  server.on("/", handleRoot);

  Serial.println("HTTP server started");

void loop(void) {
  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 {
    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 {
    return h;


  • 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  Multi LED and Resistor in Parallel: A Quick Guide


GITHUB reference