0
0
Raspberry Piprogramming~20 mins

Publishing sensor data in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sensor Data Publisher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this sensor data publishing code?

Consider this Python code running on a Raspberry Pi that reads a temperature sensor and publishes the data to a server. What will be printed when the code runs?

Raspberry Pi
import time

def read_temperature():
    return 25.5

def publish_data(temp):
    print(f"Publishing temperature: {temp}°C")

for _ in range(2):
    temp = read_temperature()
    publish_data(temp)
    time.sleep(1)
APublishing temperature: 25.5°C
B
Publishing temperature: 25.5°C
Publishing temperature: 25.5°C
CError: time.sleep is not defined
D
Publishing temperature: 25°C
Publishing temperature: 25°C
Attempts:
2 left
💡 Hint

Look at the loop and how many times the print happens.

🧠 Conceptual
intermediate
1:30remaining
Which protocol is commonly used to publish sensor data from Raspberry Pi?

When sending sensor data from a Raspberry Pi to a cloud server, which protocol is most commonly used for lightweight, reliable messaging?

AMQTT
BHTTP
CFTP
DSMTP
Attempts:
2 left
💡 Hint

Think about a protocol designed for small devices and low bandwidth.

🔧 Debug
advanced
2:30remaining
What error does this sensor publishing code raise?

Analyze the following code snippet that tries to publish sensor data. What error will it raise when run?

Raspberry Pi
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("broker.hivemq.com", 1883)

sensor_value = 42
client.publish("sensor/data", sensor_value)
client.disconnect()

print("Data published")
AAttributeError: 'Client' object has no attribute 'publish'
BConnectionRefusedError
CNo error, prints 'Data published'
DTypeError: a bytes-like object is required, not 'int'
Attempts:
2 left
💡 Hint

Check the data type expected by the publish method.

📝 Syntax
advanced
2:00remaining
Which option correctly publishes JSON sensor data using MQTT?

Given a Raspberry Pi sensor reading, which code snippet correctly publishes the data as JSON to an MQTT topic?

Raspberry Pi
import json
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("broker.hivemq.com", 1883)

sensor_data = {"temperature": 22.5, "humidity": 60}

# Publish code here

client.disconnect()
Aclient.publish("sensor/data", json.dumps(sensor_data))
Bclient.publish("sensor/data", str(sensor_data))
Cclient.publish("sensor/data", sensor_data)
Dclient.publish("sensor/data", json.load(sensor_data))
Attempts:
2 left
💡 Hint

Think about how to convert a dictionary to a string format suitable for transmission.

🚀 Application
expert
2:00remaining
How many messages are published by this Raspberry Pi sensor script?

Consider this Python script that reads sensor data and publishes it every 2 seconds for 5 iterations. How many MQTT publish messages will be sent?

Raspberry Pi
import time
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("broker.hivemq.com", 1883)

for i in range(5):
    temp = 20 + i
    client.publish("sensor/temperature", str(temp))
    time.sleep(2)

client.disconnect()
A0
B10
C1
D5
Attempts:
2 left
💡 Hint

Count how many times the loop runs and publishes.