This project provides a professional-grade, modular, and extensible firmware structure for building a feature-rich weather station using an ESP32 microcontroller. It is designed for high reliability, easy configuration, and future expansion.
- Fully Modular: Every sensor is conditionally compiled. Enable or disable features by changing a single line in
config.h
. - Robust & Safe: The code includes health checks for all sensors on startup and uses safe memory management practices.
- Advanced Calculations: Includes time-based calculations for rainfall (60-minute sliding window, daily, weekly, monthly).
- Remote Management:
- Over-the-Air (OTA) Updates: Update the firmware remotely over Wi-Fi.
- MQTT Health Status: Publishes the boot-up health status of each sensor.
- MQTT Remote Debugging: Toggle a debug mode via MQTT to receive detailed sensor data for troubleshooting.
- NTP Time Sync: Automatically synchronizes time to ensure accurate calculations.
- Centralized Configuration: A single, clean
config.h
file for all your settings.
Once the device is on your Wi-Fi network, you can upload new firmware without a physical connection. In PlatformIO, this is typically done by setting an upload_port
with the device's IP address or hostname in platformio.ini
and using the "Upload" command. The default hostname is configured in config.h
.
The station uses a base topic (weatherstation/
) which can be configured.
- Health Status: On boot, the status of each enabled sensor is published to
weatherstation/health/<Sensor_Name>
(e.g.,weatherstation/health/Wind_Direction
). The payload will beOK
orFAIL
. - Remote Debugging:
- Publish a message (
on
,true
, or1
) to the topicweatherstation/control/debug
to enable debug mode. - Publish any other message (
off
,false
,0
) to the same topic to disable it. - When enabled, each sensor will publish detailed raw data or status messages to its own debug topic (e.g.,
weatherstation/livingroom/winddir/debug
).
- Publish a message (
This project contains drivers for a wide array of environmental sensors.
Measurement | Sensor | Interface | Default Status |
---|---|---|---|
Ambient Light | LDR | Analog | Enabled |
Gas (Propane, Methane etc.) | MQ-2 | Analog | Enabled |
UV Index | GY-8511 | Analog | Enabled |
Wind Direction | WH-SP-WD | Analog | Enabled |
Rainfall | MS-WH-SP-RG | Pulse | Enabled |
Wind Speed | WH-SP-WS01 | Pulse | Enabled |
Light (Lux) | GY-302 (BH1750) | I2C | Enabled |
Lightning Detection | AS3935 | I2C | Enabled |
CO₂ | MH-Z19E | UART | Enabled |
Air Quality (Gases) | SGP30 | I2C | Disabled |
Air Quality (Particulates) | Plantower PMS5003 | UART | Disabled |
Soil Moisture & Temp | Capacitive + DS18B20 | Analog/1-Wire | Disabled |
All project configuration is done in src/config.h
.
- Enable/Disable Sensors: Find the
--- Enable/Disable Sensors ---
section and comment or uncomment the#define
for each sensor you want to use. - Set Network Credentials: Configure your
WIFI_SSID
,WIFI_PASSWORD
, andMQTT_BROKER_IP
. - Set Time Zone: Configure
UTC_OFFSET_SECONDS
andDAYLIGHT_OFFSET_SECONDS
. - Calibrate & Configure Sensors: Review all pin assignments, I2C addresses, and especially the
RAIN_MM_PER_PULSE
andWIND_KMH_PER_PULSE_PER_SEC
constants to match your hardware.
With this rich sensor data, you can implement many more calculations in the src/calculations
directory.
Calculation | Requires Sensors | Description |
---|---|---|
Dew Point | Temperature, Humidity | A better measure of how "humid" it feels. |
Wind Chill | Temperature, Wind Speed | How cold it feels when wind is a factor. |
AQI (Air Quality Index) | Particulate (PMS) or Gas (SGP30) | A standardized index for reporting air pollution. |
Cloud Base (Est.) | Temperature, Dew Point | An estimation of the altitude of the cloud base. |