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:
| Component | Specification | Qty | Est. Price |
|---|---|---|---|
| ESP32 DevKit V1 | 30-pin, WiFi + BLE | 1 | $4.00 |
| DHT22 (AM2302) | Temp + Humidity sensor | 1 | $3.00 |
| Resistor | 10kฮฉ ¼W (pull-up) | 1 | $0.05 |
| Breadboard | 400 tie-points | 1 | $2.00 |
| Jumper Wires | Male-to-Female | 4 | $0.50 |
| Micro USB Cable | For programming & power | 1 | $1.00 |
๐ Circuit Wiring Diagram
The wiring is minimal — only 4 connections between the ESP32 and the DHT22:
| DHT22 Pin | ESP32 Pin | Notes |
|---|---|---|
| Pin 1 (VCC) | 3.3V | Do NOT use 5V — ESP32 is 3.3V logic |
| Pin 2 (DATA) | GPIO 4 | Connect 10kฮฉ pull-up resistor between DATA and VCC |
| Pin 3 (NC) | — | Not connected (leave empty) |
| Pin 4 (GND) | GND | Common ground |
๐ป 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();
}
}
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
- Wire the circuit — Follow the wiring table above. Double-check that VCC goes to 3.3V (not 5V!).
- Install Arduino IDE — Download from arduino.cc. Add the ESP32 board URL in Preferences:
https://dl.espressif.com/dl/package_esp32_index.json - Install libraries — Search for "DHT sensor library" by Adafruit in Library Manager.
- Edit & upload code — Paste the code, enter your WiFi credentials, select the correct COM port, and click Upload.
- Open Serial Monitor — Set baud rate to 115200. You should see:
WiFi connected! IP Address: 192.168.x.x - 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
| Problem | Cause | Solution |
|---|---|---|
| "Sensor read failed!" in Serial Monitor | Loose wiring or wrong pin | Check DATA pin is on GPIO 4; verify 10kฮฉ pull-up resistor |
| WiFi won't connect | Wrong SSID/password or 5GHz network | ESP32 only supports 2.4GHz WiFi; double-check credentials |
| Can't access web page | Wrong IP or firewall blocking | Confirm IP in Serial Monitor; disable firewall temporarily |
| Readings show NaN | DHT22 needs 2s between reads | Ensure readInterval is ≥ 2000ms (code uses 5000ms) |
| ESP32 gets warm | Normal operation | ESP32 runs warm by design; add a small heatsink if concerned |
| Upload fails with timeout | Wrong COM port or missing driver | Install 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.
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!