0
0
Raspberry Piprogramming~20 mins

MQTT with QoS levels in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MQTT QoS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
MQTT QoS 0 Message Delivery
What will be the output on the subscriber console when a message is published with QoS 0?
Raspberry Pi
import paho.mqtt.client as mqtt

# Callback when a message is received
def on_message(client, userdata, msg):
    print(f"Received message: {msg.payload.decode()} with QoS {msg.qos}")

client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("test/qos0", qos=0)
client.loop_start()

publisher = mqtt.Client()
publisher.connect("test.mosquitto.org", 1883, 60)
publisher.publish("test/qos0", payload="Hello QoS0", qos=0)

import time
time.sleep(2)
client.loop_stop()
AReceived message: Hello QoS0 with QoS 0
BReceived message: Hello QoS0 with QoS 1
CNo message received
DRuntimeError: MQTT connection failed
Attempts:
2 left
💡 Hint
QoS 0 means the message is delivered at most once without acknowledgment.
Predict Output
intermediate
2:00remaining
MQTT QoS 1 Message Acknowledgment
What output will the subscriber show when a message is published with QoS 1?
Raspberry Pi
import paho.mqtt.client as mqtt

def on_message(client, userdata, msg):
    print(f"Received message: {msg.payload.decode()} with QoS {msg.qos}")

client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("test/qos1", qos=1)
client.loop_start()

publisher = mqtt.Client()
publisher.connect("test.mosquitto.org", 1883, 60)
publisher.publish("test/qos1", payload="Hello QoS1", qos=1)

import time
time.sleep(2)
client.loop_stop()
ATimeoutError: Message not acknowledged
BReceived message: Hello QoS1 with QoS 0
CNo message received
DReceived message: Hello QoS1 with QoS 1
Attempts:
2 left
💡 Hint
QoS 1 guarantees message delivery at least once with acknowledgment.
🧠 Conceptual
advanced
2:00remaining
MQTT QoS 2 Message Flow
Which sequence correctly describes the MQTT QoS 2 message delivery handshake between publisher and broker?
APUBLISH -> PUBCOMP -> PUBREL -> PUBREC
BPUBLISH -> PUBREC -> PUBREL -> PUBCOMP
CPUBLISH -> PUBREL -> PUBREC -> PUBCOMP
DPUBLISH -> PUBACK -> PUBREC -> PUBREL
Attempts:
2 left
💡 Hint
QoS 2 ensures exactly once delivery with a four-step handshake.
Predict Output
advanced
2:00remaining
Effect of QoS Level on Message Duplication
If a publisher sends a message with QoS 1 and the subscriber receives it twice, what is the likely cause?
AQoS 1 can cause duplicate messages due to retransmission until acknowledgment
BQoS 1 guarantees no duplicates, so this is impossible
CSubscriber is ignoring messages with QoS 1
DBroker is dropping messages silently
Attempts:
2 left
💡 Hint
QoS 1 ensures delivery at least once, so duplicates can happen.
🔧 Debug
expert
3:00remaining
Debugging MQTT QoS 2 Message Loss
Given this code snippet, why might the subscriber never receive messages published with QoS 2? client.subscribe("test/qos2", qos=2) # on_message callback is missing client.loop_forever()
Raspberry Pi
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("test/qos2", qos=2)

client.loop_forever()
AThe client must call loop_start() instead of loop_forever() for QoS 2
BQoS 2 is not supported by the broker, so messages are dropped
CNo on_message callback is set, so messages are received but not processed or printed
DSubscription QoS must be 0 for messages to be received
Attempts:
2 left
💡 Hint
Without an on_message callback, messages arrive but are ignored silently.