Bird
0
0

Identify the error in this MQTT subscription code snippet for Raspberry Pi:

medium📝 Debug Q14 of 15
Raspberry Pi - MQTT for IoT
Identify the error in this MQTT subscription code snippet for Raspberry Pi:
def on_message(client, userdata, message):
    print(message.payload)

client.on_message = on_message
client.subscribe("home/lights/control")
client.loop_start()
AIncorrect callback function name
BMissing client.connect() before subscribing
CUsing loop_start() instead of loop_forever()
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check connection setup

    Before subscribing, the client must connect to the MQTT broker.
  2. Step 2: Identify missing connect call

    The code lacks client.connect() call, so subscription won't work.
  3. Final Answer:

    Missing client.connect() before subscribing -> Option B
  4. Quick Check:

    Must connect before subscribe [OK]
Quick Trick: Always connect client before subscribing [OK]
Common Mistakes:
MISTAKES
  • Skipping client.connect() call
  • Confusing callback function names
  • Thinking loop_start() is wrong here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes