Getting Started with Azure IoT Hub

Welcome to Azure IoT Hub! This guide will walk you through the essential steps to get your first IoT device connected and communicating with the cloud.

1. Understand the Basics

Before diving in, it's helpful to grasp a few key concepts:

  • IoT Hub: A fully managed service that enables you to connect, monitor, and manage billions of IoT devices.
  • Device Identity: Each device needs a unique identity registered in IoT Hub to authenticate and authorize communication.
  • Device Twin: A cloud representation of your device, containing desired properties and reported properties.
  • Message Routing: Configure how messages from your devices are routed to various Azure services.

What is IoT?

Learn about the Internet of Things and its potential applications across industries.

Learn about IoT

2. Create an Azure IoT Hub

The first step is to provision an IoT Hub instance in your Azure subscription. You can do this through the Azure portal, Azure CLI, or Azure PowerShell.

Using the Azure Portal:

  1. Sign in to the Azure portal.
  2. Search for "IoT Hub" and select it.
  3. Click "Create".
  4. Fill in the required details: subscription, resource group, region, and a unique IoT hub name.
  5. Choose a pricing tier (e.g., F1 for free tier, S1 for standard).
  6. Click "Review + create" and then "Create".

Using Azure CLI:


az iot hub create --name MyIoTHub --resource-group MyResourceGroup --location eastus --sku S1 --unit 1
                

Azure Portal Quickstart

Follow this interactive guide to create your IoT Hub step-by-step.

Create Hub in Portal

3. Register a Device

Once your IoT Hub is created, you need to register a device to allow it to connect.

Using the Azure Portal:

  1. Navigate to your IoT Hub resource in the Azure portal.
  2. Under "Explorers", select "Devices".
  3. Click "Add Device".
  4. Provide a unique Device ID (e.g., my-first-device).
  5. Choose authentication type (Symmetric key is common for getting started).
  6. Click "Save".

After saving, you will see the device details, including the primary and secondary connection strings. Keep these connection strings secure!

Using Azure CLI:


az iot hub device-identity create --hub-name MyIoTHub --device-id my-first-device --auth-method sim_key
                

Device Registration Details

Explore different authentication methods and best practices for device registration.

Device Authentication

4. Connect Your Device

Now you can use an IoT device (or an emulator) and an SDK to connect to your IoT Hub.

Azure provides SDKs for various languages like C, Python, Node.js, and .NET. You'll need the device's connection string obtained during registration.

Example with Azure IoT SDK for Python:

Install the SDK:


pip install azure-iot-device
                

A basic sender script:


from azure.iot.device import IoTHubDeviceClient, Message

# Replace with your actual device connection string
CONNECTION_STRING = "YOUR_DEVICE_CONNECTION_STRING"

def main():
    # Create instance of the device client
    device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)

    # Connect the client
    print("Connecting to IoT Hub...")
    device_client.connect()
    print("Connected.")

    # Send a message
    message = Message("Hello, Azure IoT Hub!")
    print(f"Sending message: {message}")
    device_client.send_message(message)
    print("Message sent.")

    # Disconnect the client
    print("Disconnecting from IoT Hub...")
    device_client.disconnect()
    print("Disconnected.")

if __name__ == "__main__":
    main()
                

IoT SDKs and Samples

Find SDKs for your preferred language and explore more complex device scenarios.

SDK Reference Code Samples

5. Monitor and Manage

Once your device is sending data, you can monitor it through IoT Hub's metrics, logs, and device twins.

Explore the Azure portal's "IoT Hub" resource for device monitoring, message routing configuration, and managing device twins.

Congratulations! You've taken your first steps in connecting devices to Azure IoT Hub. From here, you can explore more advanced features like direct methods, cloud-to-device messaging, and integrating with other Azure services.

Explore More Tutorials