0
0
Raspberry Piprogramming~10 mins

MQTT with QoS levels in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MQTT with QoS levels
Client publishes message
Check QoS level
QoS0
Done
Shows how MQTT handles message delivery differently based on QoS levels 0, 1, and 2.
Execution Sample
Raspberry Pi
client.publish(topic, message, qos=1)
# QoS 1: message sent, waits for PUBACK
# Resends if no PUBACK received
# Ensures message delivered at least once
Publishes a message with QoS 1, ensuring it is delivered at least once by waiting for acknowledgment.
Execution Table
StepActionQoS LevelMessage StateAcknowledgmentNext Step
1Client sends message0Sent onceNo ack expectedDone
2Client sends message1Sent onceWait for PUBACKIf no PUBACK, resend
3Client receives PUBACK1Message acknowledgedPUBACK receivedDone
4Client sends message2Sent onceWait for PUBRECSend PUBREL after PUBREC
5Client receives PUBREC2Message received by brokerPUBREC receivedSend PUBREL
6Client sends PUBREL2PUBREL sentWait for PUBCOMPWait for PUBCOMP
7Client receives PUBCOMP2Delivery completePUBCOMP receivedDone
8No ack received in QoS1 or QoS21 or 2Resend message or PUBRELRetry until ackRepeat steps as needed
💡 Message delivery completes when expected acknowledgments are received or no ack is expected (QoS 0).
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
Message StateNot sentSent onceAcknowledged (QoS1)Received by broker (QoS2)Delivery complete (QoS2)Done
AcknowledgmentNoneNonePUBACK receivedPUBREC receivedPUBCOMP receivedComplete
Retries000000 or more if no ack
Key Moments - 3 Insights
Why does QoS 1 resend the message sometimes?
Because QoS 1 waits for a PUBACK acknowledgment. If it doesn't arrive (see step 2 and 8 in execution_table), the client resends to ensure delivery.
What makes QoS 2 different from QoS 1 in message delivery?
QoS 2 uses a four-step handshake (steps 4 to 7) with PUBREC, PUBREL, and PUBCOMP to guarantee exactly once delivery, unlike QoS 1 which only waits for PUBACK.
Does QoS 0 wait for any acknowledgment?
No, QoS 0 sends the message once without waiting for any acknowledgment (step 1), so delivery is not guaranteed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the client receive PUBACK for QoS 1?
AStep 5
BStep 3
CStep 7
DStep 1
💡 Hint
Check the 'Acknowledgment' column for QoS 1 in execution_table row 3.
At which step does the client send PUBREL in QoS 2 delivery?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for 'Client sends PUBREL' action in execution_table.
If no PUBACK is received in QoS 1, what happens according to the execution_table?
AMessage is dropped
BClient sends PUBREL
CClient resends message
DNo action
💡 Hint
See step 8 about retries when no acknowledgment is received.
Concept Snapshot
MQTT QoS levels control message delivery:
QoS 0: Send once, no ack, no guarantee.
QoS 1: Send, wait PUBACK, resend if needed (at least once).
QoS 2: Four-step handshake (PUBREC, PUBREL, PUBCOMP) for exactly once.
Higher QoS means more reliability but more overhead.
Full Transcript
This visual execution shows how MQTT handles message delivery with different Quality of Service (QoS) levels. When a client publishes a message, it checks the QoS level. For QoS 0, the message is sent once without waiting for acknowledgment. For QoS 1, the client sends the message and waits for a PUBACK acknowledgment; if none arrives, it resends the message to ensure delivery at least once. For QoS 2, a four-step handshake occurs: the client sends the message, waits for PUBREC, then sends PUBREL, and finally waits for PUBCOMP to guarantee exactly once delivery. Variables like message state and acknowledgments change step by step, showing how the client tracks delivery progress. Key moments clarify why QoS 1 resends messages, how QoS 2 differs with its handshake, and that QoS 0 does not wait for any acknowledgment. The quiz tests understanding of these steps and behaviors.