Connect Device from Azure IoT Central to Azure Digital Twin

146 Views Asked by At

I have a device in Azure IoT central that has been receiving data. How can I send its telemetry data to its Azure Digital Twin so that they are connected?

1

There are 1 best solutions below

2
On

You first need to pass the data from Azure IoT Central device to a cloud end point such as Service Bus or Event Hub. You can create an Azure Function and trigger it based on the end point you passed the data to. Within the Azure function you can create the code to push data to the Azure Digitial Twins.

Refer the following resources for data export

  1. Export IoT data to Service Bus
  2. Export IoT Hub data to Event Hubs

Once you have the data routed, use either of the following resources based on your clod end point to build Azure function

  1. Run an Azure function when a Service Bus queue or topic message is created
  2. Respond to events send to Event Hub event stream

Once you have the data flow into the Azure function, before you push the data to the Azure Digital Twin, ensure you have a Model created on the Azure digital twin representing you IoT device data.

If you haven't already done so, refer the Upload a model section to create a Data model for your Azure Digital Twin. Once you have the data model created, you can create Azure Digital Twin either from the Azure Digital Twin explorer or though the code Create Azure Digital Twin.

Once you have the Digital Twin created, you can then push data from your Azure function you created earlier. Here is the code I have tested through ServiceBus Queue trigger.

using System;  
using System.Net.Http;  
using System.Threading.Tasks;  
using Azure;  
using Azure.Core.Pipeline;  
using Azure.DigitalTwins.Core;  
using Azure.Identity;  
using Microsoft.Azure.WebJobs;  
using Microsoft.Azure.WebJobs.Host;  
using Microsoft.Extensions.Logging;  
using Newtonsoft.Json;  
using Newtonsoft.Json.Linq;  
  
namespace RajIoTCentral.Function  
{  
    public class RajIoTCentralServiceBusQueue  
    {  
        private static readonly HttpClient httpClient = new HttpClient();  
        private static string adtServiceUrl = "https://<yourdigitaltwinendpoint>.digitaltwins.azure.net";  
  
        [FunctionName("RajIoTCentralServiceBusQueue")]  
        public async Task RunAsync([ServiceBusTrigger("iotcentraldata", Connection = "RajIoTDataServiceBus_SERVICEBUS")] string myQueueItem, ILogger log)  
        {  
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");  
  
            JObject deviceMessage = (JObject)JsonConvert.DeserializeObject(myQueueItem);  
            try  
            {  
                var temperature = deviceMessage["telemetry"]["temperature"];  
                var deviceId = "TemperatureSensor";  
                log.LogInformation($"Temperature is:{temperature.Value<double>()}");  
                var credentials = new DefaultAzureCredential();  
                DigitalTwinsClient client = new DigitalTwinsClient(  
                    new Uri(adtServiceUrl), credentials, new DigitalTwinsClientOptions  
                    { Transport = new HttpClientTransport(httpClient) });  
                log.LogInformation($"ADT service client connection created.");  
                var updateTwinData = new JsonPatchDocument();  
                updateTwinData.AppendReplace("/Temperature", temperature.Value<double>());  
                await client.UpdateDigitalTwinAsync(deviceId, updateTwinData);  
            }  
            catch (Exception ex) { log.LogInformation("No temperature in the telemetry data"); }  
  
        }  
    }  
}  

In the above code, I am pushing the Temperature value I received from the Service Bus queue on to the Azure Digital Twin.

Please refer the following resources for more information on the above code and the approach

  1. Ingest IoT Hub telemetry into Azure Digital Twins
  2. Build out an end-to-end solution

A similar post has been addressed on the Microsoft Q&A forum in the past. Please refer the question here Unable to view result of pipeline which trasport a value from a sensor to digital twin