ESP32 Web Server | Display DHT22 Temperature & Humidity in Your Browser
- Get link
- X
- Other Apps
ESP32 Web Server: Display DHT22 Temperature & Humidity in Your Browser
Meta description: Turn your ESP32 into a local web server that displays live DHT22 temperature and humidity readings in any browser on your network — full code included.
Introduction
If you've already wired up a DHT22 sensor to your ESP32 (see our ESP32 DHT22 tutorial), the next logical step is getting rid of the Serial Monitor and viewing your readings on an actual web page — from your phone, laptop, or any device on your network.
In this tutorial, you'll turn your ESP32 into a self-hosted web server that displays live temperature and humidity data from the DHT22 sensor directly in your browser.
What This Project Does
The ESP32 connects to your Wi-Fi network, hosts a simple web page, and refreshes temperature and humidity readings from the DHT22 sensor automatically.
No internet connection or cloud service is required. Everything runs locally on your Wi-Fi network, making this a simple and useful foundation for ESP32 IoT projects.
Required Components
- ESP32 development board
- DHT22 temperature and humidity sensor (wired as in the previous tutorial)
- 10kΩ resistor (if not using a breakout module)
- Breadboard and jumper wires
- Wi-Fi network (2.4GHz — ESP32 doesn't support 5GHz)
Features
- Live temperature and humidity readings viewable from any browser on your network
- Automatically refreshes the readings without requiring manual page reloads
- No cloud service or dedicated app required — fully local
- Provides a foundation for future upgrades such as historical charts and remote access
How It Works
The ESP32 runs a lightweight web server using its built-in Wi-Fi capability. When a browser requests the page, the ESP32 reads fresh data from the DHT22 and serves it as an HTML page.
The web page uses a small auto-refresh feature to reload the sensor readings every few seconds. This means you can see updated temperature and humidity values without manually refreshing the browser.
Wiring / Circuit Information
The wiring is the same as the previous ESP32 DHT22 tutorial. Connect the sensor to the ESP32 as follows:
| DHT22 Pin | ESP32 Pin |
|---|---|
| VCC (+) | 3.3V |
| DATA (out) | GPIO 4 |
| GND (-) | GND |
If you're using a bare DHT22 sensor rather than a breakout module, use a 10kΩ pull-up resistor between the VCC and DATA pins.
The ESP32 connects to 2.4GHz Wi-Fi networks. Make sure your ESP32 and the device you're using to view the dashboard are connected to the same local network.
Step-by-Step Instructions
- Confirm your DHT22 wiring is already working. If you haven't tested the sensor yet, use the previous DHT22 tutorial's code first.
- Install the required libraries if you haven't already. Install the "DHT sensor library" by Adafruit and "Adafruit Unified Sensor" using the Arduino IDE Library Manager.
-
Update the Wi-Fi credentials
in the code below. Replace
YOUR_WIFI_NAMEwith your Wi-Fi network name andYOUR_WIFI_PASSWORDwith your Wi-Fi password. - Upload the code to your ESP32.
- Open the Serial Monitor at 115200 baud. Once the ESP32 connects to Wi-Fi, it will print its local IP address.
- Open a browser on any device connected to the same Wi-Fi network and enter the IP address shown in the Serial Monitor.
Complete Code
#include <WiFi.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
DHT dht(DHTPIN, DHTTYPE);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (!client) return;
String request = client.readStringUntil('\r');
client.flush();
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
String html = "<!DOCTYPE html><html><head>";
html += "<meta http-equiv='refresh' content='5'>";
html += "<title>Micro Makers - ESP32 Sensor</title>";
html += "<style>";
html += "body{font-family:Arial;text-align:center;margin-top:50px;}";
html += "h1{color:#333;}";
html += ".reading{font-size:28px;margin:10px;}";
html += "</style>";
html += "</head><body>";
html += "<h1>ESP32 Sensor Dashboard</h1>";
if (isnan(humidity) || isnan(tempC)) {
html += "<p class='reading'>Sensor read error</p>";
} else {
html += "<p class='reading'>Temperature: ";
html += String(tempC);
html += " °C</p>";
html += "<p class='reading'>Humidity: ";
html += String(humidity);
html += " %</p>";
}
html += "</body></html>";
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println(html);
client.stop();
}
Explanation of Important Code Sections
-
WiFi.begin(ssid, password)— connects the ESP32 to your local Wi-Fi network using the network credentials defined in the code. -
WiFiServer server(80)— starts a web server listening on port 80, the standard HTTP port. This allows you to access the ESP32 using its IP address without specifying a port number. -
meta http-equiv='refresh' content='5'— tells the browser to reload the page every 5 seconds, providing automatically updated sensor readings without requiring JavaScript. -
client.readStringUntil('\r')— reads the browser's incoming request so the server knows a client has connected. This basic version doesn't need to process the actual request content and always serves the same dashboard page. -
html += "..."— builds the HTML web page as a string and inserts the current temperature and humidity values into the page.
Testing
After uploading the code, open the Serial Monitor and make sure the baud rate is set to 115200.
Once the ESP32 successfully connects to Wi-Fi, you should
see an IP address similar to
192.168.1.XX.
Enter this IP address into a browser on a device connected to the same Wi-Fi network. You should see the ESP32 Sensor Dashboard displaying the current temperature and humidity.
The page automatically refreshes every 5 seconds, so the displayed readings will update without requiring you to manually reload the page.
Troubleshooting
Page won't load / "This site can't be reached":
- Confirm your device and ESP32 are connected to the same Wi-Fi network.
- Double-check the IP address printed in the Serial Monitor. The address can change if your router reassigns it.
- Make sure you're using a 2.4GHz Wi-Fi network. The ESP32 doesn't support 5GHz networks.
ESP32 won't connect to Wi-Fi:
- Double-check the SSID and password for typos. Wi-Fi credentials are case-sensitive.
- Some routers block new device connections until the device is approved in the router settings. Check your router's connected-device list if necessary.
Page loads but shows "Sensor read error":
- Check the DHT22 wiring, especially the DATA connection to GPIO 4.
- Confirm that the required pull-up resistor is installed if you're using a bare DHT22 sensor.
- Make sure the DHT22 is already working correctly with the standalone sensor test from the previous tutorial.
Common Problems and Solutions
| Problem | Likely Cause | Solution |
|---|---|---|
| Can't reach IP address | Wrong Wi-Fi band or network | Use 2.4GHz Wi-Fi and confirm the viewing device is connected to the same network. |
| IP address keeps changing | Router using dynamic DHCP | Set a static IP in your router settings for the ESP32. |
| Page loads slowly | Sensor read blocking each request | This is normal for this basic version. Advanced versions can cache readings separately. |
Improvements / Upgrades
Once this basic ESP32 web server is working, you can extend it in several useful ways:
- Add a static IP so the ESP32's address doesn't change between reboots.
- Improve the CSS and create a cleaner, more modern dashboard interface.
- Log historical temperature and humidity readings and display them as a chart.
- Add JSON API output so the sensor data can be consumed by other applications and scripts instead of only being displayed in a browser.
Frequently Asked Questions
Not with this basic setup. The dashboard is designed to be accessible only from your local Wi-Fi network. Remote access would require techniques such as port forwarding or a cloud-based IoT platform. Port forwarding is not recommended without properly securing the device.
This basic version uses an HTML meta-refresh tag because it keeps the project simple and beginner-friendly. A smoother, flicker-free dashboard could use JavaScript and AJAX requests to update only the sensor values.
Yes. You can combine this web server with other ESP32 functionality as long as you don't use conflicting GPIO pins or exceed the ESP32's processing capacity for simple projects like this.
Conclusion
You now have a fully local, browser-based dashboard showing live sensor data from your ESP32 and DHT22 — without requiring a cloud service or dedicated application.
This project is a solid foundation for expanding into smart home dashboards, environmental monitoring systems, and other ESP32-based IoT projects.
From here, you can take the project further by adding a better dashboard design, historical charts, a JSON API, static IP configuration, or eventually a secure remote-access solution.
- Get link
- X
- Other Apps
Comments
Post a Comment