Scroll to top
© 2024, Global Digital Services LLC.

Deploying IoT Solutions with Azure IoT Hub


Fernando Noguera - December 17, 2024 - 0 comments

Azure IoT Hub provides a cloud-hosted solution back end to connect virtually any device. Extend your solution from the cloud to the edge with per-device authentication, built-in device management, and scaled provisioning. Security-enhanced communication channel for sending and receiving data from IoT devices.

Let’s create a basic IoT sensor data collection system using Azure in Python, we’ll  integrate with Azure IoT Hub.

This example demonstrates how to collect data from a hypothetical temperature sensor and send it to Azure IoT Hub.

Step 1: Install Required Libraries

You’ll need the azure-iot-device package for interacting with Azure IoT Hub. Install it using pip:

pip install azure-iot-device

Step 2: Import Libraries and Set Up Azure IoT Hub Connection

Import necessary libraries and set up your connection string from Azure IoT Hub.

from azure.iot.device import IoTHubDeviceClient, Message
import time
import random

# Azure IoT Hub device connection string
CONNECTION_STRING = "Your_Azure_IoT_Hub_Device_Connection_String"

Step 3: Simulate Sensor Data

In this example, we’ll simulate temperature sensor data. In a real scenario, you’d read data from an actual sensor.

def get_simulated_sensor_data():
# Simulate temperature data
temperature = 20 + (random.random() * 15) # Random temperature between 20 and 35
return temperature

Step 4: Send Data to Azure IoT Hub

Create a function to send data to Azure IoT Hub. Data is sent as a JSON payload.

def send_data_to_iot_hub(client):
while True:
# Get sensor data
temperature = get_simulated_sensor_data()

# Create a JSON payload
message = Message(f'{{"temperature": {temperature}}}')

# Add properties to the message (optional)
message.content_encoding = "utf-8"
message.content_type = "application/json"

# Send the message
print(f"Sending message: {message}")
client.send_message(message)
print("Message successfully sent")

# Wait for a while before sending the next data
time.sleep(5)

Step 5: Main Function.

The main function initializes the device client and starts sending data.

def main():
try:
# Create an IoT Hub client
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)

# Send data to IoT Hub
send_data_to_iot_hub(client)

except KeyboardInterrupt:
print("IoT Hub data collection stopped")

if __name__ == "__main__":
main()

Note:

  • Replace "Your_Azure_IoT_Hub_Device_Connection_String" with your actual Azure IoT Hub device connection string.
  • In a real IoT scenario, you would replace the simulated data function with actual sensor reading code.
  • Ensure your device is registered with Azure IoT Hub and has the proper permissions.
  • This example sends data every 5 seconds. Adjust the frequency as needed for your use case.

The integration of IoT in business operations opens up a realm of possibilities for efficiency and innovation. As IoT continues to evolve, its role in driving business efficiency and growth becomes increasingly significant.

At GDS Consulting Services, we specialize in providing Azure infrastructure solutions. Feel free contact us for more information or visit our blog.

Related posts