Question

davide.v86 on Tue, 12 Jun 2018 14:00:36


Hello,

I'm developing a IOT Edge Module in python.

I've to read device Desired properties and send a message, so i connect to IotHub and to the Device Twin.

I used the connection strings found on Azure Portal in my module and it works fine.

In my project, I have several devices and i need to obtain "dinamically" hub and device connections strings in my Python module.

How can i do that?

Thanks.


Sponsored



Replies

Rita Han on Wed, 13 Jun 2018 02:29:22


Hello davide,

If you mean you have several edge devices and don't want to retrieve device connection string from Azure Portal, here is an option.

You can only store Azure IoT Hub connection string and use Microsoft.Azure.Devices.RegistryManager.GetDevicesAsync() to get the device and then use Device.Authentication.SymmetricKey get the device key.

Device connection string has the following format:

HostName=[YOUR HUB NAME].azure-devices.net;DeviceId=[DEVICE ID];SharedAccessKey=[DEVICE KEY]

So you can use device id and device key to generate the device connection.

I don't test Python(Module) and you can have a try. If there is any concern feel free let me know.

Best regards,

Rita

davide.v86 on Wed, 13 Jun 2018 16:09:45


Thank you Rita!

It worked.

Here the code I used:

iothub_registry_manager = IoTHubRegistryManager(conn_hub)
twinOBJ = iothub_registry_manager.get_device(DEVICE_ID)

But I need also the Device ID.

Where I can find it?

Regards,

Davide.

Rita Han on Thu, 14 Jun 2018 02:11:29


Hello davide,

In Python, you can use get_device_list() API to retrieve a list of devices registered in Azure IoT Hub. For example:

dev_list = iothub_registry_manager.get_device_list(number_of_devices)

For each device in the list you can use device.deviceId to get the Device ID.

Best regards,

Rita

 

davide.v86 on Thu, 14 Jun 2018 08:51:16


Hello Rita,

I need "current" DEVICE ID.

I want to know the id of the device where the module is running.

Is that possible?

Thanks,

Davide.

Rita Han on Thu, 14 Jun 2018 10:04:39


Hello davide,

I am not clear your use case and consider the following situations:

  • You need device id from external the module, this case, you can store device id locally on the module and send with it out in D2C message.
  • You need get device id runtime on module itself, only via IoTHubRegistryManager APIs we mentioned above.
  • You need get device id via DeviceClient, though there is GetTwinAsync API but per-device endpoint can't get device id but only properties. Reference - IoT Hub endpoints

Best regards,

Rita