2
votes

I am in the middle of a project where I having to connect some IoT devices to azure using IoTHub. I have been following this guide: https://docs.microsoft.com/en-us/samples/azure-samples/functions-js-iot-hub-processing/processing-data-from-iot-hub-with-azure-functions/

And things are working fine, I have a device connected to the IoTHub called MyPythonDevice, so now in my code I would like to see this deviceId. In the examples given in the article above, we see a deviceId, but for me that is undefined if I log it.

So I was searching and found the following code snippet:

context.log(context.bindingData.systemProperties["iothub-connection-device-id"])

But this returns the follwing

Exception: TypeError: Cannot read property 'iothub-connection-device-id' of undefined

That means that systemProperties is undefined..

Any help on how to get the deviceId ?

2
in the article, it's fetching the device id from the message. Can you post a sample of your IoTHubMessage ?Thiago Custodio

2 Answers

4
votes

Try this:

context.bindingData.systemPropertiesArray[0]["iothub-connection-device-id"]
0
votes

This Works:

    public static string Run([EventHubTrigger("EventHubName", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "xxxxx")] EventData eventMsg, DateTime enqueuedTimeUtc, ILogger log) {
var deviceId = eventMsg.SystemProperties["iothub-connection-device-id"].ToString();}