- Nov 23, 2026
- 4 min read
72 Hours on Battery, and the Winter Firmware That Sleeps by the Clock
The October firmware had a simple schedule:
- wake, stay up 5 minutes, publish every 5 seconds;
- deep-sleep 20 minutes;
- stay awake 08:30–09:30 so the pump can be controlled in the morning.
On battery, it lasted about 72 hours.
The maths I should have done first
Outside the morning window the board is awake 5 minutes out of every 25: about 20% of the time. Add the hour-long control window and that’s 5.5 to 6 hours of Wi-Fi a day. On an ESP32, Wi-Fi is the battery. The sensors, the ADC and the CPU barely register next to the radio.
So the 72 hours wasn’t a mystery. It was the schedule.
Two bugs that testing didn’t find
Both showed up the moment I explained the logic out loud.
1. A fixed timer drifts against the clock. “Sleep 20 minutes” doesn’t know what time it is. The morning window started at the first wake after 08:30, which could be 08:48. The schedule said 08:30; the board said “roughly”.
2. No time, no sleep. If NTP failed, the firmware stayed awake so it wouldn’t miss the control window. Safe for the pump schedule, fatal for the battery: a Wi-Fi outage meant the board drained itself waiting for it.
The winter rewrite
Same pins, same MQTT topics, same commands. Different idea of time:
| October | Winter | |
|---|---|---|
| Control window | 08:30–09:30 | 09:00–09:15 |
| Other hours | 5 min awake / 20 min asleep | awake HH:00–HH:01 only |
| Sleep length | fixed 20 min | computed from the clock |
| No valid time | stay awake forever | awake max 3 min, sleep 15, retry |
| Awake per day | ~5.5–6 h | ~40 min |
The core of it is that sleep length is computed, not fixed:
sleep = seconds until the next full hour − 10 s
The ESP32 keeps time through deep sleep on its RTC. On every wake, NTP corrects it (up to 20 seconds of grace, then the RTC is trusted). If RTC drift wakes it more than 90 seconds early, it goes straight back to sleep until the hour. Wakes stay on the hour instead of sliding around the day.
A few more rules came out of the review:
- At least 45 seconds awake after a timer wake, so a publish cycle always completes; 3 minutes after power-on, so I can check it after flashing.
- Don’t sleep in the middle of a pump pulse.
- The blue LED stays dark from 22:00 to 08:00. The board sits near a bedroom, and the connect blinks were visible at night.
The awake time drops from roughly 6 hours to roughly 40 minutes a day (23 one-minute wakes plus the 15-minute window).
Result: not measured yet
That’s the honest status. The design says “several times longer”. The battery hasn’t been run down yet. When it is, the number goes here, next to the 72-hour baseline.
What I expect to find: once Wi-Fi time is this low, the board itself becomes the limit. Dev boards have a USB-serial chip, a regulator and an LED that draw current even in deep sleep. Measuring that sleep current is the next step, and it may say “this board, not this firmware”.
Flashing gotchas from the same week
- Opening the serial port resets the board on this ESP32, even with DTR and RTS held low. Every monitor session is a fresh boot, so you’ll always see the 3-minute power-on window, never a “real” timer wake. Watch MQTT instead to check a sleeping board without disturbing it.
- Uploading works while it sleeps. The auto-reset line wakes it. If it doesn’t, hold BOOT while the upload starts.
- Keep the pump’s hardware switch off while flashing or resetting.
bootCountlives in RTC memory, so it survives deep sleep but resets after a flash or power loss. Handy for telling the two apart in logs.
Takeaways
- Count radio-on hours first. Battery life follows them.
- Sleep by the clock, not by a timer, if anything needs to happen at a time.
- Every “stay awake until X” needs a timeout.
- Explaining code out loud found both bugs. Tests didn’t.
Next in this series: Reason 15: The Wi-Fi Password That Was Right and Wrong
Part 5 of 8. Start with the series overview.