Challenge - 5 Problems
MQTT Mastery on Raspberry Pi
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
MQTT Client Connection Output
What will be the output when this Raspberry Pi MQTT client code runs and successfully connects to the broker?
Raspberry Pi
import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected successfully") else: print(f"Connection failed with code {rc}") client = mqtt.Client("client1") client.on_connect = on_connect client.connect("broker.hivemq.com", 1883, 60) client.loop_start()
Attempts:
2 left
💡 Hint
Look at the on_connect callback and the return code rc.
✗ Incorrect
The on_connect callback prints 'Connected successfully' only if rc is 0, which means the connection succeeded.
❓ Predict Output
intermediate2:00remaining
MQTT Message Reception
What will this Raspberry Pi MQTT subscriber print when it receives a message with payload 'Hello' on topic 'test/topic'?
Raspberry Pi
import paho.mqtt.client as mqtt def on_message(client, userdata, msg): print(f"Received message: {msg.payload.decode()}") client = mqtt.Client("client2") client.on_message = on_message client.connect("broker.hivemq.com", 1883, 60) client.subscribe("test/topic") client.loop_start() # Simulate receiving a message with payload 'Hello' on 'test/topic'
Attempts:
2 left
💡 Hint
Check how the payload is decoded before printing.
✗ Incorrect
The payload is bytes, decoded to string with decode(), so output is 'Received message: Hello'.
🔧 Debug
advanced2:00remaining
MQTT Publish Failure Debugging
This Raspberry Pi MQTT publisher code does not send messages as expected. What error will it raise?
Raspberry Pi
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) client.publish("test/topic", "Hello") client.disconnect() # Missing client.loop_start() or client.loop_forever()
Attempts:
2 left
💡 Hint
Consider how MQTT client processes network events.
✗ Incorrect
Without calling loop_start() or loop_forever(), the client does not process network events, so the message is not sent but no error occurs.
📝 Syntax
advanced2:00remaining
Identify Syntax Error in MQTT Callback
Which option contains a syntax error in defining the MQTT on_connect callback function?
Attempts:
2 left
💡 Hint
Check function definition syntax carefully.
✗ Incorrect
Option C is missing the colon ':' at the end of the function definition line, causing a syntax error.
🚀 Application
expert3:00remaining
Multi-device MQTT Network Topic Filtering
In a multi-device MQTT network, which topic subscription will allow a Raspberry Pi device to receive messages only from devices in the 'sensors' group located in 'room1'?
Attempts:
2 left
💡 Hint
Remember that '#' matches any number of levels and '+' matches exactly one level.
✗ Incorrect
The subscription 'sensors/room1/#' matches all topics starting with 'sensors/room1/', so it receives messages from all devices in 'sensors' group in 'room1'.