Bird
0
0

Which code snippet correctly subscribes to all three topics using paho-mqtt?

hard🚀 Application Q8 of 15
Raspberry Pi - MQTT for IoT
You want to create a Raspberry Pi program that subscribes to multiple topics: 'home/kitchen', 'home/livingroom', and 'home/bedroom'. Which code snippet correctly subscribes to all three topics using paho-mqtt?
Aclient.subscribe('home/kitchen,home/livingroom,home/bedroom')
Bclient.subscribe([('home/kitchen', 0), ('home/livingroom', 0), ('home/bedroom', 0)])
Cclient.subscribe('home/kitchen', 'home/livingroom', 'home/bedroom')
Dclient.subscribe(['home/kitchen', 'home/livingroom', 'home/bedroom'])
Step-by-Step Solution
Solution:
  1. Step 1: Understand multi-topic subscription syntax

    paho-mqtt accepts a list of tuples (topic, qos) to subscribe to multiple topics.
  2. Step 2: Evaluate options

    client.subscribe([('home/kitchen', 0), ('home/livingroom', 0), ('home/bedroom', 0)]) uses correct list of tuples; A and B have wrong argument formats; D uses list of strings without qos.
  3. Final Answer:

    client.subscribe([('home/kitchen', 0), ('home/livingroom', 0), ('home/bedroom', 0)]) -> Option B
  4. Quick Check:

    Subscribe multiple topics = C [OK]
Quick Trick: Use list of (topic, qos) tuples to subscribe multiple topics [OK]
Common Mistakes:
MISTAKES
  • Passing multiple strings as separate arguments
  • Passing comma-separated string of topics
  • Passing list of strings without qos

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes