Wake-to-publish latency and energy per message
How we time an ESP32-C5 from a GPIO wake edge to an acknowledged MQTT publish, why no clock sync is needed, and how energy per message is integrated from the same trace.
| Instrument | Power profiler with digital inputs in the same time base (see ops/BENCH.md) |
|---|---|
| Updated | 28 November 2026 |
A battery sensor spends most of its energy in the seconds between waking and getting its reading to the broker. This method measures that time and that energy on the same run, so the two numbers describe the same wakes. It is written for the ESP32-C5-DevKitC-1 and ESP-IDF, and it carries over to any board that can drive a spare GPIO.
What we measure
For each wake we record three things:
- Device latency: the time from the wake edge on the board’s GPIO to the moment the firmware learns that the broker has the message. The firmware raises a marker GPIO at that moment.
- Host latency: the time from the bench computer’s wake command to the message arriving at a subscriber on the broker’s computer.
- Energy per message: the charge and energy the board draws from the wake edge until it is back in deep sleep.
“The broker has the message” needs a precise meaning. We publish at QoS 1. The ESP-MQTT documentation says MQTT_EVENT_PUBLISHED is posted when the broker acknowledges a publish, and only for QoS 1 and 2. The firmware raises the marker in that event handler, so device latency ends at the broker’s acknowledgement (PUBACK) arriving back at the board.
Clocks: why no synchronisation is needed
A latency is the difference of two timestamps. If they come from two clocks, the offset between those clocks adds straight to the result. This method avoids the problem by keeping each latency on one clock.
- Device latency uses the instrument’s own time base. The wake line and the marker line go to two digital inputs that the instrument samples together with current. The timing resolution is the digital input’s sample interval. The JS220 datasheet gives 2 MSPS for its inputs. The PPK2 product page lists 8 digital inputs and the Otii Arc Pro has GPI1 and GPI2, but we did not find their digital sample rates in the pages we read, so check your instrument and record the rate.
- Host latency uses one computer’s monotonic clock.
bench.py latencytoggles the wake line through a USB serial adapter and timestamps the command. The same process subscribes to the broker on the same computer and timestamps each arrival. - The board’s clock is never used. Its timestamps are not compared with anything.
If you run the broker on a different computer from the subscriber, you need both clocks synchronised and the offset becomes an error term. We do not do that. We also do not use the broker’s log for timing: by default Mosquitto’s log_timestamp is seconds since the Unix epoch, and log_timestamp_format is passed to strftime. For a manual cross-check, mosquitto_sub -F '%U %t' prints a Unix timestamp with nanoseconds for each message.
Host latency includes the delay between the computer’s command and the edge on the wire, through USB and the adapter. We do not know that delay and do not subtract it. Device latency is the headline figure. Host latency is reported beside it, for comparing configurations on the same bench.
Firmware
The firmware is the example lab-wake-publish in examples/lab-wake-publish/, built with ESP-IDF v6.1.1 in espressif/idf:v6.1.1. Since ESP-IDF v6.0, ESP-MQTT is a separate component, espressif/mqtt, and the example’s manifest pins its version. On each wake the firmware:
- prints
LAB wake cause=gpio(ortimerwhen woken by its fallback timer) - joins the access point, using credentials from a gitignored configuration file
- connects to the broker over plain MQTT on the bench network, with no TLS in the baseline
- publishes a short payload at QoS 1 to
lab/wake/<unit> - on
MQTT_EVENT_PUBLISHED, raises the marker GPIO and printsLAB published msg_id=<id> t_ms=<ms since boot> - returns to deep sleep, waiting for the wake GPIO
Settings that change the result are recorded with every run through --meta: DHCP or a static address, whether the firmware stores the access point’s channel and BSSID for the next join, TLS on or off, the QoS level and the payload size. Each variant is a separate run. ESP-IDF’s default Wi-Fi power-save type is WIFI_PS_MIN_MODEM. The firmware states the type it sets.
Bench setup
- Supply. Configuration M from the current-measurement method: 3.300 V from the instrument into J1 pin 1, J5 removed, both USB cables unplugged. The functional check runs on USB before that.
- Wake line. The RTS pin of a 3.3 V USB serial adapter goes to the wake GPIO and to instrument digital input 0. Check the polarity on the instrument before the first run.
- Marker line. The marker GPIO goes to instrument digital input 1.
- Ground. Adapter, instrument and board share ground.
- Network. One access point with a fixed channel. The broker (Mosquitto) and the subscriber run on the bench computer, which is wired to the access point’s LAN. Record the access point model and firmware, the channel and band, and the Mosquitto version.
Procedure
Run the commands from the repository root with the iotfieldguide Python environment. Credentials and the broker address come from the gitignored .env, and tools/bench/env.example lists the keys. All files for one unit go in one folder, content-data/measurements/wake-to-publish-latency-u1/ for unit u1.
- Flash the firmware and run the functional check on USB. In a second terminal, start the wake pulses while the capture runs.
python tools/bench/bench.py flash --port <port> --example lab-wake-publish --target esp32c5
python tools/bench/bench.py capture --port <port> --seconds 400 --reset \
--expect tools/bench/expect/lab-wake-publish.txt --board-rev "ESP32-C5-DevKitC-1 v1.2" \
--test wake-to-publish-latency --example lab-wake-publish --unit u1 \
--run-id wake-to-publish-latency-u1 --meta ap_model="<model, firmware>" --meta channel="<band, channel>"
python tools/bench/bench.py latency --wake-port <adapter-port> --period 10 --count 30 \
--topic 'lab/wake/#' --run-id wake-to-publish-latency-u1 --label usb
- Unplug USB and set up configuration M. Start recording on the instrument with current, voltage and digital inputs 0 and 1.
- Send 30 wakes and stop the recording after the last one returns to sleep:
python tools/bench/bench.py latency --wake-port <adapter-port> --period 10 --count 30 \
--topic 'lab/wake/#' --run-id wake-to-publish-latency-u1 --label m
- Export the trace as
content-data/measurements/wake-to-publish-latency-u1/trace-m.csv(format below) and compute latency and energy:
python tools/calc/energy_per_message.py content-data/measurements/wake-to-publish-latency-u1/trace-m.csv \
--wake-col d0 --marker-col d1 --threshold 0.001 --pad 0.002 --json \
> content-data/measurements/wake-to-publish-latency-u1/wake-m.json
- Repeat for each unit and each firmware variant, then record each run:
python tools/bench/bench.py record --run wake-to-publish-latency-u1 --test wake-to-publish-latency \
--device esp32-c5-devkitc-1 --units 1 --instrument "<model> (see ops/BENCH.md)" \
--settings "lab-wake-publish; QoS 1; DHCP; no TLS; <band, channel>; supply M" --summary "<medians, see wake-m.json>"
The first three wakes after flashing or power-up are excluded from the summary and kept in the raw data. A wake with no marker counts as a failed message and is reported, not dropped.
Raw data
| File | Content |
|---|---|
serial.log, run.json |
Functional check on USB (identifiers masked by the runner) |
host-latency-usb.csv, host-latency-m.csv |
n,edge_host_s,arrival_host_s,latency_ms, written by bench.py latency; times relative to the first wake command |
trace-m.csv |
t_s,current_a,voltage_v,d0,d1; seconds, amperes, volts, and 0 or 1 for the wake and marker lines |
wake-m.json |
Per-wake latency and energy from energy_per_message.py, with the summary |
How energy per message is computed
Each rising edge on d0 starts a wake. The latency is the time to the first rising edge on d1 before the next wake. The energy window runs from the wake edge to the last sample above 1 mA before the next wake, plus 2 ms. Charge and energy are trapezoidal integrals of current and of voltage times current over that window (tools/calc/energy_per_message.py). The sleep current between wakes is not included. The current-measurement method gives it separately, so a battery estimate can add the two.
Known sources of error
- Digital sample interval. It limits how finely an edge is placed. Record it.
- Instrument accuracy and voltage drop. As in the current-measurement method. The JS220 datasheet also reports up to 10 µA of induced current in a 3 V/m field test, which matters for the sleep part of a window.
- Adapter delay. Host latency includes an unknown USB and adapter delay, as explained above.
- The network. Other traffic on the access point, neighbouring networks on the same channel, the DHCP server and the broker computer’s load all move the result. Results in another building, with another access point or broker, will differ.
- First wakes. Wakes straight after power-up or flashing behave differently from later ones, which is why the first three are excluded.
- Unit-to-unit spread. We test several units and report them separately.
How results are reported
For each unit and firmware variant: device latency and energy per message as median, minimum and maximum over the counted wakes, host latency the same way, and the number of wakes with and without a marker. Across units we give the spread, never only the fastest unit. Every result names the access point model and firmware, channel and band, distance, the broker version, the instrument and its digital sample rate, the firmware commit and the ESP-IDF version.
For makers
If you have no instrument with digital inputs, run steps 1 and 3 on USB power. bench.py latency alone gives host latency with one cable and a USB serial adapter. It includes the adapter delay, but it is enough to see whether a static address or a stored channel shortens your wake.
For product teams
Energy per message sets battery life for most Wi-Fi sensors. Measure it with the TLS and QoS settings you will ship, on the access points your customers use, and on several units. Keep the per-wake raw data: the slow tail, not the median, decides whether a device misses its reporting window.