Ingest Telemetry from IoT Hub to Azure Digital Twin using Python Error

Anh Dinh 40 Reputation points
2023-07-28T08:41:27.5833333+00:00

Hello, I would like to send the telemetry data from the IOT hub to Azure digital twins using Python instead of C#( I don't have experience with C#), but I can't get it.

I have followed the complete example from Microsoft (https://learn.microsoft.com/en-us/azure/digital-twins/how-to-ingest-iot-hub-data) without success.

This is my code for Simulated Device. I checked and it successfully send the message to IoT Hub:

from azure.iot.device import IoTHubDeviceClient
import json
import random
import time

CONNECTION_STRING = "
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.
231 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,118 questions
0 comments No comments
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 16,046 Reputation points
    2023-07-28T20:09:29.53+00:00

    Hi @Anh Dinh , as Sander said, you need to check if your Event Grid function is configured to receive data from Event Hub. If you look into the section Connect the IoT hub to the Azure function, it provides the Azure CLI command needed to link the IoT Hub with the Azure function. Please note that you would need to publish the function on the Azure function app before you get execute the following command.

    az iot hub device-identity create --device-id thermostat67 --hub-name <your-IoT-hub-name> --resource-group <your-resource-group>
    

    I would also recommend modifying the simulation code to ensure that the data sent is in utf-8 format. You can achieve this using the Message class from azure.iot.device package. Please refer the following sinppet

    from azure.iot.device import IoTHubDeviceClient, Message
    
    MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}'
    while true:
        temperature = TEMPERATURE + (random.random() * 15)
        humidity = HUMIDITY + (random.random() * 20)
        msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)
        message = Message(msg_txt_formatted, content_encoding="utf-8", content_type="application/json")
        client.send_message(message)
    

    Within your Event Grid function inspect the data obtained by doing the following

    def main(event: func.EventGridEvent):
        result = json.dumps({
            'id': event.id,
            'data': event.get_json(),
            'topic': event.topic,
            'subject': event.subject,
            'event_type': event.event_type,
        })
    
        logging.info('Python EventGrid trigger processed an event: %s', result)
    

    Hope this helps. Please let us know if you have any addtioinal questions or need further assistance.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 33,136 Reputation points MVP
    2023-07-28T14:24:35.88+00:00

    Hello @Anh Dinh,

    It seems you are receiving IoT messages using EventGrid.

    Is 'logging.info(event.data)' executed? Does the message data look like what you expected?

    Without sending the data toward ADT, do you see the Azure Function (when running in your VS Code) being executed using IoT Hub messages?

    Without sending the data toward ADT, do you see the Azure Function (when running in the cloud) being executed using IoT Hub messages?

    Notice you create a new DigitalTwinsClient EVERYTIME a call is made to the function. Functions are stateless. You could try to make the DigitalTwinsClient creation static, outside the function This works well in C#.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    3 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.