Digital Twin is not updating [SOLVED]

Argianto Rahartomo 31 Reputation points
2022-07-18T14:59:02.313+00:00

Dear all,
I have tried this documentation URL: https://learn.microsoft.com/en-us/azure/digital-twins/how-to-ingest-iot-hub-data and I still have not able to update my Digital Twin telemetry instance knowing in IoT Hub was successful.

Please kindly for the help

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
221 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Argianto Rahartomo 31 Reputation points
    2022-07-28T07:30:25.183+00:00

    Thank You in advance @Matthijs van der Veer , yes it was. I realized the issue was came from the Function App in the context of the C# code.
    It failed to read the message body to obtain my telemetry, because it was encoded.
    To decode, we can use:

    var bodyJson = Encoding.ASCII.GetString((byte[])deviceMessage["body"]);
    JObject body = (JObject)JsonConvert.DeserializeObject(bodyJson);

    Another thing is we need to instantiate BasicDigitalTwin first before updating our Digital Twin as follows (for example in my case):

    var cred = new DefaultAzureCredential();
    var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), cred);

    string twinId = "<TWIN_ID>";
    var initData = new BasicDigitalTwin
    {
    Id = twinId,
    Metadata = { ModelId = "<MODEL_ID>" },
    Contents =
    {
    { "Temperature", 0.0},
    { "Humidity", 0.0},
    { "Light", 0.0}
    }
    };