Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the MQTT client from the paho-mqtt library.
Raspberry Pi
from paho.mqtt.client import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'client' instead of 'Client'.
Trying to import 'mqtt_client' which does not exist.
✗ Incorrect
The correct import is Client with a capital C from paho.mqtt.client.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
mqtt.Client without importing mqtt.Calling
Client() twice.✗ Incorrect
You create a client by calling Client() after importing it.
3fill in blank
hardFix the error in the code to connect the client to the broker at 'localhost' on port 1883.
Raspberry Pi
client.[1]('localhost', 1883)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like
connect_to or connectBroker.Forgetting to call the method.
✗ Incorrect
The correct method to connect is connect.
4fill in blank
hardFill both blanks to subscribe the client to the topic 'home/temperature' with QoS level 1.
Raspberry Pi
client.[1]('home/temperature', [2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
publish instead of subscribe.Setting QoS to 0 instead of 1.
✗ Incorrect
You subscribe with subscribe and set QoS to 1.
5fill in blank
hardFill all three blanks to publish the message '23' to topic 'home/temperature' with QoS 0 and retain flag False.
Raspberry Pi
client.[1]('home/temperature', [2], qos=[3], retain=False)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
subscribe instead of publish.Passing message as a number instead of string.
Setting QoS to 1 or 2 instead of 0.
✗ Incorrect
Use publish to send a message. The message is '23' as a string, QoS is 0.