679  Protocol State Machine Playground

Interactive Protocol Behavior Visualization

679.1 Protocol State Machine Playground

NoteLearning Objectives

By using this interactive tool, you will be able to:

  • Visualize how protocols transition between states
  • Understand message acknowledgment patterns
  • Explore error scenarios and recovery
  • Compare QoS behavior across MQTT, CoAP, and BLE

A state machine describes how a system behaves - what “state” it’s in and how it responds to events.

Concept Example
State Connected, Disconnected, Waiting
Transition An event that causes state change
Action What happens during a transition

Real Example: A light switch has two states (ON/OFF) and one transition (flip).

IoT protocols are more complex - they handle connections, messages, errors, and acknowledgments through carefully designed state machines.


679.2 Select Protocol


679.3 State Machine Visualization


679.4 Controls


679.5 Message Log


679.6 Scenario Walkthroughs

Step-by-step walkthrough:

  1. Client → Broker: CONNECT
    • Client sends connection request with client ID, credentials, clean session flag
    • State: Disconnected → Connecting
  2. Broker → Client: CONNACK
    • Broker accepts or rejects with return code
    • Return codes: 0=Accepted, 1=Bad protocol, 2=ID rejected, 4=Bad credentials, 5=Not authorized
    • State: Connecting → Connected
  3. Client → Broker: SUBSCRIBE
    • Client subscribes to topic filter with desired QoS
    • State: Connected → Subscribing
  4. Broker → Client: SUBACK
    • Broker confirms subscription with granted QoS
    • May grant lower QoS than requested
    • State: Subscribing → Connected
  5. Client → Broker: PUBLISH (QoS 1)
    • Client publishes message to topic
    • State: Connected → Publishing
  6. Broker → Client: PUBACK
    • Broker confirms receipt (QoS 1 only)
    • State: Publishing → Connected

Try it: Click CONNECT → CONNACK → SUBSCRIBE → SUBACK → PUBLISH → PUBACK

Step-by-step walkthrough:

  1. Client sends CON (Confirmable) Request
    • Request includes Message ID for matching
    • State: Idle → Waiting for ACK
  2. Normal case: ACK received
    • Server responds with ACK + same Message ID
    • Response piggybacked or separate
    • State: Waiting → Confirmed → Idle
  3. Timeout case: No ACK received
    • Wait RESPONSE_TIMEOUT (default 2s)
    • State: Waiting → Retrying
  4. Retransmission
    • Resend with exponential backoff
    • Max 4 retransmissions by default
    • State: Retrying → Waiting
  5. Max retries exceeded
    • Report failure to application
    • State: Retrying → Idle

Key difference from MQTT: CoAP is RESTful (request/response) vs MQTT’s pub/sub model.

Step-by-step walkthrough:

  1. Peripheral: Start Advertising
    • Broadcast advertisement packets on 3 channels
    • Contains device name, services, flags
    • State: Standby → Advertising
  2. Central: Start Scanning
    • Listen for advertisements
    • State: Standby → Scanning
  3. Central: Found Device
    • Parse advertisement, check services
    • State: Scanning → Initiating
  4. Central: Send Connection Request
    • CONNECT_IND packet with parameters
    • Includes connection interval, timeout
  5. Both: Connected
    • Begin connection events
    • Can exchange data via GATT
    • State: Initiating → Connected / Advertising → Connected

Key concept: BLE uses roles (Central/Peripheral) that determine who initiates connections.


679.7 QoS Comparison


679.8 Knowledge Check

NoteQuestion 1

In MQTT, what happens if a client publishes with QoS 1 but doesn’t receive a PUBACK?

  1. The message is lost
  2. The client retransmits until PUBACK received
  3. The broker ignores duplicates
  4. Both B and C

D) Both B and C - QoS 1 guarantees “at least once” delivery. The client retransmits on timeout, and the broker tracks Message IDs to handle duplicates. This can result in duplicate delivery if the original was received but PUBACK was lost.

NoteQuestion 2

Why does CoAP use UDP instead of TCP?

  1. UDP is more secure
  2. UDP has lower overhead for constrained devices
  3. TCP doesn’t work on wireless networks
  4. UDP supports larger messages

B) UDP has lower overhead for constrained devices - TCP’s connection establishment, congestion control, and guaranteed delivery add overhead. CoAP implements its own lightweight reliability (CON/ACK) when needed, making it more suitable for IoT devices with limited resources.

NoteQuestion 3

In BLE, which device initiates the connection?

  1. Always the Peripheral
  2. Always the Central
  3. Either device can initiate
  4. The device with stronger signal

B) Always the Central - In BLE’s connection model, Peripherals advertise and Centrals scan. Only Centrals can initiate connections. This is a fundamental asymmetry in BLE’s design. (Note: Some devices can switch roles.)