0
0
Raspberry Piprogramming~20 mins

Multi-device MQTT network in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MQTT Mastery on Raspberry Pi
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AConnected successfully
BConnection failed with code 1
CConnection failed with code 5
DNo output, program hangs
Attempts:
2 left
💡 Hint
Look at the on_connect callback and the return code rc.
Predict Output
intermediate
2: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'
AReceived message: b'Hello'
BReceived message: Hello
CReceived message: None
DNo output printed
Attempts:
2 left
💡 Hint
Check how the payload is decoded before printing.
🔧 Debug
advanced
2: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()
ANo error, message sent successfully
BRuntimeError: start_loop() called twice
CAttributeError: 'Client' object has no attribute 'publish'
DMessage not sent, but no error raised
Attempts:
2 left
💡 Hint
Consider how MQTT client processes network events.
📝 Syntax
advanced
2:00remaining
Identify Syntax Error in MQTT Callback
Which option contains a syntax error in defining the MQTT on_connect callback function?
A
def on_connect(client, userdata, flags, rc):
    print("Connected")
    return None
B
def on_connect(client, userdata, flags, rc):
    print("Connected")
C
def on_connect(client, userdata, flags, rc)
    print("Connected")
D
)"detcennoC"(tnirp    
:)cr ,sgalf ,atadresu ,tneilc(tcennoc_no fed
Attempts:
2 left
💡 Hint
Check function definition syntax carefully.
🚀 Application
expert
3: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'?
Asensors/room1/#
Broom1/sensors/+
Csensors/+/room1
D#/sensors/room1
Attempts:
2 left
💡 Hint
Remember that '#' matches any number of levels and '+' matches exactly one level.