Introduction
In this article, we are going to see how to send telemetry from an IoT device to the Azure IoT Hub using C#. IoT Hub is a cloud platform to securely connect billions of IoT devices to create IoT applications. Please read the previous parts of the article before continuing with this one.
- How To Create Azure IoT Hub Using PowerShell
- How to Register IoT Device in Azure IoT Hub Using PowerShell
Creating a C# Console Application
-
Open Visual Studio, go to File -> New -> and select Project.

-
In Templates, select Visual C#, select Console App (.NET Framework) and give an appropriate name in the ‘Name’ textbox and then click the OK button.

Installing Microsoft Azure IoT Device Client SDK
-
Go to Project -> Manage NuGet Packages.
-
Click Browse tab and search for Microsoft.Azure.Devices.Client. You will see the Microsoft.Azure.Devices.Client device SDK will have listed in the search result and click Install button
-
Now, click the I Accept button to accept the license agreement.
-
It will take a few minutes to install the SDK in our project

Get IoT device connect string from Azure IoT Hub
-
Open your Azure portal and choose your IoT Hub.
-
Click IoT devices In Explorers. You can see the list of devices that are connected to the IoT Hub.

-
Double click the device, you can see the device detailed information like device id, Primary Key, Secondary Key, Connection String(primary key) and Connection String (secondary key).
-
Using Microsoft.Azure.Devices.Client library we can create device client. The device client has CreateFromConnectionString method which requires device connection string as parameter. Create a read only static string s_connectionString01 and assign the connection string that we copy from Azure Portal.
-
Here you can create a random temperature and humidity values using Random() method.
-
Now open the program.cs file and type the below code


Michael LenartPosted Aug 10, 2022, 10:50 PM
Is there a way to programmatically delete the telemetry?
Abhishek MishraPosted Feb 1, 2019, 11:18 AM
Loved this series
Sourav MukherjeePosted Feb 1, 2019, 8:54 AM
Very Nice, article. Thanks, Ravi!