We publish sensor data to share information from sensors with other devices or systems. This helps us monitor and react to changes in the environment.
0
0
Publishing sensor data in Raspberry Pi
Introduction
You want to send temperature readings from a Raspberry Pi to a cloud server.
You need to share humidity data with a mobile app in real-time.
You want to log sensor data remotely for later analysis.
You want to trigger alerts when sensor values cross a limit.
You want to connect multiple sensors and send their data to a dashboard.
Syntax
Raspberry Pi
import paho.mqtt.client as mqtt import time # Setup MQTT client client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Function to publish sensor data def publish_sensor_data(topic, data): client.publish(topic, data) # Example usage sensor_topic = "home/room1/temperature" sensor_value = "23.5" publish_sensor_data(sensor_topic, sensor_value) # Disconnect after publishing client.disconnect()
This example uses MQTT protocol to publish data.
You need an MQTT broker to send data through.
Examples
Simple publish of temperature data to a topic.
Raspberry Pi
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Publish temperature data client.publish("home/room1/temperature", "22.0") client.disconnect()
Publishing empty data to check how receiver handles it.
Raspberry Pi
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Publish empty data (edge case) client.publish("home/room1/temperature", "") client.disconnect()
Publishing data with degree symbol to show encoding support.
Raspberry Pi
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Publish data with special characters client.publish("home/room1/temperature", "23.5°C") client.disconnect()
Sample Program
This program connects to an MQTT broker, publishes three temperature readings one by one, and then disconnects.
Raspberry Pi
import paho.mqtt.client as mqtt import time # Connect to MQTT broker client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Simulate sensor data sensor_topic = "home/room1/temperature" sensor_values = [22.5, 22.7, 22.9] print("Publishing sensor data:") for sensor_value in sensor_values: print(f"Publishing {sensor_value} to topic {sensor_topic}") client.publish(sensor_topic, str(sensor_value)) time.sleep(1) # wait 1 second between publishes client.disconnect() print("Disconnected from broker.")
OutputSuccess
Important Notes
Publishing sensor data usually takes O(1) time per message.
Memory use is small, mostly for the message and connection.
Common mistake: forgetting to connect or disconnect the MQTT client.
Use publishing when you want to send data out; use subscribing when you want to receive data.
Summary
Publishing sensor data shares sensor readings with other devices.
Use MQTT protocol to send data easily from Raspberry Pi.
Always connect before publishing and disconnect after.