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.