ESP32 DHT22 WiFi Temperature & Humidity Monitor: Complete DIY Guide

Imagine checking the temperature and humidity of your home, greenhouse, server room, or baby's nursery from anywhere in the world — right from your smartphone. No expensive smart home hub. No monthly cloud subscription. Just a $7 DIY circuit you can build in under an hour.

In this hands-on guide from TechFix Hub, you'll learn how to build a complete ESP32-based WiFi monitoring station using the ultra-accurate DHT22 sensor. The ESP32 hosts its own web server, so you simply open a browser on any device connected to your WiFi network and see live readings instantly.

๐Ÿ’ก Why This Project?
• 100% local — your data never leaves your home network
• Ultra-low power: runs for weeks on a single LiPo battery
• No app required — works in any web browser
• Perfect beginner-friendly IoT project with real-world value

๐Ÿ“ฆ Components List (BOM)

All parts are affordable and available on AliExpress, Amazon, or your local electronics store:

ComponentSpecificationQtyEst. Price
ESP32 DevKit V130-pin, WiFi + BLE1$4.00
DHT22 (AM2302)Temp + Humidity sensor1$3.00
Resistor10kฮฉ ¼W (pull-up)1$0.05
Breadboard400 tie-points1$2.00
Jumper WiresMale-to-Female4$0.50
Micro USB CableFor programming & power1$1.00
⚠️ DHT22 vs DHT11: Don't confuse them! The DHT22 is far more accurate (±0.5°C vs ±2°C) and supports temperatures down to -40°C. The extra $1.50 is absolutely worth it.

๐Ÿ”Œ Circuit Wiring Diagram

The wiring is minimal — only 4 connections between the ESP32 and the DHT22:

DHT22 PinESP32 PinNotes
Pin 1 (VCC)3.3VDo NOT use 5V — ESP32 is 3.3V logic
Pin 2 (DATA)GPIO 4Connect 10kฮฉ pull-up resistor between DATA and VCC
Pin 3 (NC)Not connected (leave empty)
Pin 4 (GND)GNDCommon ground
๐Ÿ”ง Pro Tip: If your DHT22 module already has a built-in resistor (most breakout boards do), you can skip the external 10kฮฉ resistor. Check the board — if there are 3 pins instead of 4, the pull-up is already included.

๐Ÿ’ป Arduino Code (Complete & Ready to Upload)

First, install these libraries in Arduino IDE (Sketch → Include Library → Manage Libraries):

  • DHT sensor library by Adafruit (v1.4.6+)
  • Adafruit Unified Sensor (dependency)

Then, copy and paste this complete code:


#include <WiFi.h>
#include <DHT.h>

// ========== CONFIGURATION ==========
const char* ssid     = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

#define DHTPIN    4       // GPIO 4
#define DHTTYPE   DHT22

DHT dht(DHTPIN, DHTTYPE);
WiFiServer server(80);

// Timing
unsigned long lastRead = 0;
const long readInterval = 5000; // Read every 5 seconds

// Sensor data
float temperature = 0.0;
float humidity    = 0.0;

void setup() {
  Serial.begin(115200);
  dht.begin();

  // Connect to WiFi
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  // Read sensor data periodically
  if (millis() - lastRead > readInterval) {
    lastRead = millis();
    float t = dht.readTemperature();
    float h = dht.readHumidity();

    if (!isnan(t) && !isnan(h)) {
      temperature = t;
      humidity    = h;
      Serial.printf("Temp: %.1f°C | Humidity: %.1f%%\n", t, h);
    } else {
      Serial.println("Sensor read failed!");
    }
  }

  // Handle web clients
  WiFiClient client = server.available();
  if (client) {
    String request = client.readStringUntil('\r');
    client.flush();

    // Build HTML response
    String html = "<!DOCTYPE html><html><head>";
    html += "<meta name='viewport' content='width=device-width,initial-scale=1'>";
    html += "<meta http-equiv='refresh' content='5'>";
    html += "<title>ESP32 Monitor</title>";
    html += "<style>";
    html += "body{font-family:Arial;text-align:center;background:#1a1a2e;color:#eee;padding:20px;}";
    html += ".card{background:#16213e;border-radius:16px;padding:30px;margin:15px auto;max-width:400px;box-shadow:0 4px 15px rgba(0,0,0,0.3);}";
    html += ".value{font-size:48px;font-weight:bold;color:#0f3460;}";
    html += ".temp .value{color:#e94560;} .hum .value{color:#00b4d8;}";
    html += "h1{color:#e94560;} h2{margin:0;color:#aaa;}";
    html += "</style></head><body>";
    html += "<h1>๐ŸŒก️ ESP32 Monitor</h1>";
    html += "<div class='card temp'><h2>Temperature</h2>";
    html += "<div class='value'>" + String(temperature, 1) + "°C</div></div>";
    html += "<div class='card hum'><h2>Humidity</h2>";
    html += "<div class='value'>" + String(humidity, 1) + "%</div></div>";
    html += "<p style='color:#666;'>Auto-refreshes every 5s</p>";
    html += "</body></html>";

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Connection: close");
    client.println();
    client.print(html);
    delay(10);
    client.stop();
  }
}

⚠️ Before Uploading: Replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD with your actual WiFi credentials. Select "ESP32 Dev Module" as your board in Arduino IDE.

๐Ÿš€ Step-by-Step Assembly Guide

  1. Wire the circuit — Follow the wiring table above. Double-check that VCC goes to 3.3V (not 5V!).
  2. Install Arduino IDE — Download from arduino.cc. Add the ESP32 board URL in Preferences: https://dl.espressif.com/dl/package_esp32_index.json
  3. Install libraries — Search for "DHT sensor library" by Adafruit in Library Manager.
  4. Edit & upload code — Paste the code, enter your WiFi credentials, select the correct COM port, and click Upload.
  5. Open Serial Monitor — Set baud rate to 115200. You should see: WiFi connected! IP Address: 192.168.x.x
  6. Open your browser — Type the IP address shown in Serial Monitor. Your live dashboard appears!

๐Ÿ“ฑ Accessing from Any Device

Once the ESP32 is running, any device on the same WiFi network can view the dashboard:

  • ๐Ÿ“ฑ Phone: Open Chrome/Safari → type the IP address
  • ๐Ÿ’ป Laptop: Any browser → type the IP address
  • ๐Ÿ“บ Tablet: Perfect for wall-mounting as a permanent display
๐ŸŒ Want access from outside your home? You can set up port forwarding on your router or use a free service like Tailscale or ngrok for secure remote access without exposing your network.

๐Ÿ” Troubleshooting Common Issues

ProblemCauseSolution
"Sensor read failed!" in Serial MonitorLoose wiring or wrong pinCheck DATA pin is on GPIO 4; verify 10kฮฉ pull-up resistor
WiFi won't connectWrong SSID/password or 5GHz networkESP32 only supports 2.4GHz WiFi; double-check credentials
Can't access web pageWrong IP or firewall blockingConfirm IP in Serial Monitor; disable firewall temporarily
Readings show NaNDHT22 needs 2s between readsEnsure readInterval is ≥ 2000ms (code uses 5000ms)
ESP32 gets warmNormal operationESP32 runs warm by design; add a small heatsink if concerned
Upload fails with timeoutWrong COM port or missing driverInstall CP210x or CH340 USB driver; hold BOOT button during upload

๐Ÿ”‹ Power Optimization (Run for Weeks on Battery)

If you want to run this project on a LiPo battery, add deep sleep between readings:


// Add at the end of loop():
esp_sleep_enable_timer_wakeup(300 * 1000000); // Sleep 5 minutes
esp_deep_sleep_start();

With deep sleep, the ESP32 consumes only ~10ยตA, meaning a 2000mAh LiPo battery lasts over 3 months!

๐ŸŽฏ Next Steps & Upgrades

Once your basic monitor is running, try these upgrades:

  • ๐Ÿ“Š Add a graph: Use ThingSpeak (free) to log historical data
  • ๐Ÿ”” Add alerts: Send a Telegram/email notification when temperature exceeds a threshold
  • ๐Ÿ“Ÿ Add an OLED display: Show readings locally using a 0.96" SSD1306 screen
  • ๐Ÿ  Integrate with Home Assistant: Use MQTT for full smart home integration
  • ๐ŸŒฑ Greenhouse automation: Add a relay to control a fan or water pump automatically

✅ Conclusion

In less than an hour and under $10, you've built a fully functional WiFi environmental monitor that rivals commercial products costing 10x more. The ESP32's built-in WiFi and the DHT22's precision make this one of the most practical IoT projects you can add to your smart home.

This is just the beginning. Once you master sensor reading and web serving on the ESP32, the possibilities are endless — from air quality monitors to smart irrigation systems.

๐Ÿ”” Built this project?
Share your results and photos in the comments below! Have questions? Drop them and we'll help you troubleshoot.

๐Ÿ“Œ Bookmark this page for easy reference, and check out our other ESP32 projects and IoT tutorials at TechFix Hub!

Bottom Ad [Post Page]