Azure IoT Hub Documentation
Your comprehensive guide to building and managing IoT solutions on Azure.
Introduction to Azure IoT Hub
Azure IoT Hub is a fully managed service that enables you to connect, monitor, and manage billions of IoT devices. It provides robust security, bi-directional communication, and device management capabilities, making it the cornerstone of any Azure-based IoT solution.
With IoT Hub, you can:
- Ingest telemetry data from devices at scale.
- Send commands and configurations to devices.
- Manage device identities and credentials securely.
- Integrate with other Azure services for analytics, storage, and machine learning.
Getting Started with IoT Hub
Begin your IoT journey with these essential steps:
- Create an IoT Hub: Provision an IoT Hub instance in your Azure subscription through the Azure portal, Azure CLI, or Azure PowerShell.
- Register a Device: Create a unique identity for each device that will connect to your IoT Hub. This involves generating device IDs and optionally keys.
- Connect Your Device: Use one of the Azure IoT SDKs to establish a secure connection from your device to the IoT Hub.
- Send Data: Start sending telemetry data from your devices to IoT Hub.
For detailed instructions, refer to the Register a Device tutorial.
Core Concepts
Understanding these fundamental concepts is key to effectively using Azure IoT Hub:
Device Identity Registry
The device identity registry securely stores information about each device that is allowed to connect to your IoT Hub. Each entry includes a unique device ID, authentication credentials (symmetric keys or X.509 certificates), and device twin properties.
Device Twin
A device twin is a JSON document that represents the state of a device. It contains:
- Tags: Metadata about the device, such as location or device type, which can be used for querying.
- Properties:
- Desired Properties: Used by the cloud application to set desired state (e.g., desired temperature).
- Reported Properties: Used by the device to report its current state (e.g., actual temperature).
Message Routing
IoT Hub can route device-to-cloud messages to different endpoints based on message content and device twin properties. This allows for flexible data processing and integration with various Azure services like Azure Storage, Azure Service Bus, and Azure Event Hubs.
Key Features
Device Provisioning
IoT Hub supports various device provisioning methods, including manual registration, bulk creation, and automatic provisioning with Azure IoT Hub Device Provisioning Service (DPS) for at-scale deployments.
Device Management
Manage your devices throughout their lifecycle. This includes monitoring device connectivity, updating device twins, performing device reboots, and deploying firmware updates.
Communication Patterns
IoT Hub supports several communication patterns:
- Device-to-Cloud (D2C) Telemetry: Devices send sensor data, events, and other operational data to the cloud.
- Cloud-to-Device (C2D) Commands: The cloud sends commands or messages to individual devices.
- Direct Methods: Cloud invokes a method on a device, and the device responds synchronously.
- Device Twin Updates: Devices report their state (reported properties) and the cloud can set desired states (desired properties).
Security
Security is paramount in IoT. IoT Hub provides:
- Identity Management: Secure device authentication using SAS tokens or X.509 certificates.
- Transport Encryption: TLS encryption for all device communication.
- Access Control: Fine-grained permissions for cloud applications interacting with IoT Hub.
Tutorials & Guides
How to Register a Device
Learn how to register a new device identity in Azure IoT Hub.
# Example using Azure CLI
az iot hub device-identity create --hub-name MyIoTHub --device-id my-first-device
This command creates a new device identity named my-first-device in your IoT Hub named MyIoTHub.
Sending Telemetry Data
This guide covers sending basic telemetry messages from a simulated device.
You can use the Azure IoT SDKs (e.g., Python, C#, Node.js) to send messages. Here's a conceptual Python example:
from azure.iot.device import IoTHubDeviceClient, Message
# Connection string is obtained from the device identity in IoT Hub
conn_str = "HostName=YOUR_HUB_NAME.azure-devices.net;DeviceId=YOUR_DEVICE_ID;SharedAccessKey=YOUR_KEY"
device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
async def send_telemetry_data():
message = Message("Hello, world!")
await device_client.send_message(message)
print("Message sent!")
# Call send_telemetry_data() in your application logic
Receiving Cloud-to-Device Commands
Devices can receive commands from the cloud. This involves implementing command handlers in your device application.
Conceptual example for handling direct methods:
from azure.iot.device.aio import IoTHubDeviceClient
# ... connection string setup ...
async def command_handler(command_name, payload):
print(f"Received command: {command_name} with payload: {payload}")
# Process the command and return a result
return {"response": "Command processed successfully"}
# Register the command handler
device_client.on_method_request_received = command_handler
# ... rest of the device client setup ...
API Reference
REST API
Azure IoT Hub exposes a rich REST API for managing devices, invoking methods, and interacting with device twins. You can use this API to build custom management applications or integrate with other services.
For detailed information, please refer to the official IoT Hub REST API documentation.
SDK Reference
Azure provides Software Development Kits (SDKs) for various languages, simplifying device and backend development.
- Azure IoT Device SDKs: For connecting and managing devices (C, C++, Python, Java, Node.js, .NET).
- Azure IoT Service SDKs: For building cloud-side applications that interact with IoT Hub (Python, Java, Node.js, .NET).
Find SDKs and quickstarts here: Azure IoT SDKs.