Brown-out
Our ESP32 sensor node runs on a tired battery. Its ESP32-WROOM-32 module holds 3.3 V flash memory, so it needs 3.0 to 3.6 volts. Awake with the radio off it draws about 50 mA, and the meter reads 3.15 V.
Wi-Fi starts sending and the radio draws about 240 mA. A battery has inner resistance, so more current means less voltage: 240 mA through 4 ohms loses 0.96 V. The rail sags to 2.39 V.
A brown-out is a dip, not a blackout: the power is low but not gone. At 2.39 V the module is below its 3.0 V minimum, so it may misread flash or store wrong values. Half-working is worse than stopped.
A brown-out detector is a comparator built into the chip. It checks the supply against a set level, here 2.43 V. 2.39 is below 2.43, so it pulls the reset line and stops the CPU before it misbehaves.
Resets
A reset stops everything and boots the chip again, like a fresh start. SRAM variables begin from scratch, so our reading count n is gone. But the hardware notes why it reset, and setup() runs first.
Line 2 calls esp_reset_reason(). It returns ESP_RST_BROWNOUT. Other answers include ESP_RST_POWERON for a fresh battery, ESP_RST_PANIC for a crash and watchdog resets. Good firmware checks this first.
If we start Wi-Fi again at once, the same dip resets us again, forever. So line 3 calls rest(): deep sleep for 60 seconds. With almost no current drawn, the rail climbs back to about 3.35 V.
Faults
Now a software fault. startWiFi() reads cfg->ssid, but cfg is a null pointer, address 0. Nothing lives there, so the CPU raises an exception: LoadProhibited. The panic handler prints the registers, then restarts the chip.
Line 4 adds 1 to crashes on each panic. crashes lives in .noinit memory, which startup does not clear, so a software restart keeps it. Any other reset sets it back to 0 on line 5.
After 3 panics in a row, line 6 calls backOff(): deep sleep for an hour. A crash loop would run the radio again and again and empty the battery. Sleeping keeps the node alive until the fault clears or is fixed.
What happens
;; step 1/10 · rail
module ESP32-WROOM-32: 3.0 V to 3.6 V
(its 3.3 V flash sets the 3.0 V floor)
chip the bare ESP32 alone: 2.3 V to 3.6 V
battery 3.35 V behind 4 ohms (worn cell, thin wires)
idle 50 mA -> 3.35 - 0.05 x 4 = 3.15 V
The battery is modelled as 3.35 V behind 4 ohms, an assumed value for a worn cell plus thin wires; a real battery's voltage also recovers slowly. The 240 mA and 30-68 mA figures are the ESP32 datasheet's typical values at 3.3 V. The Wi-Fi radio and the brown-out detector are inside the ESP32. The figure draws them as separate blocks so you can see the current and the comparison.