Azure IoT Hub Device Simulation with .Net C#

Arkaprava Sinha
.Net Programming
Published in
3 min readMar 21, 2021

Today, we will learn how to simulate IoT devices in .NET and C#. These simulated IoT devices will be sending the data to Azure IoT Hub. After we received the telemetry data in IoT hub, we can run Time Series Insights or we can configure Event Hub to process the data or we can also use Azure Stream Analytics which is a costly resource. So let’s get strated then…

What you need?

  1. Azure Account, don’t worry if you don’t have one create a free trial or you can use the Microsoft Learn Sandboxes. (Please always delete the resources after you have done)
  2. Visual Studio 2019

Nuget Packages Required

  1. Microsoft.Azure.Devices.Client (it will add other dependent projects)

Steps:

  1. Go to Azure Portal
  2. Create a Resource , in the Search Bar , Search for IoT Hub
  3. Then Select IoT Hub and click on Create Button
IoT hub in Azure Portal Market Place

4. Then Select your subscription and resource group , after that select a location where you want to deploy the IoT Hub instance better to create it near your geographical location so latency will be lesser and also Provide a name. I am choosing Central India as location as It is closest to my location. click on Review + Create. It will review and validate the settings and then click on Create. It will take some time to create the IoT Hub, please wait till then.

5. After IoT Hub is created, go to the Resource, On Left Menu you will find IoT Devices option under Explorer Sub Menu, Select that one and Click on +New to create a new device. Provide a unique name for the device, as of now we will authenticate the device with Shared Access Signature Token, if you want to change the Authentication Type you can and then click on Save.

6. Now you can find your newly created Device in Explorer sub menu under the IoT Devices option. Please click on your <device-name> to go to the connection settings page.

Explorers>IoT Devices

7. Copy the Primary Connection String and Paste it in notepad for later use.

8. Now Go to Visual Studio.

9. Create a New Console Project (.Net Framework) add the Nuget package mentioned above . It will add other necessary dependent packages as well.

10. Copy and Paste the below code in Program.cs and Rename the File name to TruckSystem.cs and Please change the connection string to earlier copied one.

11. Run the Application , your console will look like this,

Console

12. Now go to your Azure Portal, navigate to your IoT Hub and got Overview and if you scroll down a bit, you will see below metrics,

13. Now we are able to send data to IoT Hub.

For any clarifications and doubts , follow Microsoft Documentation.

Please let me know your feedbacks on the same so that we can improve our material quality and help everyone on this cloud journey.

--

--

Arkaprava Sinha
.Net Programming

Senior Software Engineer@Walmart , Cloud, IoT and DevOps Enthusiast