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?
-
There are a hundred ways of doing this, so this question might not be one for Stackoverflow. But if you need a reference on how to do you can check [this repo](https://github.com/MatthijsvdVeer/copy-the-world) for an example that uses IoT Hub. It uses a Function to translate incoming messages to ADT. – Matthijs van der Veer Jan 13 '23 at 08:52
1 Answers
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
Once you have the data routed, use either of the following resources based on your clod end point to build Azure function
- Run an Azure function when a Service Bus queue or topic message is created
- 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
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

- 333
- 1
- 9
-
thank you very much for the reply. Wow there are apparently a lot of steps then – Ziggy Jan 15 '23 at 12:21
-
It looks like a lot of steps but there are just three. 1 Route data from Azure IoT Central to a cloud end point. 2 Create Azure Digitial Twin. 3 Create Azure Function to respond to the events routed to the end point in step 1 and push the data to digital twin created in step 2. --Cheers – LeelaRajesh_Sayana Jan 16 '23 at 16:20