Exercise an ESP32 device state machine
Verify normal, timeout, wrong-state, fault, and reset transitions with buttons, LEDs, and structured serial proof.

Gateway Gus: I want every button, timeout, guard, retry, and reset to leave a state transition another engineer can audit.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolVerify normal, timeout, wrong-state, fault, and reset transitions with buttons, LEDs, and structured serial proof.
Open the ESP32 editor, paste diagram.json, then paste sketch.ino.
Open Wokwi to paste in the files (new tab)Get the files
Use both prepared files. This is a paste-in setup; saving a project requires a Wokwi account.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
Step 1
- Do
- Paste the supplied diagram and sketch into Wokwi; identify the three event buttons and four state LEDs.
- You will see
- Sample, Ack, and Fault / Reset buttons feed GPIO12-14; white, yellow, green, and red LEDs represent IDLE, SAMPLE, TRANSMIT, and FAULT.
- Why it matters
- Named inputs and outputs make the state contract observable.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- Inspect the State enum, enterState function, and wrong-state reject function.
- You will see
- The code names four states and logs every accepted or rejected event with context.
- Why it matters
- The transition owner should be one dispatcher, not unrelated callback side effects.

Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 3
- Do
- Start the simulation and open Serial Monitor at 115200 baud.
- You will see
- ESP32 STATE MACHINE READY appears followed by STATE=IDLE retries=0, with the white LED on.
- Why it matters
- A known initial state makes every later event traceable.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- Press Sample once and wait for the simulated sample to complete.
- You will see
- IDLE + EV_SAMPLE_REQUEST -> SAMPLE is followed by SAMPLE + EV_SAMPLE_READY -> TRANSMIT and message_id=42.
- Why it matters
- This exercises the normal request and completion rows from the page's transition table.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- Press Ack before the transmit timeout expires.
- You will see
- TRANSMIT + EV_ACK_RECEIVED -> IDLE appears with proof=message_closed and retries=0.
- Why it matters
- The acknowledgement closes the pending work and returns to a known state.

Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 6
- Do
- Press Sample again, do not acknowledge, and wait through three transmit timeouts.
- You will see
- Retries increment to 1 and 2, then TRANSMIT + EV_TIMEOUT -> FAULT reports ack_timeout_radio_off at retries=3.
- Why it matters
- A retry guard prevents the page's explicit unbounded-retry failure.

Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 7
- Do
- Press Ack while the machine is in FAULT.
- You will see
- EVENT_REJECTED state=FAULT event=EV_ACK_RECEIVED reason=wrong_state appears and the red LED stays on.
- Why it matters
- Late or wrong-state events must be explicit and must not silently change device state.

Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 8
- Do
- Press Fault / Reset once while FAULT is active.
- You will see
- FAULT + EV_RESET -> IDLE appears with proof=manual_reset_safe and retries=0.
- Why it matters
- Recovery should record why reset was allowed and return outputs and context to a known state.

Step 8 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
A sensor alternates between SLEEP, SAMPLE, TRANSMIT, and FAULT. Why is this a good fit for a finite state machine?
Return to the chapter’s knowledge checkA TRANSMIT state retries forever after acknowledgement timeouts. Which transition-table change most directly fixes the design?
Return to the chapter’s knowledge checkA firmware design handles radio callbacks, sensor completions, button presses, and timer expirations. What is the safest state-machine pattern?
Return to the chapter’s knowledge check