0
0
Raspberry Piprogramming~10 mins

Publishing sensor data 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] as mqtt
Drag options to blanks, or click blank then click option'
Atime
Brandom
Cpaho.mqtt.client
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated libraries like random or os.
Forgetting to import the MQTT client library.
2fill in blank
medium

Complete the code to connect the MQTT client to the broker at 'localhost'.

Raspberry Pi
client = mqtt.Client()
client.[1]('localhost')
Drag options to blanks, or click blank then click option'
Aconnect
Bloop_start
Csubscribe
Dpublish
Attempts:
3 left
💡 Hint
Common Mistakes
Using publish instead of connect.
Calling subscribe before connecting.
3fill in blank
hard

Fix the error in publishing the sensor data to topic 'sensor/temperature'.

Raspberry Pi
temperature = 23.5
client.publish([1], str(temperature))
Drag options to blanks, or click blank then click option'
A'temperature/sensor'
Bsensor/temperature
C'sensor.temperature'
D'sensor/temperature'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the topic string.
Using dots instead of slashes in topic names.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps sensor names to their values if the value is above 20.

Raspberry Pi
sensor_data = {'temp': 22, 'humidity': 18, 'pressure': 25}
filtered = {k: v for k, v in sensor_data.items() if v [1] 20 and k [2] 'humidity'}
Drag options to blanks, or click blank then click option'
A>
B!=
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for value comparison.
Using '==' instead of '!=' to exclude 'humidity'.
5fill in blank
hard

Fill all three blanks to create a loop that publishes each sensor's data as a string to the topic 'sensor/{sensor_name}'.

Raspberry Pi
for [1], [2] in sensor_data.items():
    topic = f'sensor/[3]'
    client.publish(topic, str([2]))
Drag options to blanks, or click blank then click option'
Asensor
Bvalue
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both key and value.
Not using the sensor name in the topic string.