📖 Scenario: You are working with MQTT, a messaging protocol used in IoT devices. Retained messages help new subscribers get the last known message immediately after subscribing.Imagine you have a smart home system where sensors send temperature updates. You want new devices to get the latest temperature right away without waiting for the next update.
🎯 Goal: Build a simple simulation of MQTT retained messages using a Python dictionary. You will store messages with a retained flag, update the retained messages, and retrieve the last retained message for a topic.
📋 What You'll Learn
Create a dictionary to store MQTT topics and their retained messages
Add a variable to represent a new message with a retained flag
Write logic to update the retained message only if the retained flag is true
Print the retained message for a specific topic
💡 Why This Matters
🌍 Real World
MQTT retained messages are used in IoT devices to ensure new subscribers get the latest state immediately, such as the last temperature reading or device status.
💼 Career
Understanding retained messages is important for IoT developers and DevOps engineers managing MQTT brokers and device communication to ensure reliable and efficient data delivery.
Progress0 / 4 steps
1
Create the initial retained messages dictionary
Create a dictionary called retained_messages with these exact entries: 'home/temperature': '22C', 'home/humidity': '45%'
IOT Protocols
Hint
Use curly braces to create a dictionary with keys as topic strings and values as message strings.
2
Add a new MQTT message with retained flag
Create a variable called new_message as a dictionary with keys 'topic', 'payload', and 'retained'. Set 'topic' to 'home/temperature', 'payload' to '23C', and 'retained' to true.
IOT Protocols
Hint
Create a dictionary with the exact keys and values as specified.
3
Update retained messages if retained flag is true
Write an if statement that checks if new_message['retained'] is true. If yes, update retained_messages for the key new_message['topic'] with the value new_message['payload'].
IOT Protocols
Hint
Use an if statement to check the retained flag and update the dictionary accordingly.
4
Print the retained message for 'home/temperature'
Write a print statement to display the retained message for the topic 'home/temperature' from the retained_messages dictionary.
IOT Protocols
Hint
Use print() with the dictionary key to show the updated retained message.
Practice
(1/5)
1. What is the main purpose of a retained message in MQTT?
easy
A. To delete all messages from the broker
B. To store the last message on a topic for new subscribers
C. To encrypt messages during transmission
D. To limit the message size to 256 bytes
Solution
Step 1: Understand retained message concept
Retained messages keep the last message sent on a topic stored on the broker.
Step 2: Identify the purpose for new subscribers
New subscribers receive the last retained message immediately upon subscribing, ensuring they have the latest state.
Final Answer:
To store the last message on a topic for new subscribers -> Option B
Quick Check:
Retained message = last stored message [OK]
Hint: Retained means broker keeps last message for new subscribers [OK]
Common Mistakes:
Confusing retained messages with message encryption
Thinking retained messages delete old messages
Assuming retained messages limit message size
2. Which MQTT publish option must be set to true to create a retained message?
easy
A. retain
B. qos
C. cleanSession
D. willFlag
Solution
Step 1: Identify publish options related to message retention
The retain flag controls whether the broker stores the message as retained.
Step 2: Confirm correct flag for retention
Setting retain=true tells the broker to keep the message for future subscribers.
Final Answer:
retain -> Option A
Quick Check:
retain flag true = retained message [OK]
Hint: Set retain flag true to keep message on broker [OK]
Common Mistakes:
Confusing retain with cleanSession flag
Using qos instead of retain for message storage
Mistaking willFlag for retention control
3. Given the following MQTT publish code snippet, what will a new subscriber receive immediately after subscribing to topic home/temperature?
B. The message '23°C' because it was the last published
C. No message because retain is false on last publish
D. Both messages '22°C' and '23°C'
Solution
Step 1: Analyze retained flag on each publish
The first message '22°C' is published with retain=True, so it is stored on the broker.
Step 2: Understand effect of second publish with retain=False
The second message '23°C' is published without retain, so it is sent but not stored as retained. Importantly, publishing with retain=False on the same topic clears the retained message.
Step 3: Determine what new subscribers receive
New subscribers get no retained message because the last retained message was cleared by the second publish.
Final Answer:
No message because retain is false on last publish -> Option C
Quick Check:
Retain=False on last publish clears retained message [OK]
Hint: Retain=False on last publish clears retained message [OK]
Common Mistakes:
Assuming last published message is always retained
4. You want to clear a retained message on topic device/status. Which publish command will correctly clear it?
medium
A. client.publish('device/status', '', retain=True)
B. client.publish('device/status', '', retain=False)
C. client.publish('device/status', None, retain=True)
D. client.publish('device/status', None, retain=False)
Solution
Step 1: Understand how to clear retained messages
Publishing an empty payload with retain=True clears the retained message on that topic.
Step 2: Analyze the options
Publishing with retain=False does not update or clear the retained message. Using None as payload is invalid. Only publishing an empty string ('') with retain=True clears the retained message.
Final Answer:
client.publish('device/status', '', retain=True) -> Option A
Hint: Publish empty message with retain=True to clear retained message [OK]
Common Mistakes:
Using retain=False when trying to clear retained message
Sending None as payload which causes error
Expecting retain=False to remove retained message
5. You have multiple IoT devices publishing their status to devices/+/status with retained messages. A new device subscribes to devices/device123/status. What will it receive and why?
hard
A. It receives only non-retained messages published after subscription
B. It receives all retained messages from all devices under devices/+/status
C. It receives no messages until a new status is published
D. It receives the last retained status message for device123 immediately
Solution
Step 1: Understand MQTT wildcard subscription
The subscription to devices/device123/status matches only that specific topic, not all devices.
Step 2: Understand retained message delivery
The broker sends the last retained message on that exact topic immediately upon subscription.
Step 3: Determine what the new device receives
The new device receives the last retained status message for device123 immediately, not messages from other devices.
Final Answer:
It receives the last retained status message for device123 immediately -> Option D
Quick Check:
Retained message sent on exact subscribed topic [OK]
Hint: New subscriber gets last retained message on exact topic [OK]
Common Mistakes:
Thinking wildcard subscription delivers all retained messages
Expecting no retained messages on subscription
Confusing retained with non-retained message delivery
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.