```html Getting Started with Azure IoT - MSDN Documentation

Azure IoT Documentation

Introduction

Azure IoT Hub provides a secure, reliable, and scalable platform for connecting, monitoring, and managing billions of IoT assets. This guide walks you through creating your first IoT Hub, provisioning a device, and sending telemetry using C#.

Prerequisites

1️⃣ Create an IoT Hub

  1. Sign in to the Azure Portal.
  2. Click Create a resource → Internet of Things → IoT Hub.
  3. Give it a unique name, select a region, and choose the Free F1 tier.
  4. Review + create.
  5. Wait for deployment to finish and note the Hostname (e.g., my-iothub.azure-devices.net).

2️⃣ Register a Device

  1. Navigate to your IoT Hub resource.
  2. Select IoT Devices → + New.
  3. Enter a device ID (e.g., myDevice001) and click Save.
  4. Copy the generated Primary connection string. You’ll need it in the code.

3️⃣ Send Telemetry from C#

Below is a minimal console app that sends a temperature reading to IoT Hub every second.

// Program.cs
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;

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

    static async Task Main()
    {
        deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt);
        Console.WriteLine("Sending telemetry... Press Ctrl+C to exit.");
        while (true)
        {
            var telemetry = new
            {
                temperature = new Random().Next(20, 30) + new Random().NextDouble(),
                timestamp = DateTime.UtcNow
            };
            var messageString = JsonConvert.SerializeObject(telemetry);
            var message = new Message(Encoding.UTF8.GetBytes(messageString))
            {
                ContentType = "application/json",
                ContentEncoding = "utf-8"
            };
            await deviceClient.SendEventAsync(message);
            Console.WriteLine($"\u001b[32mSent: {messageString}\u001b[0m");
            await Task.Delay(1000);
        }
    }
}

4️⃣ Verify the Telemetry

  1. In Azure Portal, go to your IoT Hub → IoT devices → your device → Messages.
  2. Enable Built‑in endpoint → Events and click Start monitoring.
  3. You should see JSON payloads similar to:
    {
      "temperature": 24.73,
      "timestamp": "2025-09-16T12:34:56.789Z"
    }

Next Steps

```