readme.md
Markdown · 74 lines
# ESP32 DHT22 Captive Portal (Temperature & Humidity)
This project turns an ESP32 into an **open Wi-Fi Access Point** with a **captive portal** (no login page) and a simple web UI that displays live **temperature** and **humidity** readings from a **DHT22 (3-pin) sensor**.
When a phone/laptop connects to the AP, it is redirected to the portal automatically (DNS wildcard). The portal shows:
- Temperature (°C)
- Humidity (%)
- Data “age” (ms since last sensor read)
## Features
- Open Wi-Fi AP (no password) for quick access
- Captive portal using DNS wildcard (redirects any domain to ESP32)
- Web dashboard (`/`) + JSON endpoint (`/data`)
- DHT22 polling with safe interval (>= 2 seconds)
- Minimal, lightweight UI with 1s auto-refresh
## Hardware
- ESP32 (any common dev board)
- DHT22 / AM2302 temperature & humidity sensor (3-pin module recommended)
## Wiring (DHT22 3-pin)
| DHT22 Pin | ESP32 |
|----------:|:------|
| VCC | 3V3 |
| DATA | GPIO4 (configurable in code) |
| GND | GND |
### Notes
- Use **3.3V** with ESP32.
- Most **3-pin DHT22 modules** already include the pull-up resistor.
- If you use a **bare 4-pin sensor** (no PCB), add a **4.7k–10k pull-up resistor** between **VCC and DATA**.
## Software Requirements
- Arduino IDE (or PlatformIO)
- ESP32 board support: **“ESP32 by Espressif Systems”**
### Libraries
Install via Arduino Library Manager:
- **DHT sensor library** (Adafruit)
- **Adafruit Unified Sensor**
Built-in (ESP32 Arduino core):
- `WiFi.h`
- `WebServer.h`
- `DNSServer.h`
## How to Use
1. Flash the firmware to your ESP32.
2. Power the ESP32 and connect your phone/PC to the Wi-Fi network:
- **SSID:** `DHT22-Portal`
3. The captive portal should open automatically.
- If it doesn’t, open a browser and go to: `http://192.168.4.1/`
4. Read temperature and humidity live on the web page.
## Configuration
Edit these constants in the sketch:
- `AP_SSID` / `AP_PASS` (leave empty for open AP)
- `DHT_PIN` (default: GPIO4)
- `READ_INTERVAL_MS` (default: 2000 ms; do not go below 2000 for DHT22)
## Endpoints
- `GET /` – web UI
- `GET /data` – JSON status:
- `temp_c` (number or null)
- `hum` (number or null)
- `age_ms` (number)
## Troubleshooting
- **Always shows N/A:** check wiring, correct pin number, and that the sensor is powered from 3.3V.
- **Random/unstable readings:** add a pull-up resistor if using a bare sensor, shorten wires, and keep the read interval >= 2s.
- **Portal does not open automatically:** open `http://192.168.4.1/` manually.
## License
MIT