0
0
Raspberry Piprogramming~20 mins

Subscribing to control topics in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MQTT Control Topic Master
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 subscription callback?

Consider a Raspberry Pi subscribing to an MQTT topic home/lights/control. The callback prints the received message payload.

What will be printed when the message payload is ON?

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("broker.hivemq.com", 1883, 60)
client.subscribe("home/lights/control")

# Simulate receiving a message
class Msg:
    payload = b"ON"

on_message(client, None, Msg())
AReceived message: ON
BReceived message: b'ON'
CReceived message: None
DReceived message: on
Attempts:
2 left
💡 Hint

Remember that MQTT message payloads are bytes and need decoding.

🧠 Conceptual
intermediate
1:30remaining
Which MQTT topic subscription pattern matches all control topics under home?

You want your Raspberry Pi to subscribe to all control topics under home, such as home/lights/control and home/thermostat/control. Which subscription topic pattern should you use?

Ahome/#
Bhome/+/+
Chome/control/+
Dhome/+/control
Attempts:
2 left
💡 Hint

The plus sign + matches exactly one topic level.

🔧 Debug
advanced
2:30remaining
Why does this MQTT subscription callback not print messages?

The Raspberry Pi subscribes to home/lights/control but the callback never prints any message. What is the problem?

Raspberry Pi
import paho.mqtt.client as mqtt

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

client = mqtt.Client()
client.on_message = on_message
client.connect("broker.hivemq.com", 1883, 60)
client.subscribe("home/lights/control")

# Missing client.loop_forever() or client.loop_start() here
AThe client does not process network events because the loop is not started
BThe callback function is missing a return statement
CThe client.connect() call is missing the port number
DThe subscription topic is incorrect and does not match any published messages
Attempts:
2 left
💡 Hint

MQTT clients need to run a loop to receive messages.

📝 Syntax
advanced
2:00remaining
Which option correctly subscribes to multiple topics in one call?

You want to subscribe your Raspberry Pi MQTT client to home/lights/control and home/thermostat/control in one call. Which code is correct?

Aclient.subscribe("home/lights/control", "home/thermostat/control")
Bclient.subscribe([("home/lights/control", 0), ("home/thermostat/control", 0)])
Cclient.subscribe({"home/lights/control":0, "home/thermostat/control":0})
Dclient.subscribe("home/lights/control", qos=0); client.subscribe("home/thermostat/control", qos=0)
Attempts:
2 left
💡 Hint

Refer to the Paho MQTT subscribe method for multiple topics.

🚀 Application
expert
3:00remaining
How many messages will be received with this subscription pattern?

Your Raspberry Pi subscribes to home/+/control. The broker publishes messages to these topics:

  • home/lights/control
  • home/thermostat/control
  • home/lights/status
  • home/garden/control
  • home/lights/control/extra

How many messages will your client receive?

A2
B4
C3
D5
Attempts:
2 left
💡 Hint

The plus sign + matches exactly one topic level.