Please read the previous parts of the article series before continuing with this one.
What is device twin?
Device twins contain information on IoT device configurations, metadata, and conditions. Device twins store the device state information in a JSON document.
Prerequisites
Before you begin, ensure that you have created Azure IoT Hub and registered IoT device in the IoT Hub. if not, here is the article on How To Create Azure IoT Hub Using PowerShell and also, How to Register IoT Device in Azure IoT Hub Using PowerShell.
Get IoT Hub connection string
-
Open the IoT Hub in the Azure Portal. Select resource groups, select the resource group where you created the IoT Hub.
-
In the left pane, you can see the Shared access policies. From the top menu select +Add.
-
Enter the access policy name as serviceAndRegistryRead. choose permissions Registry Read and Service connect. Then, click Create.

-
Back on the Shared access policy, select copy icon for the Connection string --primary key

Create the service app to add tags and query the device twins
Here we create the .NET console app using C# that adds the location metadata to the device twin associated with the registered IoT device.
Here we query the device twins store in the IoT Hub to select devices located in Coimbatore and then reported a cellular connection.
-
Open Visual Studio -> choose to create a new project and select the Console App (.NET Core) and click Next.

-
Enter the project name as AddTagsAndQuery, location, solution name and click Create.

-
In Solution Explorer, right-click the AddTagsAndQuery project, and then click Manage NuGet package...
-
In the NuGet Package Manager, click the Browse tab and search Microsoft.Azure.Devices, and select Install.

Add the following code in the program.cs file.
- using System;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.Azure.Devices;
- namespace AddTagsAndQuery
- {
- class Program
- {
- static RegistryManager registryManager;
- static string connectionString = "<Enter the IoT Hub Connection String>";
- static void Main(string[] args)
- {
- registryManager = RegistryManager.CreateFromConnectionString(connectionString);
- AddTagsAndQuery().Wait();
- Console.WriteLine("Press Enter to exit.");
- Console.ReadLine();
- Console.WriteLine("Hello World!");
- }
- public static async Task AddTagsAndQuery()
- {
- var twin = await registryManager.GetTwinAsync("RPi01");
- var patch =
- @"{
- tags: {
- location: {
- region: 'India',
- plant: 'Coimbatore'
- }
- }
- }";
- await registryManager.UpdateTwinAsync(twin.DeviceId, patch, twin.ETag);
- var query = registryManager.CreateQuery(
- "SELECT * FROM devices WHERE tags.location.plant = 'Coimbatore'", 100);
- var twinsInCoimbatore = await query.GetNextAsTwinAsync();
- Console.WriteLine("Devices in Coimbatore: {0}",
- string.Join(", ", twinsInCoimbatore.Select(t => t.DeviceId)));
- query = registryManager.CreateQuery("SELECT * FROM devices WHERE tags.location.plant = 'Coimbatore' AND properties.reported.connectivity.type = 'cellular'", 100);
- var twinsInCoimbatoreUsingCellular = await query.GetNextAsTwinAsync();
- Console.WriteLine("Devices in Coimbatore using cellular network: {0}",
- string.Join(", ", twinsInCoimbatoreUsingCellular.Select(t => t.DeviceId)));
- }
- }
- }







Sundaram SubramanianPosted May 8, 2020, 5:05 AM
Thanks a lot, mate. Good to know many new technologies. Keep sharing, helps me a lot
Abhishek MishraPosted Aug 28, 2019, 12:06 AM
Your series is highly helpful for one of my friend. He is doing IOT implementation and using your article as reference.