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
TipFor Beginners: What is a State Machine?
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
NoteMQTT: Normal Connection Flow
Step-by-step walkthrough:
- Client → Broker: CONNECT
- Client sends connection request with client ID, credentials, clean session flag
- State: Disconnected → Connecting
- 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
- Client → Broker: SUBSCRIBE
- Client subscribes to topic filter with desired QoS
- State: Connected → Subscribing
- Broker → Client: SUBACK
- Broker confirms subscription with granted QoS
- May grant lower QoS than requested
- State: Subscribing → Connected
- Client → Broker: PUBLISH (QoS 1)
- Client publishes message to topic
- State: Connected → Publishing
- Broker → Client: PUBACK
- Broker confirms receipt (QoS 1 only)
- State: Publishing → Connected
Try it: Click CONNECT → CONNACK → SUBSCRIBE → SUBACK → PUBLISH → PUBACK
NoteCoAP: Confirmable Request with Retry
Step-by-step walkthrough:
- Client sends CON (Confirmable) Request
- Request includes Message ID for matching
- State: Idle → Waiting for ACK
- Normal case: ACK received
- Server responds with ACK + same Message ID
- Response piggybacked or separate
- State: Waiting → Confirmed → Idle
- Timeout case: No ACK received
- Wait RESPONSE_TIMEOUT (default 2s)
- State: Waiting → Retrying
- Retransmission
- Resend with exponential backoff
- Max 4 retransmissions by default
- State: Retrying → Waiting
- 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.
NoteBLE: Connection Establishment
Step-by-step walkthrough:
- Peripheral: Start Advertising
- Broadcast advertisement packets on 3 channels
- Contains device name, services, flags
- State: Standby → Advertising
- Central: Start Scanning
- Listen for advertisements
- State: Standby → Scanning
- Central: Found Device
- Parse advertisement, check services
- State: Scanning → Initiating
- Central: Send Connection Request
- CONNECT_IND packet with parameters
- Includes connection interval, timeout
- 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?
- The message is lost
- The client retransmits until PUBACK received
- The broker ignores duplicates
- Both B and C
TipAnswer
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?
- UDP is more secure
- UDP has lower overhead for constrained devices
- TCP doesn’t work on wireless networks
- UDP supports larger messages
TipAnswer
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?
- Always the Peripheral
- Always the Central
- Either device can initiate
- The device with stronger signal
TipAnswer
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.)