Hi @Hamza Outa Greetings! Thank you for posting this question here.
You are encountering this error because you are trying to use the same device connection string from two different client applications simultaneously. This device connection string is not meant to be used by different clients. It is supposed to be used from a single device.
To overcome this error and achieve what you are looking for, you can leverage the Service shared access policy of IoT Hub and use the below code to get the device twin properties.
from azure.iot.hub import IoTHubRegistryManager
# Connection string for the IoT Hub service shared access policy
CONNECTION_STRING = "<IoTHubServiceConnectionString>"
DEVICE_ID = "TestDevice1"
def get_device_twin():
# Create an instance of the IoTHubRegistryManager
registry_manager = IoTHubRegistryManager.from_connection_string(
CONNECTION_STRING)
try:
# Get the device twin
twin = registry_manager.get_twin(DEVICE_ID)
print("Device Twin:")
print(twin)
desired_properties = twin.properties.desired
# Process desired properties
if desired_properties:
print("Desired properties:")
print(desired_properties)
else:
print("No desired properties found.")
except Exception as e:
print("Error getting device twin:", str(e))
# Call the function to get the device twin
get_device_twin()
You can get the connection string for Service policy by navigating to Security settings on IoT Hub --> Shared Access Policies --> service --> Primary Connection string.
Hope this helps. Please let us know if you have any additional questions or need further clarifications on this.
If the response helped, please do click Accept Answer and Yes. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.