Bird
Raised Fist0
Raspberry Piprogramming~10 mins

Publishing sensor data in Raspberry Pi - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of publishing sensor data from a Raspberry Pi?

easy
A. To share sensor readings with other devices or systems
B. To store data only on the Raspberry Pi without sharing
C. To turn off the sensor automatically
D. To update the Raspberry Pi operating system

Solution

  1. Step 1: Understand the concept of publishing sensor data

    Publishing sensor data means sending sensor readings to other devices or systems so they can use the information.
  2. Step 2: Identify the correct purpose

    Sharing data is the key reason, not storing locally or system updates.
  3. Final Answer:

    To share sensor readings with other devices or systems -> Option A
  4. Quick Check:

    Publishing = Sharing data [OK]
Hint: Publishing means sending data out to others [OK]
Common Mistakes:
  • Thinking publishing only stores data locally
  • Confusing publishing with system updates
  • Assuming publishing turns off sensors
2.

Which of the following is the correct way to publish a message using the paho-mqtt library in Python?

client = mqtt.Client()
client.connect('broker.hivemq.com', 1883)
# What next?
easy
A. client.push('sensor/data', '25.5')
B. client.send('sensor/data', '25.5')
C. client.publish('sensor/data', '25.5')
D. client.post('sensor/data', '25.5')

Solution

  1. Step 1: Recall the paho-mqtt publish method

    The correct method to send data is publish, not send, push, or post.
  2. Step 2: Match method with syntax

    Using client.publish(topic, message) is the right syntax.
  3. Final Answer:

    client.publish('sensor/data', '25.5') -> Option C
  4. Quick Check:

    Use publish() to send MQTT messages [OK]
Hint: Use client.publish() to send MQTT messages [OK]
Common Mistakes:
  • Using non-existent methods like send or push
  • Forgetting to call publish before disconnect
  • Mixing HTTP verbs with MQTT methods
3.

What will be the output of this Python code snippet on a Raspberry Pi publishing sensor data?

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect('broker.hivemq.com', 1883)
result = client.publish('sensor/temp', '22.4')
print(result.rc)
medium
A. 1
B. 0
C. None
D. Error

Solution

  1. Step 1: Understand the publish return value

    The publish method returns an object with a rc attribute indicating the result code. 0 means success.
  2. Step 2: Check the printed value

    Printing result.rc will show 0 if the publish succeeded.
  3. Final Answer:

    0 -> Option B
  4. Quick Check:

    MQTT publish success code = 0 [OK]
Hint: Check publish().rc for success (0 means success) [OK]
Common Mistakes:
  • Expecting publish() to return message content
  • Confusing rc with error codes
  • Not calling connect() before publish()
4.

Identify the error in this Raspberry Pi Python code that publishes sensor data:

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.publish('sensor/humidity', '55')
client.connect('broker.hivemq.com', 1883)
client.disconnect()
medium
A. Calling publish before connect
B. Missing import statement
C. Using wrong topic name
D. Not disconnecting after publish

Solution

  1. Step 1: Check the order of MQTT calls

    The client must connect to the broker before publishing messages.
  2. Step 2: Identify the incorrect sequence

    Here, publish is called before connect, which will cause an error.
  3. Final Answer:

    Calling publish before connect -> Option A
  4. Quick Check:

    Connect before publish [OK]
Hint: Always connect before publishing data [OK]
Common Mistakes:
  • Publishing before connecting
  • Forgetting to disconnect
  • Using invalid topic names
5.

You want to publish temperature and humidity data from a Raspberry Pi to two MQTT topics sensor/temp and sensor/humidity. Which code snippet correctly publishes both values and disconnects properly?

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect('broker.hivemq.com', 1883)
# Fill in the blanks
client.publish('sensor/temp', '23.1')
client.publish('sensor/humidity', '60')
client.disconnect()
hard
A. Must publish only one topic per connection
B. Missing client.loop_start() before publishing
C. Should call disconnect() before publishing
D. Correct as shown, publishes both then disconnects

Solution

  1. Step 1: Verify connection and publishing order

    Connect first, then publish each topic's data, then disconnect.
  2. Step 2: Check if loop_start() is required

    For simple publish, loop_start() is optional; synchronous publish works fine.
  3. Step 3: Confirm multiple publishes per connection

    Multiple publishes can be done before disconnecting.
  4. Final Answer:

    Correct as shown, publishes both then disconnects -> Option D
  5. Quick Check:

    Connect -> publish multiple -> disconnect [OK]
Hint: Publish multiple topics after connect, then disconnect [OK]
Common Mistakes:
  • Disconnecting before publishing
  • Thinking loop_start() is always needed
  • Believing only one publish per connection allowed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard