Objective: Compare QoS 0, 1, and 2 behavior under different conditions.
Tasks:
Set up local Mosquitto broker
Create a publisher that sends 100 messages with each QoS level
Use Wireshark or tcpdump to capture MQTT traffic
Analyze packet captures:
QoS 0: Single PUBLISH packet
QoS 1: PUBLISH + PUBACK
QoS 2: PUBLISH + PUBREC + PUBREL + PUBCOMP
(Optional) Simulate 20% packet loss and observe retransmissions
Measurement Template:
QoS Level
Packets Sent
Packets Received
ACKs
Latency (ms)
0
100
?
0
?
1
100
?
?
?
2
100
?
?
?
Hints:
Use mosquitto -v to run broker in verbose mode
Filter Wireshark by mqtt protocol
Simulate packet loss: tc qdisc add dev lo root netem loss 20%
1187.4 Common Pitfalls
WarningPitfall: Wrong MQTT Wildcard Usage
The Mistake: Using wildcards incorrectly leads to missing or unintended messages.
Wrong approach:
# INVALID: # wildcard not at endclient.subscribe("sensors/#/temperature")# INVALID: Mixing wildcards incorrectlyclient.subscribe("sensors/+/#/data")
Correct approach:
# Correct: Use + for single-level wildcardclient.subscribe("sensors/+/temperature")# Correct: Use # at the end onlyclient.subscribe("sensors/room1/#")# Correct: Multiple + wildcardsclient.subscribe("sensors/+/+/temperature")
WarningPitfall: Non-Unique Client ID
The Mistake: Multiple clients using the same client_id causes disconnection loops.
Why It Happens: MQTT brokers only allow one connection per client_id. When a second client connects with the same ID, the first is disconnected.
Wrong approach:
# All devices using same client ID - CAUSES DISCONNECTIONSclient.connect(broker, client_id="sensor")
Correct approach:
# Unique client ID based on device identitydevice_id = get_device_serial()client.connect(broker, client_id=f"sensor-{device_id}")# Or use MAC addressmac = get_mac_address()client.connect(broker, client_id=f"device-{mac}")
WarningPitfall: Using QoS 2 Everywhere
The Mistake: Setting QoS 2 for all messages thinking “higher is better.”
The Impact:
4x message overhead
3-4x battery drain
2-3 RTT additional latency
Unnecessary broker load
The Fix:
# Temperature reading every 30 seconds - QoS 0 is fineclient.publish("sensors/temp", "24.5", qos=0)# Motion alert - QoS 1 ensures deliveryclient.publish("alerts/motion", "detected", qos=1)# Door unlock command - QoS 2 prevents duplicatesclient.publish("actuators/door/unlock", "1", qos=2)
WarningPitfall: Retained Message Accumulation
The Mistake: Publishing large payloads with retain=true causes broker memory exhaustion.
The Problem: Retained messages persist indefinitely until cleared. A 1MB firmware binary retained on 100 topics = 100MB broker memory.
{const container =document.getElementById('kc-mqtt-practice-1');if (container &&typeof InlineKnowledgeCheck !=='undefined') { container.innerHTML=''; container.appendChild(InlineKnowledgeCheck.create({question:"A thermostat publishes its setpoint to 'home/thermostat/setpoint' with retain=true. Later, it's replaced with a new model publishing to a different topic. A month later, the old setpoint (68F) still appears on dashboards. What happened?",options: [ {text:"The broker is caching old messages incorrectly",correct:false,feedback:"This is expected MQTT behavior, not a bug. Retained messages persist until cleared."}, {text:"The old retained message was never cleared when the thermostat was decommissioned",correct:true,feedback:"Correct! Retained messages persist indefinitely. Decommissioning requires publishing an empty payload with retain=true to clear."}, {text:"The dashboard is subscribed to the wrong topic",correct:false,feedback:"The dashboard correctly receives the retained message. The issue is it should have been cleared."}, {text:"MQTT automatically expires retained messages after 30 days",correct:false,feedback:"MQTT 3.1.1 has no automatic expiry. Retained messages persist forever until cleared."} ],difficulty:"medium",topic:"mqtt-retained" })); }}
1187.5 Knowledge Check
TipQuestion 1: Architecture Model
What is the main difference between MQTT and HTTP communication?
Options:
MQTT uses peer-to-peer while HTTP uses client-server
MQTT uses publish-subscribe with a broker while HTTP uses request-response
MQTT requires persistent connections while HTTP cannot use them
MQTT only works over UDP while HTTP requires TCP
NoteAnswer
Correct: B)
MQTT decouples publishers and subscribers via a broker and topic-based routing. HTTP is direct client-to-server request/response.
TipQuestion 2: QoS Selection
A door lock command must be delivered exactly once. Which QoS level?
Options:
QoS 0
QoS 1
QoS 2
QoS 3
NoteAnswer
Correct: C) QoS 2
QoS 2 provides exactly-once delivery via 4-way handshake. QoS 0 has no guarantee, QoS 1 may duplicate. QoS 3 doesn’t exist.
TipQuestion 3: Wildcard Patterns
Which pattern receives temperature from ALL rooms on floor 3?
Options:
building/3/+/temperature
building/3/#
building/+/temperature
building/3/*/temperature
NoteAnswer
Correct: A)
+ matches exactly one level (the room). # would match all sensor types. * is not a valid MQTT wildcard.