Introduction
Azure IoT Hub is a cloud gateway which supports open source SDKs and multiple protocols. IoT Hub communicates both ways - devices to cloud and cloud to devices. The IoT Hub monitoring helps to check device health, failures, and device connection status.
Please read the previous parts of the series before continuing with this one.
- How To Create Azure IoT Hub Using PowerShell
- How to Register IoT Device in Azure IoT Hub Using PowerShell
- How To Send Telemetry From An IoT Device To The Azure IoT Hub Using C#
- How To Configure Message Routing With IoT Hub - Part One
- How To Configure Message Routing With IoT Hub - Part Two
- How To Setup Diagnostic Logs With An IoT Hub
Creating a C# Console Application
Installing Microsoft.Azure.Devices NuGet package
-
Go to Project -> Manage NuGet Packages.
-
Click the "Browse" tab and search for Microsoft.Azure.Devices. You will see Microsoft.Azure.Devices SDK listed in the search results; click the "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 Hub Connection string from Azure IoT Hub
-
Open your Azure portal and choose your IoT Hub.
-
Click "Shared access policies" in "Settings". You can see the list of policies to the IoT Hub.
-
Double click the iothubowner policy and copy the connection string from the Shared access key.
-
Here, I have used RegistryManager class. It contains methods that services can use to perform create, update, and delete operations on devices.
-
Also, we have used the AddDeviceAsync() method to create bulk IoT devices.
Open the program.cs file and type the below code.
- using System;
- using System.Threading.Tasks;
- using Microsoft.Azure.Devices;
- using Microsoft.Azure.Devices.Common.Exceptions;
-
- namespace IoTDeviceBulkImpExp
- {
- class Program
- {
- private static RegistryManager registryManager;
-
- private static string connectionString = "Enter the IoT Hub Connection String";
-
- static async Task Main(string[] args)
- {
-
- registryManager = RegistryManager.CreateFromConnectionString(connectionString);
-
- string deviceId = "device";
-
- for (int i = 0; i < 10; i++)
- {
- string newDeviceId = deviceId + i;
-
- await AddDeviceAsync(newDeviceId);
-
- }
- }
-
-
- private async static Task AddDeviceAsync(string deviceId)
- {
-
- Device device;
- try
- {
- Console.WriteLine("New device:");
-
- var d = new Device(deviceId);
-
- device = await registryManager.AddDeviceAsync(d);
- }
- catch (DeviceAlreadyExistsException)
- {
- Console.WriteLine("Already existing device:");
- device = await registryManager.
- GetDeviceAsync(deviceId);
- }
-
- Console.WriteLine("Generated device key: {0}",
- device.Authentication.SymmetricKey.PrimaryKey);
- }
- }
- }
Run the project using F5 function key.
That’s it. I hope you have learned how to bulk import the IoT devices in Azure IoT Hub programmatically using C#. Feel free to fill up the comment box below if you need any further assistance.