An ESP32 microcontroller based UPS battery monitor that bypasses the UPS's internal logic to provide raw, highly accurate telemetry (Voltage, Current, Wattage, and State of Charge) via an external shunt. Uses coulomb counting and a multi-variable state machine to trigger automated server shutdowns via MQTT.
For a homelabs setup, it can help you gracefully shutdown servers for legacy UPS or invertors. helps save bucks from buying a smart UPS
- ESP32-S3 DevKitC board
- INA226 power sensor with 100A/75mV shunt
- PlatformIO (recommended) or Arduino IDE
- MQTT broker (e.g., Mosquitto)
# Clone repository
git clone <repository-url>
cd dipstick
# Install dependencies
pio pkg install
# Build for ESP32-S3
pio run -e esp32-s3-devkitc-1
# Run native tests
pio test -e nativeNote: Arduino IDE requires headers to be in the sketch root or src/ folder.
- Copy
src/dipstick.inoto your Arduino sketch folder - Copy all files from
src/include/directly into the sketch folder (flatten structure) - Rename
wifi_manager.hif you encounter naming conflicts - Install required libraries:
- INA226 (by robtillaart)
- ArduinoJson (by bblanchon)
- PubSubClient (by knolleary)
- Select board: ESP32-S3 DevKitC
- Upload
- Real-time Telemetry: Voltage, current, power, and State of Charge (SOC)
- Coulomb Counting: Accurate battery capacity tracking with deadband filtering
- State Machine: Multi-variable debounce for stable state transitions
- MQTT Integration: Publishes comprehensive system status to configured topic
- 3-Sample Debounce: Prevents false triggers from transient conditions
- Thermal Override: Forces critical state when temperature exceeds 75°C
- Thermal Throttling: Reduces publish frequency at high temperatures
- I2C Noise Protection: Timeout and validation to prevent hangs
- Watchdog Timer: Prevents system lockup
- Coulomb counting with proper energy calculation
- SOC capping at 100% during charging
- Deadband filter (< 0.02A treated as 0A)
- Exponential backoff for MQTT reconnection (1s → 2s → 4s → max 60s)
- Thermal override bypasses all debounce logic
dipstick/
├── src/
│ ├── include/
│ │ ├── hal/ # Hardware Abstraction Layer
│ │ │ ├── board.h # Pin definitions and board config
│ │ │ ├── ina226.h # INA226 sensor driver
│ │ │ ├── isensor.h # Sensor interface (abstract)
│ │ │ └── temperature.h # Temperature sensor interface
│ │ ├── app/ # Application Logic
│ │ │ └── monitor.h # State machine, coulomb counting, DTO
│ │ └── net/ # Networking
│ │ ├── mqtt_manager.h # MQTT client with backoff
│ │ └── wifi_manager.h # WiFi connection manager
│ └── dipstick.ino # Main orchestration
├── test/
│ └── test_monitor/ # Unit tests
├── docs/
│ └── examples.md # Configuration and usage examples
├── platformio.ini
└── README.md
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ INA226 │───►│ Monitor │───►│ MQTT │
│ (Sensor) │ │ (State │ │ (Publish) │
└─────────────┘ │ Machine) │ └─────────────┘
└─────────────┘
▲
┌─────┴─────┐
│ Temperature│
└─────────────┘
| Parameter | Value |
|---|---|
| Shunt | 100A / 75mV External Shunt (Low-Side) |
| Max Capacity | 108.0 Wh (12V 9Ah SLA Battery) |
| I2C Pins | GPIO8 (SDA), GPIO9 (SCL) |
| Sensor Calibration | 3413 (based on 0.00075Ω shunt) |
| Current LSB | 0.002A per bit |
ESP32-S3 INA226 Battery
──────── ────── ───────
GPIO8 ────────────────► SDA │
GPIO9 ────────────────► SCL │
GND ────────────────► GND ─┼─► Load
3.3V ────────────────► VIN ─┼─►
Shunt
┌─────┐
│ │
└─────┘
│ │
+V -V
# Build for ESP32-S3
pio run -e esp32-s3-devkitc-1
# Build and upload
pio run -e esp32-s3-devkitc-1 -t upload
# Run all tests
pio test -e native- Open
dipstick.inoin Arduino IDE - Select Board: "ESP32-S3 DevKitC"
- Configure MQTT broker in
wifi_manager.handmqtt_manager.h - Upload
test/test_monitor/
├── test_main.cpp # Basic functionality tests
├── test_coulomb.t.cpp # Coulomb counting verification
├── test_soc.t.cpp # SOC calculation tests
├── test_thermal.t.cpp # Thermal override tests
├── test_debounce.t.cpp # State debounce tests
├── test_state_machine.t.cpp# State transition tests
└── test_utils.h # Mock objects for testing
# Run all native tests
pio test -e native
# Run specific test file
pio test -e native -f test_coulomb
# Run with verbose output
pio test -e native -vvv| Test Suite | Tests | Coverage |
|---|---|---|
| Basic Functionality | 5 | Initial state, critical state, deadband, SOC, debounce |
| Coulomb Counting | 3 | Discharge, charge, deadband filter |
| SOC Logic | 2 | Decrease on discharge, cap at 100% |
| Thermal Override | 2 | Critical temp, bypass debounce |
| Debounce | 3 | Warning, critical, reset |
| State Machine | 2 | Charging detection, on_battery |
| Total | 17 | All critical paths |
MockSensor: Injects controlled voltage, current, power valuesMockTemperature: Simulates temperature readings for thermal testsArduinoFake: Mocks Arduino runtime functions for native testing
dipstick/state
{
"system": {
"uptime_seconds": 86400,
"uptime_human": "1d 00h 00m 00s",
"internal_temp_c": 48.5,
"free_heap_bytes": 284520
},
"network": {
"ip_address": "192.168.1.115",
"wifi_rssi": -62,
"publish_interval_ms": 2000
},
"battery": {
"voltage_bus": 12.15,
"current_amps": -25.40,
"power_watts": -308.6,
"capacity_wh_remaining": 83.5,
"estimated_soc_pct": 76,
"estimated_runtime_seconds": 950
},
"status": {
"power_state": "on_battery",
"alert_level": "warning",
"reason": "capacity_low"
}
}| Power State | Description |
|---|---|
charging |
Voltage ≥ 13.2V AND Current > 0A |
on_battery |
Normal discharging (safe) |
warning |
Runtime ≤ 300s OR SOC ≤ 30% (3 samples) |
critical |
Runtime ≤ 120s OR SOC ≤ 10% OR Voltage ≤ 11.2V OR Temp > 75°C |
| Alert Level | Description |
|---|---|
none |
Normal operation |
warning |
Attention needed, system stable |
critical |
Immediate action required |
| Reason | Description |
|---|---|
capacity_low |
SOC or runtime below warning threshold |
voltage_low |
Voltage below critical threshold |
overheat |
Temperature exceeds 75°C |
[env:esp32-s3-devkitc-1]
build_flags =
-D BOARD_HAS_PSRAM # Enable PSRAM
-Isrc/include # Include path for headers
lib_deps =
robtillaart/INA226 # INA226 driver
bblanchon/ArduinoJson # JSON handling (v7)
knolleary/PubSubClient # MQTT client| Parameter | Warning | Critical |
|---|---|---|
| Runtime | ≤ 300s | ≤ 120s |
| SOC | ≤ 30% | ≤ 10% |
| Voltage | - | ≤ 11.2V |
| Temperature | > 65°C (throttle) | > 75°C (override) |
- Check wiring: SDA to GPIO8, SCL to GPIO9
- Verify shunt connection (low-side configuration)
- Check
Wire.setTimeOut(100)is preventing hangs
- Verify broker is running and accessible
- Check WiFi credentials in
wifi_manager.h - Monitor reconnection backoff (exponential, max 60s)
- Verify battery capacity setting (default: 108 Wh)
- Check deadband filter threshold (0.02A)
- Ensure coulomb counting formula uses correct time delta
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agre 5423 ed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.