Skip to content

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., your practice guide

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 Tool

Verify normal, timeout, wrong-state, fault, and reset transitions with buttons, LEDs, and structured serial proof.

Tier 1 · Web · No account

Version tested: Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-10. Date: 2026-09-10.

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.

diagram.json

2,318 bytes · Circuit connections

Download

sketch.ino

2,345 bytes · ESP32 program

Download

  1. Use the launch button above to open the ESP32 editor in Wokwi.
  2. Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
  3. Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.

Steps

Screens captured against Wokwi ESP32 Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-10 on 2026-09-10; the tool may have moved on — the text steps are the contract.

  1. 1 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: Three event buttons and four state LEDs connect to the ESP32. The ring marks the explicit event and state boundary.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 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: Four named states, the transition logger, and rejected-event logger are visible. The ring marks the state contract.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 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: The machine starts in IDLE with retries=0. The ring marks the initial-state proof.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 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: The event trace moves IDLE to SAMPLE to TRANSMIT with message_id=42. The ring marks the normal path.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 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: The acknowledgement closes the message and returns to IDLE. The ring marks the accepted transition.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 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: Two bounded retries end in FAULT with ack_timeout_radio_off. The ring marks the retry-limit proof.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 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: A late acknowledgement is rejected in FAULT. The ring marks the wrong-state decision.
    Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  8. 8 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: A manual safe reset returns FAULT to IDLE with retries cleared. The ring marks the recovery proof.
    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.

  1. 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 check
  2. A TRANSMIT state retries forever after acknowledgement timeouts. Which transition-table change most directly fixes the design?

    Return to the chapter’s knowledge check
  3. A 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

Caution

The buttons simulate events and the timer simulates sampling and acknowledgement deadlines. This lab does not prove real sensor validity, radio callbacks, interrupt safety, persistence, brownout recovery, or queue overflow behavior. Keep 220 ohm LED resistors on physical hardware and define reset authority before actuation.

Return to State Machine Fundamentals · Browse Labs