Introduction
When building IoT solutions, one of the first things you need is a reliable way to connect your devices to the cloud. That’s where Azure IoT Hub becomes essential. It acts as a secure bridge between devices and cloud applications, enabling telemetry, command delivery, and device management. In this guide, we’ll walk through a simple .NET example—creating an IoT Hub, registering a device, and sending live telemetry.
Step 1: Create an IoT Hub using Azure CLI
We’ll use Azure CLI for a fast setup.
![1]()
Create a resource group
![2]()
Create the IoT Hub
![3]()
Step 2: Register a Device
Every device connecting to IoT Hub needs an identity.
![4]()
To get the connection string
![5]()
Retrieve and store this connection string, as it will be used by the device client in your .NET application for authentication and communication with IoT Hub.
Output
![6]()
Step 3: Send Telemetry Using .NET
Install SDK
dotnet add package Microsoft.Azure.Devices.Client
Sample Code
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
class Program
{
private static string connectionString = "<YOUR_CONNECTION_STRING>";
private static DeviceClient deviceClient;
static async Task Main(string[] args)
{
deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt);
Console.WriteLine("Device connected. Sending telemetry...");
while (true)
{
var temp = new Random().Next(20, 35);
var messageString = $"{{\"temperature\":{temp}}}";
var message = new Message(Encoding.UTF8.GetBytes(messageString));
await deviceClient.SendEventAsync(message);
Console.WriteLine($"Sent: {messageString}");
await Task.Delay(3000);
}
}
}
![7]()
Output
The below output confirms our device is actively pushing data to the cloud.
![8]()
Step 4: Monitor Messages in IoT Hub
Execute the below command in command prompt to monitor the events sent to the iothub.
![9]()
Output
From the below output window, we can clearly see the events are ingested from device to cloud in real time.
![10]()
Step 5: Send Cloud-to-Device Message
![11]()
Output
![12]()
Conclusion
We built a working IoT pipeline using Azure IoT Hub and .NET. From creating the hub to sending telemetry and viewing live output, everything was done using simple tools like Azure CLI. The key takeaway here is how quickly we can go from zero to a working prototype. Once this foundation is in place, we can scale to thousands of devices, add security layers, or integrate with analytics services.