0
0
Raspberry Piprogramming~20 mins

Why MQTT is the IoT standard in Raspberry Pi - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MQTT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A1
B0
CNone
DException raised
Attempts:
2 left
💡 Hint
The publish() method returns an MQTTMessageInfo object with an rc attribute. 0 means success.
🧠 Conceptual
intermediate
1: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?
AIt requires devices to maintain constant TCP connections without keep-alive.
BIt uses a heavy protocol with large message sizes for reliability.
CIt supports a lightweight publish-subscribe model with low bandwidth use.
DIt only works with wired Ethernet connections.
Attempts:
2 left
💡 Hint
Think about IoT devices often having limited power and network.
🔧 Debug
advanced
2: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
ANo messages printed because program exits immediately
BRuntimeError because loop_start() is called without loop_forever()
CNo error, prints messages as they arrive
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint
Think about what happens after starting the loop in a script that ends immediately.
📝 Syntax
advanced
1: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?
Asensors/*
Bsensors/+/#
Csensors/+/+/#
Dsensors/#
Attempts:
2 left
💡 Hint
MQTT uses '#' for multi-level and '+' for single-level wildcards.
🚀 Application
expert
2: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?
A3
B5
C4
D2
Attempts:
2 left
💡 Hint
The '+' wildcard matches exactly one topic level.