Dynamically Provision iot Device With Azure dps - Unexpected Failure. Python sdk

bruthaStopThat 25 Reputation points
2023-07-14T12:20:21.14+00:00

I am dynamically provision an iot device using the python azure-iot-device python package. I am using v2 and not 3.0.0b2. I can't even get that to compile. Here's my python code trying to provision a device:

import asyncio
import os

from azure.iot.device.aio import (
    ProvisioningDeviceClient,
)
from dotenv import load_dotenv
load_dotenv(dotenv_path=".env")

CONNECTION_STRING = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
ID_SCOPE = os.getenv("PROVISIONING_IDSCOPE")
REGISTRATION_ID = os.getenv("PROVISIONING_REGISTRATION_ID")
SYMMETRIC_KEY = os.getenv("PROVISIONING_SYMMETRIC_KEY")
PROVISIONING_HOST = os.getenv("PROVISIONING_HOST")
# PROVISIONING_SHARED_ACCESS_KEY = os.getenv("PROVISIONING_SHARED_ACCESS_KEY")

async def main():
    print("Starting multi-feature sample")
    provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key(
            provisioning_host=PROVISIONING_HOST,
            registration_id=REGISTRATION_ID,
            id_scope=ID_SCOPE,
            symmetric_key=SYMMETRIC_KEY,
        )
    provisioning_device_client.provisioning_payload = "
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,189 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
219 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,556 Reputation points MVP
    2023-07-16T21:51:24.4933333+00:00

    Hello @

    I assume you first try an individual enrollment.

    How do you get that device-related 'CONNECTION_STRING'?

    It should be returned by the DPS client after provisioning the device registration.

    That device connection string returns the linked IoT Hub, the device ID, and the connection string.

    Here is an example written in C#.

    Here is an example written in Python. See also this blog post.

    Notice the DPS will register the device in the selected IoT Hub.

    First, check if the DPS enrollment is successful (it shows the state).

    Then, check if the device is registered in the IoT Hub.

    Please, take some time to learn about the provisioning process using this learning module.


    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.


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.