This project demonstrates an IoT edge experiment where an HC-SR04 Ultrasonic Sensor is connected to a Raspberry Pi 5 to stream real-time data to Azure IoT Hub. The focus is on seamless integration between edge devices and cloud platforms for efficient data management and analytics.
Sensor π₯Ώ β Raspberry Pi 5 β‘ β Python + gpiozero, Azure IoT SDK π» β Azure IoT Hub π β Cloud Analytics π
- Hardware Setup:
- Raspberry Pi 5
- HC-SR04 Ultrasonic Sensor
- Software:
- Python
- Libraries:
gpiozero
for sensor interfacing and Azure IoT SDK for cloud communication
- Cloud Integration:
- Azure IoT Hub for data management, analytics, and automation
- Connect the HC-SR04 Ultrasonic Sensor to the GPIO pins on the Raspberry Pi 5:
- Trigger Pin: GPIO 23
- Echo Pin: GPIO 24
- Power the sensor using the Raspberry Piβs 5V and GND pins.
- Python 3.x
- Libraries:
gpiozero
azure.iot.device
Install required libraries using pip:
pip install gpiozero azure-iot-device
The Python program interfaces with the HC-SR04 sensor to measure distance and sends the data to Azure IoT Hub.
from gpiozero import DistanceSensor
from azure.iot.device import IoTHubDeviceClient, Message
import time
# Azure IoT Hub connection string
CONNECTION_STRING = "YOUR CONNECTION STRING"
# Create the IoT Hub device client
device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
# Initialize the sensor on GPIO 23 and 24
sensor = DistanceSensor(echo=24, trigger=23)
print("Program started")
def send_data_to_iot_hub(distance):
message = Message(f"{{'distance': {distance}}}") # Create the telemetry message
device_client.send_message(message) # Send the message to Azure IoT Hub
print(f"Sent message: {message}")
try:
while True:
dist = sensor.distance * 100 # Convert distance to centimeters
print(f"Measured Distance: {dist:.2f} cm")
# Send the measured distance to IoT Hub
send_data_to_iot_hub(dist)
time.sleep(1)
except KeyboardInterrupt:
print("Measurement stopped by User")
finally:
device_client.shutdown() # Close the connection when done
- Sensor Interfacing:
- The
gpiozero
library is used to interact with the HC-SR04 sensor. It measures the distance in centimeters.
- The
- Cloud Communication:
- The Azure IoT SDK enables secure communication between the Raspberry Pi 5 and Azure IoT Hub.
- The measured distance is packaged as a telemetry message and sent to the IoT Hub.
- Data Management:
- The IoT Hub processes the incoming data, which can be visualized and analyzed in Azure cloud services.
Use the Azure CLI to monitor the data sent to the Azure IoT Hub. Run the following commands:
- Add the Azure IoT extension if not already installed:
az extension add --name azure-iot
- Monitor events from your IoT device:
az iot hub monitor-events --hub-name YourIotHubName --device-id YourDeviceId
Replace YourIotHubName
with the name of your Azure IoT Hub and YourDeviceId
with the ID of your IoT device.
- Real-time measurement of distance displayed on the terminal.
- Telemetry data sent to Azure IoT Hub.
Example output:
Program started
Measured Distance: 12.34 cm
Sent message: {'distance': 12.34}
Measured Distance: 15.67 cm
Sent message: {'distance': 15.67}
This project is licensed under the MIT License. Feel free to use and adapt it for your needs!