Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to connect an MQTT client to a broker.
IOT Protocols
client.connect('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an IP address that is not reachable
Leaving the broker address empty
Using localhost when the broker is remote
✗ Incorrect
The code connects the MQTT client to the public broker at broker.hivemq.com.
2fill in blank
mediumComplete the code to subscribe to the topic 'home/temperature'.
IOT Protocols
client.subscribe('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Subscribing to the wrong topic
Using a topic with a typo
Using a topic from a different location
✗ Incorrect
Subscribing to 'home/temperature' allows the client to receive messages published to that topic.
3fill in blank
hardFix the error in publishing a message to 'home/light'.
IOT Protocols
client.publish('[1]', 'ON')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using underscores instead of slashes
Using dots instead of slashes
Adding extra topic levels unintentionally
✗ Incorrect
The correct topic format uses slashes, so 'home/light' is valid. Other options use different separators or wrong topic names.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps topics to their message lengths.
IOT Protocols
{topic: len([1]) for topic, [2] in messages.items()} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for both blanks
Using a variable not defined in the loop
Confusing topic and message variables
✗ Incorrect
The comprehension iterates over messages.items() with variables topic and msg. The length is calculated on msg.
5fill in blank
hardFill all three blanks to filter messages with payload length greater than 5 and create a new dictionary with uppercase topics.
IOT Protocols
{topic.upper(): [1] for topic, [2] in messages.items() if len([3]) > 5} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names between blanks
Using topic instead of message for length check
Not converting topic to uppercase
✗ Incorrect
The comprehension uses 'msg' as the dictionary value, 'msg' as the loop variable for message, and filters by length of 'msg'.