An ESP32-based weather station that connects to Wi-Fi and an MQTT broker, collects environmental data (temperature, humidity, pressure, rainfall), and publishes the results as JSON.
It supports a BME280 sensor for temperature, humidity, and pressure, and a tipping bucket rain gauge for rainfall measurement. Boot information is stored in EEPROM for persistence across reboots.
See the Complete Documentation for more detail on installation and setup.
- Connects to Wi-Fi with static IP configuration and auto-reconnect.
- Connects to an MQTT broker with auto-reconnect.
- Subscribes to an input MQTT topic and publishes sensor readings to an output MQTT topic.
- Collects data from:
- BME280 (temperature, humidity, pressure).
- Davis tipping bucket rain gauge (rainfall).
- Uses EEPROM to log:
- Number of boots.
- Last reboot reason.
- Includes reboot logic for invalid sensor readings or failed connections.
- ESP32 development board.
- BME280 sensor (I²C).
- Davis tipping bucket rain gauge or compatible (connected to pin
GPIO16).
| ESP32 Pin | BME280 Pin |
|---|---|
| 3.3V | VCC |
| GND | GND |
| GPIO21 | SDA |
| GPIO22 | SCL |
| ESP32 Pin | Rain Gauge |
|---|---|
| GPIO16 | Signal |
| 3.3V | VCC |
| GND | GND |
Note: Rain gauge uses an interrupt on GPIO16 to count bucket tips.
- Arduino IDE or PlatformIO.
- ESP32 board support package.
- Arduino libraries:
Update these variables in the code before flashing:
// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// MQTT broker
const char* mqtt_server = "BROKER_IP_ADDRESS";
// Optional static IP config
IPAddress local_IP(192, 168, 1, 40);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);MQTT authentication (username/password) can also be updated in the connect_to_mqtt() function.
- Subscribe
esp32/in→ triggers the device to take a reading and publish data.
- Publish
esp32/out→ JSON payload containing temperature, pressure, humidity, and rainfall.esp32/out/debug→ Debug messages (boot count and reboot reasons).esp32/out/keepalive→ Periodic keep-alive signal.
{
"temperature": 20.0,
"pressure": 1000.0,
"humidity": 50.0,
"mm_rain": 1.2
}The EEPROM stores a small struct with:
num_boots: number of boots since first flash.reason: the reason for the last reboot (e.g., Wi-Fi failure, MQTT failure, invalid sensor values).
- The
setup_bme280()andget_bme_values()functions are stubbed; uncomment and configure wiring to enable live sensor data. - Improve error handling for MQTT and Wi-Fi reconnection.
- Add OTA updates for field deployment.
- Dane Lennon
Initial version: 2022-11-22