Build Your Own IoT Weather Station with ESP32 and DHT22
Welcome to another exciting project from Yarana IoT Guru! 🌡️ In this tutorial, we’ll guide you through building a DIY IoT weather station using an ESP32 microcontroller and a DHT22 sensor. This project collects temperature and humidity data, stores it in a MySQL database via a REST API, and displays it on a stunning web dashboard. Perfect for beginners and IoT enthusiasts alike, this hands-on project will level up your skills in embedded systems and full-stack development. Let’s dive in!

Why Build This Project? 🚀
From monitoring your home’s climate to automating a greenhouse, this IoT weather station is both practical and fun. Here’s why you’ll love it:
- Real-World Impact: Use it for smart homes, agriculture, or server room monitoring.
- Beginner-Friendly: Step-by-step guide with a Hindi YouTube tutorial.
- Full-Stack Learning: Master hardware, APIs, databases, and web development.
- Free & Open-Source: Customize and share your project with the community!
What You’ll Need 🛠️
Gather these components to get started:
- Hardware:
- ESP32 board (e.g., NodeMCU ESP32)
- DHT22 sensor (or DHT11 for budget)
- USB cable
- Jumper wires, optional 4.7k–10k pull-up resistor
- Software:
- Arduino IDE (2.x+)
- XAMPP or WAMP
- Web browser or Postman
- Internet: Stable Wi-Fi
How It Works 🔍
The project connects three key components:
- Sensor: DHT22 captures temperature and humidity.
- Database: ESP32 sends data to MySQL via a PHP REST API (HTTP POST).
- Dashboard: Web interface displays data (HTTP GET).
Explore the live dashboard: DHT22 Dashboard. Watch the tutorial for a full demo:
Setup Guide 🚀
Follow these steps to build your weather station:
1. Set Up MySQL Database
- Install XAMPP/WAMP, start Apache/MySQL.
- In phpMyAdmin (
http://localhost/phpmyadmin
), create databaseiot_data
. - Create table:
CREATE TABLE sensor_data ( id INT AUTO_INCREMENT PRIMARY KEY, temperature FLOAT NOT NULL, humidity FLOAT NOT NULL, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP );
- Update
api/config.php
:
2. Deploy API and Dashboard
- Copy
api/
anddashboard/
to XAMPP/WAMPhtdocs
(e.g.,C:\xampp\htdocs\tutorial
). - Test API (
http://localhost/tutorial/DHT/insert_data.php
) and dashboard (http://localhost/tutorial/DHT/dht_sensor.php
). - For cloud, upload to server’s web root (e.g.,
/var/www/html/
) and update ESP32 code.
3. Program ESP32 with DHT22
- Install ESP32 board in Arduino IDE:
- Add to Preferences > Additional Boards Manager URLs:
https://raw.githubusercontent.com/espressif/arduino-esp32/master/package_esp32_index.json
- Install
esp32
in Boards Manager.
- Add to Preferences > Additional Boards Manager URLs:
- Install
DHT.h
library (Adafruit). - Use this code:
#include #include #include "DHT.h" #define DHTPIN 4 // DHT sensor pin #define DHTTYPE DHT22 // or DHT11 if using DHT11 const char* ssid = "your_wifi_ssid"; const char* password = "your_wifi_password"; const char* serverName = "https://yaranaiotguru.in/tutorial/DHT/insert_data.php"; DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); dht.begin(); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { float temp = dht.readTemperature(); float hum = dht.readHumidity(); if (isnan(temp) || isnan(hum)) { Serial.println("Failed to read from DHT sensor!"); return; } if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(serverName); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String postData = "temperature=" + String(temp) + "&humidity=" + String(hum); int httpResponseCode = http.POST(postData); Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); Serial.println("Server response: " + http.getString()); http.end(); } else { Serial.println("WiFi disconnected"); } delay(5000); }
- Update
ssid
,password
, andserverName
. - Connect DHT22:
- VCC → ESP32 3.3V
- GND → ESP32 GND
- DATA → ESP32 GPIO 4
- Optional: 4.7k–10k resistor (VCC to DATA)
- Select ESP32 Dev Module, upload code.
4. Test the System
- Check Serial Monitor (115200 baud) for:
Connected to WiFi
HTTP Response code: 200
Server response: {"status": "success"}
- Verify data in phpMyAdmin (
sensor_data
table). - View dashboard: Live Dashboard.
Circuit Diagram 📏
ESP32 DHT22
3.3V ---- VCC
GND ---- GND
GPIO4 ---- DATA
---- 4.7kΩ resistor (VCC to DATA)
API Endpoints 🔗
Hosted at https://yaranaiotguru.in/tutorial/DHT/.
Method | Endpoint | Description |
---|---|---|
POST | /tutorial/DHT/insert_data.php |
Inserts temperature/humidity data |
GET | /tutorial/DHT/dht_sensor.php |
Displays data on dashboard |
Example POST:
POST https://yaranaiotguru.in/tutorial/DHT/insert_data.php
Content-Type: application/x-www-form-urlencoded
Body: temperature=25.5&humidity=60.2
Response: {"status": "success", "message": "Data inserted successfully"}
Troubleshooting 🛠️
- Wi-Fi Failure: Check SSID/password; ensure ESP32 is in range.
- DHT22 Issues: Verify wiring; add resistor; check
DHTTYPE
. - HTTP Errors: Confirm API URL; check server status.
- Database Errors: Validate
config.php
; ensure MySQL is running. - Dashboard Issues: Check
dht_sensor.php
database connection.
Stuck? Watch the tutorial or comment on YouTube!
Level Up Your Project 🚀
Try these enhancements:
- Build a mobile app for remote monitoring.
- Add alerts for temperature thresholds.
- Include sensors like light or CO2.
- Host on a cloud server for global access.
Join the IoT Revolution 🌍
Enjoyed this project? Subscribe to Yarana IoT Guru for more IoT tutorials. Visit yaranaiotguru.in for resources and updates.
Show off your work! Share your dashboard or ideas in the comments or tag #YaranaIoTGuru on social media.
Keep tinkering, and see you in the next IoT project! 🚀