Bird
0
0

Identify the error in this MQTT client setup code on a Raspberry Pi:

medium📝 Debug Q14 of 15
Raspberry Pi - MQTT for IoT

Identify the error in this MQTT client setup code on a Raspberry Pi:

client = mqtt.Client()
client.connect("broker.local")
client.subscribe("home/kitchen/temperature")
client.loop_forever()

# Missing on_message callback assignment
AThe on_message callback is not assigned, so messages won't be processed.
BThe client ID is missing in mqtt.Client() constructor.
CThe subscribe topic is invalid syntax.
Dloop_forever() should be called before connect().
Step-by-Step Solution
Solution:
  1. Step 1: Check callback assignment

    Without assigning client.on_message, received messages have no handler and won't be processed.
  2. Step 2: Verify other parts

    Client ID is optional but recommended; subscribe syntax is correct; loop_forever() must be after connect().
  3. Final Answer:

    The on_message callback is not assigned, so messages won't be processed. -> Option A
  4. Quick Check:

    Missing on_message means no message handling [OK]
Quick Trick: Always assign on_message callback to process messages [OK]
Common Mistakes:
MISTAKES
  • Ignoring on_message callback assignment
  • Thinking client ID is mandatory
  • Calling loop_forever() before connect()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes