0
0
Raspberry Piprogramming~10 mins

Subscribing to control topics in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the MQTT client library.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Apaho.mqtt.client
Bmqtt.client
Cmqtt
Dpaho.mqtt
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'mqtt' or 'mqtt.client' which are not valid imports.
Forgetting the full path 'paho.mqtt.client'.
2fill in blank
medium

Complete the code to create a new MQTT client instance named 'client'.

Raspberry Pi
client = [1]()
Drag options to blanks, or click blank then click option'
Apaho.mqtt.Client
Bmqtt.Client
Cpaho.mqtt.client.Client
Dpaho.Client
Attempts:
3 left
💡 Hint
Common Mistakes
Using incomplete paths like 'mqtt.Client' or 'paho.Client'.
Forgetting to call the constructor with parentheses.
3fill in blank
hard

Fix the error in the code to subscribe to the topic 'control/led'.

Raspberry Pi
client.subscribe([1])
Drag options to blanks, or click blank then click option'
A"control/led"
Bcontrol/led
C'control/led'
Dcontrol_led
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the topic string.
Using an incorrect topic name like 'control_led'.
4fill in blank
hard

Fill both blanks to define a callback function that prints the received message payload.

Raspberry Pi
def on_message(client, userdata, [1]):
    print('Received:', [2].payload.decode())
Drag options to blanks, or click blank then click option'
Amsg
Bmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and inside the function.
Using undefined variable names.
5fill in blank
hard

Fill all three blanks to connect to the broker, set the message callback, and start the loop.

Raspberry Pi
client.connect([1])
client.[2] = on_message
client.[3]()
Drag options to blanks, or click blank then click option'
A'localhost'
Bon_message
Cloop_start
Dloop_forever
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong broker address.
Not assigning the callback correctly.
Using loop_forever() instead of loop_start() when asynchronous loop is needed.