ESP32 Weather Station
A comprehensive weather monitoring system built on ESP32 with multiple sensors and MQTT connectivity for IoT integration.
Features
- Temperature and humidity monitoring (DHT11)
- Atmospheric pressure measurement (BMP180)
- Rain detection sensor (analog)
- Light detection (LDR) with LED indicator
- Real-time 16x2 I2C LCD display
- WiFi connectivity with automatic reconnection
- MQTT data publishing with JSON format
- Automatic MQTT reconnection with random client ID
- Non-blocking timer-based sensor readings
- Error handling and status indication
- Serial debugging output (115200 baud)
Hardware Requirements
- ESP32 development board
- DHT11 temperature/humidity sensor
- BMP180 pressure sensor
- Rain sensor module (analog output)
- LDR (Light Dependent Resistor)
- 16x2 I2C LCD display (0x27 address)
- LED (for light status indication)
- Breadboard and jumper wires
Software Dependencies
- DHT sensor library
- LiquidCrystal I2C
- SFE BMP180 Breakout Library
- PubSubClient
Configuration
Update the following constants in weatherstation.ino:
ssid: Your WiFi network name
password: Your WiFi password
mqtt_server: Your MQTT broker address
mqtt_port: MQTT broker port (default 1883)
mqtt_topic: MQTT topic for data publishing (default: weather/station)
Pin Configuration
- DHT11 Sensor: Pin 4
- LDR Sensor: Pin 35 (input only)
- Rain Sensor: Pin 34 (analog)
- Status LED: Pin 2
- I2C LCD (SDA): Pin 21
- I2C LCD (SCL): Pin 22
MQTT Data Format
The station publishes JSON data to the configured topic:
{
"temperature": 23.45,
"humidity": 67.89,
"pressure": 1013.25,
"rainLevel": 15,
"lightDetected": true
}
LCD Display Format
Line 1: T:23.4C H:67%
Line 2: R:15% P:1013
Serial Output Format
Temp: 23.45°C, Humidity: 67.89%, Rain: 15%, Pressure: 1013.25 hPa, Light: YES
Installation
- Install PlatformIO
- Set up MQTT broker (Mosquitto, HiveMQ, AWS IoT, etc.)
- Connect hardware according to pin configuration
- Update WiFi and MQTT credentials in sketch
- Run
pio run to build
- Run
pio run --target upload to flash
- Run
pio device monitor to view serial output
- Subscribe to MQTT topic to receive weather data
MQTT Broker Setup
- Install Mosquitto broker or use cloud service
- Configure broker for client connections on port 1883
- Create topic structure: weather/station (or custom)
- Test broker connectivity with MQTT client tools
- Configure firewall if using local broker
- Verify broker allows anonymous connections or add authentication
Operation Schedule
- Sensor readings: Every 2 seconds (2000ms)
- MQTT data publishing: Every 30 seconds (30000ms)
- LCD display updates: Every 2 seconds
- LED status updates: With each sensor reading
- WiFi connection attempts: 20 attempts (10 seconds timeout)
- MQTT reconnection: Every 5 seconds if disconnected
Troubleshooting
- Sensor errors: Check wiring connections and power supply
- WiFi connection: Verify SSID and password credentials
- MQTT connection: Ensure broker is accessible and running
- LCD display: Check I2C address (default 0x27) and connections
- DHT11 readings: Look for NaN values in serial output
- BMP180 sensor: Verify initialization and I2C connection
- Rain sensor: Check analog pin assignment (Pin 34)
- LDR/LED: Verify digital pin assignment (Pin 35 for LDR, Pin 2 for LED)
Code Architecture
- Modular design: Separate functions for each sensor
- Non-blocking operations: Timer-based without delays
- Structured data: WeatherData struct for sensor values
- Error handling: Validation and status reporting
- Automatic recovery: WiFi and MQTT reconnection logic
Technical Specifications
- Microcontroller: ESP32 (Dual Core, 240MHz)
- Memory: 520KB SRAM, 4MB Flash
- Wireless: WiFi 802.11 b/g/n
- Sensors: DHT11 (±2°C, ±5%RH), BMP180 (±1hPa), Rain sensor, LDR
- Display: 16x2 Character LCD with I2C interface
- Power: 5V via USB or external supply
- Communication: I2C, WiFi, MQTT
Schematics and Wiring
Quick Reference Diagram
ESP32 Weather Station Wiring
┌─────────────────────────────────────┐
│ ESP32 │
│ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │GPIO2│ │GPIO4│ │GPIO21│ │GPIO22│ │
│ │ LED │ │DHT11│ │ SDA │ │ SCL │ │
│ └─────┘ └─────┘ └─────┘ └─────┘ │
│ ┌─────┐ ┌─────┐ │
│ │GPIO34│ │GPIO35│ │
│ │Rain │ │ LDR │ │
│ └─────┘ └─────┘ │
└─────────────────────────────────────┘
Complete Wiring Guide
- DHT11 Sensor: Pin 4 (Data), 3.3V, GND
- BMP180 Sensor: SDA→GPIO21, SCL→GPIO22, 3.3V, GND
- Rain Sensor: AO→GPIO34, 3.3V, GND
- LDR: GPIO35 with 10kΩ pull-down, 3.3V
- LED: GPIO2 with 220Ω resistor, GND
- LCD Display: SDA→GPIO21, SCL→GPIO22, 5V, GND
Detailed Schematics
For complete circuit diagrams, component layouts, and assembly instructions, see schematics.md.
Bill of Materials
- ESP32 Development Board
- DHT11 Temperature & Humidity Sensor
- BMP180 Pressure Sensor
- Rain Sensor Module (analog)
- LDR (Light Dependent Resistor)
- 16x2 LCD Display with I2C Module
- LED (5mm) + 220Ω resistor
- 10kΩ resistor (for LDR)
- Breadboard and jumper wires