Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does QoS level 1 guarantee in MQTT communication?
easy
A. At least once delivery of the message
B. Exactly once delivery of the message
C. At most once delivery of the message
D. No delivery guarantee

Solution

  1. Step 1: Understand QoS levels in MQTT

    QoS 0 means no guarantee, QoS 1 means at least once, QoS 2 means exactly once.
  2. Step 2: Identify QoS 1 guarantee

    QoS 1 ensures the message is delivered at least once, but duplicates may occur.
  3. Final Answer:

    At least once delivery of the message -> Option A
  4. Quick Check:

    QoS 1 = At least once delivery [OK]
Hint: QoS 1 means message arrives at least once [OK]
Common Mistakes:
  • Confusing QoS 1 with exactly once delivery
  • Thinking QoS 1 has no delivery guarantee
  • Mixing QoS 0 and QoS 1 guarantees
2. Which of the following is the correct way to publish a message with QoS 2 using the Paho MQTT Python client?
easy
A. client.publish(topic, payload, qos=1)
B. client.publish(topic, payload, qos=3)
C. client.publish(topic, payload, qos=2)
D. client.publish(topic, payload, qos=0)

Solution

  1. Step 1: Recall valid QoS values in MQTT

    MQTT QoS levels are 0, 1, and 2 only; 3 is invalid.
  2. Step 2: Check syntax for publishing with QoS 2

    The Paho client uses qos parameter in publish method; qos=2 is valid.
  3. Final Answer:

    client.publish(topic, payload, qos=2) -> Option C
  4. Quick Check:

    QoS 2 publish syntax = client.publish(..., qos=2) [OK]
Hint: QoS must be 0, 1, or 2; 2 is highest reliability [OK]
Common Mistakes:
  • Using qos=3 which is invalid
  • Omitting qos parameter defaults to 0
  • Confusing qos=1 with qos=2 syntax
3. Consider this Python code snippet using Paho MQTT client:
client.publish('sensor/data', 'temp=22', qos=0)
client.publish('sensor/data', 'temp=23', qos=1)
client.publish('sensor/data', 'temp=24', qos=2)
What is the expected behavior regarding message delivery?
medium
A. All messages are delivered exactly once
B. First message may be lost, second delivered at least once, third exactly once
C. All messages may be lost without retry
D. First message delivered at least once, others may be duplicated

Solution

  1. Step 1: Analyze QoS 0 message delivery

    QoS 0 means message sent once, no retry, may be lost.
  2. Step 2: Analyze QoS 1 and QoS 2 messages

    QoS 1 guarantees at least once delivery (may duplicate), QoS 2 guarantees exactly once delivery.
  3. Final Answer:

    First message may be lost, second delivered at least once, third exactly once -> Option B
  4. Quick Check:

    QoS 0=may lose, QoS 1=at least once, QoS 2=exactly once [OK]
Hint: Remember QoS 0 no retry, QoS 1 retry, QoS 2 handshake [OK]
Common Mistakes:
  • Assuming QoS 0 always delivers
  • Confusing QoS 1 with exactly once
  • Ignoring possible duplicates at QoS 1
4. You wrote this code to publish a message with QoS 2:
client.publish('home/lights', 'ON', qos=2)
But the message is sometimes duplicated on the subscriber side. What is the likely cause?
medium
A. The client.publish syntax is incorrect
B. QoS 2 does not guarantee no duplicates
C. QoS 2 is not supported by the broker
D. The subscriber is not handling duplicate messages properly

Solution

  1. Step 1: Understand QoS 2 guarantees

    QoS 2 guarantees exactly once delivery, so duplicates should not occur in transmission.
  2. Step 2: Identify cause of duplicates

    If duplicates appear, likely the subscriber application is not filtering duplicates properly.
  3. Final Answer:

    The subscriber is not handling duplicate messages properly -> Option D
  4. Quick Check:

    QoS 2 = no duplicates in network, subscriber must handle duplicates [OK]
Hint: Duplicates often come from subscriber logic, not QoS 2 itself [OK]
Common Mistakes:
  • Blaming QoS 2 for duplicates
  • Assuming broker always supports QoS 2
  • Thinking publish syntax is wrong
5. You want to design a Raspberry Pi sensor system that sends temperature data reliably without duplicates, but network is unstable. Which MQTT QoS level should you choose and why?
hard
A. QoS 2, to guarantee exactly once delivery with no duplicates
B. QoS 1, to ensure message arrives at least once
C. QoS 1, because QoS 2 is not supported on Raspberry Pi
D. QoS 0, for fastest delivery without retries

Solution

  1. Step 1: Analyze requirements for reliability and no duplicates

    The system needs reliable delivery and no duplicate messages despite unstable network.
  2. Step 2: Match QoS levels to requirements

    QoS 0 is fastest but unreliable, QoS 1 may duplicate, QoS 2 guarantees exactly once delivery.
  3. Step 3: Confirm Raspberry Pi supports QoS 2

    MQTT clients on Raspberry Pi support QoS 2, so it is suitable.
  4. Final Answer:

    QoS 2, to guarantee exactly once delivery with no duplicates -> Option A
  5. Quick Check:

    Reliable + no duplicates = QoS 2 [OK]
Hint: Choose QoS 2 for no duplicates and reliable delivery [OK]
Common Mistakes:
  • Choosing QoS 1 and ignoring duplicates
  • Thinking QoS 0 is reliable
  • Assuming Raspberry Pi can't use QoS 2
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard