0
0
Raspberry Piprogramming~20 mins

paho-mqtt library usage in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MQTT Mastery Badge
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 client connection code?

Consider this Python code using paho-mqtt to connect to a broker and print connection status.

What will be printed when the client successfully connects?

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()
client.on_connect = on_connect
client.connect("test.mosquitto.org", 1883, 60)
client.loop_start()
AConnected successfully
BConnection failed with code 1
CNo output
DSyntaxError
Attempts:
2 left
💡 Hint

Check the meaning of rc == 0 in the on_connect callback.

Predict Output
intermediate
2:00remaining
What message does this MQTT subscriber print when a message arrives?

This code subscribes to topic home/temperature and prints the message payload.

What will be printed if a message with payload 25.5 arrives?

Raspberry Pi
import paho.mqtt.client as mqtt

def on_message(client, userdata, msg):
    print(f"Received message: {msg.payload.decode()}")

client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("home/temperature")
client.loop_start()
AReceived message: 25.5
BReceived message: b'25.5'
CReceived message: None
DTypeError
Attempts:
2 left
💡 Hint

Remember to decode the message payload from bytes to string.

🔧 Debug
advanced
2:00remaining
Why does this MQTT client fail to receive messages after subscribing?

Look at this code snippet. The client subscribes but never receives messages. What is the problem?

Raspberry Pi
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("home/livingroom")
# Missing loop_start or loop_forever call
AThe topic name is invalid and causes subscription failure.
BThe client never starts the network loop to process incoming messages.
CThe client.connect call is missing a callback function.
DThe client.subscribe call must be before connect.
Attempts:
2 left
💡 Hint

Check if the client processes network events after subscribing.

📝 Syntax
advanced
2:00remaining
Which option correctly publishes a message with QoS 1 using paho-mqtt?

Choose the code snippet that publishes the message "Hello" to topic test/topic with QoS level 1.

Aclient.publish("test/topic", "Hello", qos=2)
Bclient.publish(topic="test/topic", message="Hello", qos=1)
Cclient.publish("test/topic", payload="Hello", qos=1)
Dclient.publish("test/topic", payload="Hello", qos=3)
Attempts:
2 left
💡 Hint

Check the correct parameter names and valid QoS values (0, 1, or 2).

🚀 Application
expert
2:00remaining
How many messages will be received by this MQTT client after publishing?

This client subscribes to sensor/# and publishes to sensor/temperature. How many messages will the client receive?

Raspberry Pi
import paho.mqtt.client as mqtt

received = []

def on_message(client, userdata, msg):
    received.append(msg.topic)

client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("sensor/#")
client.loop_start()

client.publish("sensor/temperature", "22.5")

import time
time.sleep(1)  # wait for message

client.loop_stop()

print(len(received))
ARaises RuntimeError
B0
C2
D1
Attempts:
2 left
💡 Hint

Consider if the client receives its own published messages on subscribed topics.