Challenge - 5 Problems
MQTT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this MQTT message publishing code?
Consider this Python code running on a Raspberry Pi that publishes a message to an MQTT broker. What will be printed when the message is successfully published?
Raspberry Pi
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect('test.mosquitto.org', 1883, 60) result = client.publish('home/temperature', '22.5') print(result.rc)
Attempts:
2 left
💡 Hint
The publish() method returns an MQTTMessageInfo object with an rc attribute. 0 means success.
✗ Incorrect
In MQTT, a result code of 0 means the message was accepted for delivery by the client library.
🧠 Conceptual
intermediate1:30remaining
Why is MQTT preferred for IoT devices?
Which of the following reasons best explains why MQTT is widely used as the IoT communication standard?
Attempts:
2 left
💡 Hint
Think about IoT devices often having limited power and network.
✗ Incorrect
MQTT uses a lightweight publish-subscribe model that minimizes bandwidth and power consumption, ideal for IoT.
🔧 Debug
advanced2:30remaining
Identify the error in this MQTT subscriber code
This Python code is intended to subscribe to an MQTT topic and print received messages. What error will it raise when run?
Raspberry Pi
import paho.mqtt.client as mqtt def on_message(client, userdata, message): print(f"Received message: {message.payload.decode()}") client = mqtt.Client() client.on_message = on_message client.connect('broker.hivemq.com', 1883) client.subscribe('home/temperature') client.loop_start() # Missing loop or wait here
Attempts:
2 left
💡 Hint
Think about what happens after starting the loop in a script that ends immediately.
✗ Incorrect
The script starts the network loop in the background but then exits immediately, so no messages are received or printed.
📝 Syntax
advanced1:30remaining
Which MQTT topic filter is valid for subscribing to all sensors?
You want to subscribe to all topics under 'sensors' using MQTT wildcards. Which subscription topic string is syntactically correct?
Attempts:
2 left
💡 Hint
MQTT uses '#' for multi-level and '+' for single-level wildcards.
✗ Incorrect
The '#' wildcard must be at the end and matches all subtopics. '+' matches exactly one level. '*' is not valid in MQTT.
🚀 Application
expert2:00remaining
How many messages will be received by this MQTT subscriber?
A subscriber subscribes to 'devices/+/status' and the broker receives messages published to these topics:
- devices/device1/status
- devices/device2/status
- devices/device1/temperature
- devices/device3/status
- devices/device3/battery
How many messages will the subscriber receive?
Attempts:
2 left
💡 Hint
The '+' wildcard matches exactly one topic level.
✗ Incorrect
The subscription matches topics with exactly one level between 'devices' and 'status'. Only three topics match: device1/status, device2/status, device3/status.