Azure IoT Hub – Get started

Welcome

This quickstart helps you create an Azure IoT Hub, register a device, and send telemetry from that device to the cloud using your language of choice.

Prerequisites

1. Create an IoT Hub

Run the following Azure CLI command:

az iot hub create --resource-group MyResourceGroup --name MyIoTHub --sku F1

This creates a free‑tier hub named MyIoTHub in the resource group MyResourceGroup.

2. Register a device

Generate a device identity called myFirstDevice:

az iot hub device-identity create --hub-name MyIoTHub --device-id myFirstDevice --output json

Copy the primaryKey from the output – you’ll need it in the sample code.

3. Send telemetry from a device

C#
Python
Java
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;

class Program
{
    private const string DeviceConnectionString = "";
    private static DeviceClient deviceClient;

    static async Task Main(string[] args)
    {
        deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
        Console.WriteLine("Sending telemetry...");
        for (int i = 0; i < 5; i++)
        {
            var telemetry = new
            {
                temperature = 20 + i,
                humidity = 60 + i
            };
            string messageString = Newtonsoft.Json.JsonConvert.SerializeObject(telemetry);
            using var message = new Message(Encoding.ASCII.GetBytes(messageString));
            await deviceClient.SendEventAsync(message);
            Console.WriteLine($\"Sent: {messageString}\");
            await Task.Delay(1000);
        }
        await deviceClient.CloseAsync();
    }
}
import json
import time
from azure.iot.device import IoTHubDeviceClient, Message

CONNECTION_STRING = ""

def main():
    client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
    print("Sending telemetry...")
    for i in range(5):
        telemetry = {{
            "temperature": 20 + i,
            "humidity": 60 + i
        }}
        msg = Message(json.dumps(telemetry))
        client.send_message(msg)
        print("Sent:", telemetry)
        time.sleep(1)
    client.shutdown()

if __name__ == "__main__":
    main()
import com.microsoft.azure.sdk.iot.device.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

public class TelemetrySender {
    private static final String connString = "";
    private static DeviceClient client;

    public static void main(String[] args) throws URISyntaxException, IOException {
        client = new DeviceClient(connString, IotHubClientProtocol.MQTT);
        client.open();
        System.out.println("Sending telemetry...");

        for (int i = 0; i < 5; i++) {
            Map<String, Object> telemetry = new HashMap<>();
            telemetry.put("temperature", 20 + i);
            telemetry.put("humidity", 60 + i);
            String msgStr = new com.google.gson.Gson().toJson(telemetry);
            Message msg = new Message(msgStr);
            client.sendEventAsync(msg, new EventCallback(), null);
            System.out.println("Sent: " + msgStr);
            try { Thread.sleep(1000); } catch (InterruptedException ignored) {}
        }

        client.closeNow();
    }

    private static class EventCallback implements IotHubEventCallback {
        @Override
        public void execute(IotHubStatusCode status, Object context) {
            System.out.println("Telemetry sent with status: " + status.name());
        }
    }
}

Next steps