Complete DIY STEM and IoT project guide
Project overview
An ESP32 Wi-Fi Weather Station is an affordable Internet of Things (IoT) project that measures temperature and relative humidity and displays the readings on a live web dashboard. It uses an ESP32 development board and either a DHT11 or DHT22 sensor.
The dashboard can be opened using a smartphone, tablet, or computer connected to the same Wi-Fi network.
1. Project objectives
- Measure temperature and humidity using a digital sensor.
- Connect the ESP32 to a Wi-Fi network.
- Create a live web dashboard.
- Display environmental readings on a smartphone or computer.
- Explore IoT applications in agriculture, schools, homes, and environmental monitoring.
2. Components and materials required
| Component | Quantity | Purpose |
|---|---|---|
| ESP32 DevKit | 1 | Main controller and Wi-Fi communication |
| DHT22 sensor | 1 | Temperature and humidity measurement |
| DHT11 sensor | 1 | Alternative lower-cost sensor |
| Breadboard | 1 | Circuit assembly |
| Jumper wires | Several | Electrical connections |
| 10 kΩ resistor | 1 | Pull-up resistor for bare sensor |
| USB cable | 1 | Programming and power |
| Wi-Fi router or hotspot | 1 | Wireless network |
| Smartphone or computer | 1 | Dashboard access |
Only one sensor is needed: either DHT11 or DHT22.
DHT11 versus DHT22
| Feature | DHT11 | DHT22 |
|---|---|---|
| Temperature range | 0–50°C | -40–80°C |
| Temperature accuracy | Approximately ±2°C | Approximately ±0.5°C |
| Humidity range | 20–80% RH | 0–100% RH |
| Humidity accuracy | Approximately ±5% RH | Approximately ±2–5% RH |
| Cost | Lower | Higher |
The DHT22 is recommended where better measurement accuracy and a wider operating range are required.
3. Completed ESP32 Wi-Fi weather station
The completed prototype consists of an ESP32 board connected to a DHT sensor. The ESP32 reads the environmental data and makes it available through a web server.
4. ESP32 and DHT22/DHT11 circuit connections
Connect the sensor as follows:
| DHT sensor pin | ESP32 connection |
|---|---|
| VCC | 3V3 |
| DATA | GPIO 4 |
| GND | GND |
For a bare four-pin DHT sensor, connect a 10 kΩ pull-up resistor between DATA and VCC. Many three-pin sensor modules already include this resistor.
Important: ESP32 GPIO pins use 3.3 V logic. Do not apply 5 V to the GPIO pins.
5. System block diagram
The system operates through the following stages:
- The DHT sensor measures temperature and humidity.
- The sensor sends digital data to the ESP32.
- The ESP32 processes the measurements.
- The ESP32 hosts a local web server.
- A smartphone or computer accesses the ESP32 IP address.
- The dashboard displays updated readings.
6. Installing the required software
Use the Arduino IDE to program the ESP32.
Download Arduino IDE: https://www.arduino.cc/en/software
Install ESP32 board support:
- Open Arduino IDE.
- Navigate to File → Preferences.
- Add this URL under Additional Boards Manager URLs:
https://espressif.github.io/arduino-esp32/package_esp32_index.json
- Open Tools → Board → Boards Manager.
- Search for ESP32 and install the package by Espressif Systems.
- Select the appropriate ESP32 board.
Install these libraries using Library Manager:
- DHT sensor library by Adafruit
- Adafruit Unified Sensor
7. ESP32 Wi-Fi weather station Arduino code
The code below reads the sensor and displays the measurements on a live web dashboard.
Replace the Wi-Fi name and password with your own credentials.
#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
#define DHTPIN 4
#define DHTTYPE DHT22
// Change DHT22 to DHT11 if using DHT11
DHT dht(DHTPIN, DHTTYPE);
WebServer server(80);
float temperature = 0;
float humidity = 0;
unsigned long previousMillis = 0;
const unsigned long interval = 2500;
const char webpage[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Weather Station</title>
<meta name="viewport"
content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial;
background: #eef3f8;
text-align: center;
padding: 20px;
}
.container {
max-width: 650px;
margin: auto;
}
.card {
background: white;
padding: 25px;
margin: 18px 0;
border-radius: 14px;
box-shadow: 0 3px 12px #0002;
}
.value {
font-size: 42px;
font-weight: bold;
color: #0879bd;
}
.label {
color: #555;
font-size: 18px;
}
.status {
color: green;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>ESP32 Wi-Fi Weather Station</h1>
<p>Live Environmental Monitoring</p>
<div class="card">
<div class="label">Temperature</div>
<div class="value" id="temperature">-- °C</div>
</div>
<div class="card">
<div class="label">Relative Humidity</div>
<div class="value" id="humidity">-- %</div>
</div>
<p class="status" id="status">Connecting...</p>
<p id="updated">Waiting for readings</p>
</div>
<script>
async function updateData() {
try {
const response = await fetch('/data');
if (!response.ok) throw new Error('Network error');
const data = await response.json();
document.getElementById('temperature').innerText =
data.temperature + ' °C';
document.getElementById('humidity').innerText =
data.humidity + ' %';
document.getElementById('status').innerText =
'Sensor connected';
document.getElementById('updated').innerText =
'Last updated: ' + new Date().toLocaleTimeString();
} catch (error) {
document.getElementById('status').innerText =
'Unable to retrieve readings';
}
}
updateData();
setInterval(updateData, 3000);
</script>
</body>
</html>
)rawliteral";
void handleRoot() {
server.send_P(200, "text/html", webpage);
}
void handleData() {
String json = "{";
json += "\"temperature\":" + String(temperature, 1) + ",";
json += "\"humidity\":" + String(humidity, 1);
json += "}";
server.send(200, "application/json", json);
}
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Wi-Fi connected!");
Serial.print("Dashboard address: http://");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/data", handleData);
server.begin();
}
void loop() {
server.handleClient();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float newHumidity = dht.readHumidity();
float newTemperature = dht.readTemperature();
if (!isnan(newHumidity) && !isnan(newTemperature)) {
humidity = newHumidity;
temperature = newTemperature;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
Serial.println("DHT sensor reading failed!");
}
}
}
For DHT11, change:
#define DHTTYPE DHT22
to:
#define DHTTYPE DHT11
8. Live web dashboard
The dashboard displays temperature, relative humidity, sensor status, and the time of the latest browser update.
The JavaScript function retrieves new measurements from the ESP32 /data endpoint every three seconds. The ESP32 reads the sensor approximately every 2.5 seconds.
9. Uploading and accessing the dashboard
- Connect the ESP32 to your computer using a USB cable.
- Open the Arduino IDE and paste the code.
- Enter your Wi-Fi credentials.
- Select the correct ESP32 board and COM port.
- Click Verify, then Upload.
- Open the Serial Monitor at 115200 baud.
- Wait for the ESP32 to connect to Wi-Fi.
- Copy the IP address displayed.
- Open that address in a browser on a phone or computer connected to the same Wi-Fi network.
For example, the address may look like:
http://192.168.1.105
The actual address depends on the network.
Network limitation: This version provides a local dashboard. Remote access outside the local network requires additional secure networking or cloud integration.
10. Testing and evaluation
| Test | Procedure | Expected result |
|---|---|---|
| Sensor test | Power the ESP32 | Temperature and humidity appear |
| Wi-Fi test | Connect to the network | ESP32 obtains an IP address |
| Dashboard test | Open IP address | Dashboard loads |
| Temperature test | Compare with reference thermometer | Readings are reasonably close |
| Humidity test | Compare with reference hygrometer | Readings are reasonably close |
| Refresh test | Observe dashboard | Values update automatically |
Record actual measurements during testing rather than assuming a particular accuracy.
11. Construction and installation
For outdoor use, place the electronics in a weather-resistant enclosure and mount the DHT sensor in a ventilated radiation shield. Keep the sensor away from direct sunlight, rain, and heat sources to obtain more representative ambient measurements.
12. Applications
- Smart agriculture: Monitor greenhouse temperature and humidity.
- Poultry farming: Observe environmental conditions inside poultry houses.
- Schools: Demonstrate IoT, sensors, and wireless communication.
- Smart homes: Monitor indoor environmental conditions.
- Storage facilities: Monitor conditions in rooms storing sensitive materials.
13. Possible project improvements
| Upgrade | Component | Function |
|---|---|---|
| Atmospheric pressure | BMP280/BME280 | Measures air pressure |
| Light monitoring | BH1750 | Measures light intensity |
| Rain detection | Rain sensor | Detects rainfall |
| Wind speed | Anemometer | Measures wind speed |
| Cloud storage | ThingSpeak | Stores readings online |
| Historical graphs | Chart.js | Displays measurement trends |
| Local display | OLED screen | Displays readings on the device |
14. Troubleshooting
How to Fix Common ESP32 Upload and Connection Errors
| Problem | Possible solution |
|---|---|
| ESP32 not detected | Use a data USB cable and check the USB driver |
| Upload failure | Select the correct board and hold BOOT if required |
| Wi-Fi not connecting | Check credentials and use a compatible 2.4 GHz network |
| DHT returns NaN | Check wiring, sensor type, and pull-up resistor |
| Dashboard not opening | Confirm the IP address and same Wi-Fi network |
15. STEM and CBE learning outcomes
Learners develop practical skills in:
- Scientific investigation through environmental measurement.
- Electronics and sensor connections.
- Programming using Arduino IDE.
- Wireless communication and IoT.
- Data collection, analysis, and interpretation.
- Engineering design and problem-solving.
- Development of solutions to agricultural and environmental challenges.
16. Conclusion
The ESP32 Wi-Fi Weather Station is an affordable and practical IoT project that demonstrates how sensors, microcontrollers, wireless communication, and web technologies can be integrated into an environmental monitoring system.
Using an ESP32 and a DHT11 or DHT22 sensor, learners can measure temperature and humidity and display the results on a live dashboard. The system can later be expanded into a smart agriculture or remote environmental monitoring platform.







