MQTT with QoS levels helps control how messages are sent and received reliably between devices.
When you want to make sure a message is delivered at least once.
When you want to avoid duplicate messages.
When you want the fastest message delivery without guarantees.
When you need reliable communication between sensors and a server.
When you want to balance speed and reliability in message delivery.
Syntax
Raspberry Pi
client.publish(topic, message, qos=level)
# qos levels:# 0 - At most once (fast, no guarantee)# 1 - At least once (guaranteed delivery, possible duplicates)# 2 - Exactly once (guaranteed delivery, no duplicates)
The qos parameter controls message delivery quality.
Higher QoS means more reliability but slower delivery.
Examples
Sends temperature once, no guarantee it arrives.
Raspberry Pi
client.publish('home/temperature', '22C', qos=0)
Sends temperature and retries until confirmed received.
Raspberry Pi
client.publish('home/temperature', '22C', qos=1)
Sends temperature exactly once with full handshake.
Raspberry Pi
client.publish('home/temperature', '22C', qos=2)
Sample Program
This program connects to a public MQTT broker and sends three messages with QoS levels 0, 1, and 2. It then disconnects and prints a confirmation.
Raspberry Pi
import paho.mqtt.client as mqtt
# Define MQTT broker address
broker = 'test.mosquitto.org'# Create MQTT client
client = mqtt.Client()
# Connect to broker
client.connect(broker, 1883, 60)
# Publish messages with different QoS levels
client.publish('test/qos', 'Message with QoS 0', qos=0)
client.publish('test/qos', 'Message with QoS 1', qos=1)
client.publish('test/qos', 'Message with QoS 2', qos=2)
# Disconnect
client.disconnect()
print('Messages sent with QoS 0, 1, and 2')
OutputSuccess
Important Notes
QoS 0 is fastest but may lose messages.
QoS 1 retries sending until acknowledged, so duplicates can happen.
QoS 2 ensures message is received exactly once but is slower.
Summary
MQTT QoS controls message delivery reliability.
Use QoS 0 for speed, QoS 1 for guaranteed delivery, QoS 2 for no duplicates.
Choose QoS based on your application's needs for speed and reliability.
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
Step 1: Understand QoS levels in MQTT
QoS 0 means no guarantee, QoS 1 means at least once, QoS 2 means exactly once.
Step 2: Identify QoS 1 guarantee
QoS 1 ensures the message is delivered at least once, but duplicates may occur.
Final Answer:
At least once delivery of the message -> Option A
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
Step 1: Recall valid QoS values in MQTT
MQTT QoS levels are 0, 1, and 2 only; 3 is invalid.
Step 2: Check syntax for publishing with QoS 2
The Paho client uses qos parameter in publish method; qos=2 is valid.
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
Step 1: Understand QoS 2 guarantees
QoS 2 guarantees exactly once delivery, so duplicates should not occur in transmission.
Step 2: Identify cause of duplicates
If duplicates appear, likely the subscriber application is not filtering duplicates properly.
Final Answer:
The subscriber is not handling duplicate messages properly -> Option D
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
Step 1: Analyze requirements for reliability and no duplicates
The system needs reliable delivery and no duplicate messages despite unstable network.
Step 2: Match QoS levels to requirements
QoS 0 is fastest but unreliable, QoS 1 may duplicate, QoS 2 guarantees exactly once delivery.
Step 3: Confirm Raspberry Pi supports QoS 2
MQTT clients on Raspberry Pi support QoS 2, so it is suitable.
Final Answer:
QoS 2, to guarantee exactly once delivery with no duplicates -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.