使用Azure dps动态配置IoT设备-意外失败,Python SDK

uttx8gqw  于 2023-08-07  发布在  Python
关注(0)|答案(1)|浏览(97)

我正在使用python azure-iot-device python包动态配置IoT设备。我用的是v2而不是3.0.0b2。我都没法编译。
下面是我的python代码,它尝试配置一个设备:

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 = "<Your Payload>"
    provisioning_result = None
    try:
        provisioning_result = await provisioning_device_client.register()
    except Exception as e:
        print(f"an error occurred provisioning the device -- {e}")
    finally:
        print(f"result -- {provisioning_result}")
  
if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        # Exit application because user indicated they wish to exit.
        # This will have cancelled `main()` implicitly.
        print("User initiated exit. Exiting.")

字符串
通过使用注册组主密钥来计算设备的注册ID的HMAC-SHA 256来导出对称密钥。我只是简单地按照本指南中的“派生设备密钥”一节进行操作--https://learn.microsoft.com/en-us/azure/iot-dps/how-to-legacy-device-symm-key?tabs=linux&pivots=programming-language-python#derive-a-device-key
我一直收到“意外失败”错误。代码非常少,几乎没有什么可调试的。我相信我在设置我的物联网中心和DPS时密切遵循了这些步骤。请让我知道任何建议

vfhzx4xs

vfhzx4xs1#

错误消息“意外失败”过于一般,无法确定问题的根本原因。但是,我们可以检查一些事情来解决问题。

  • 首先,确保连接字符串、ID范围、注册ID、对称密钥和Provisioning主机都正确有效。其次,确保设备尚未配置。


的数据


  • 创建IoT Hub和IoT设备
  • 创建Azure IoT Hub设备设置服务
  • 在(DPS)Linked IoT hubs中添加IoT hub。
  • 使用添加链接到IoT中心管理注册。
  • 在调配设备上启用IoT Edge。
  • 已将此链接用于MSDOC中的az iot dps enrollment-group
  • 替换resourcegroupname、dpsname、enrollment_idregistration_id。它给出derived-device-keyPROVISIONING_SYMMETRIC_KEY
az iot dps enrollment-group compute-device-key -g {resource_group_name} --dps-name {dps_name} --enrollment-id {enrollment_id} --registration-id {registration_id}

字符串


  • 在DPS Azure IoT Hub Device Provisioning Service中使用对称密钥注册组对Provisioning设备使用了相同的参考


验证码:

import asyncio
import os
import logging
from azure.iot.device.aio import ProvisioningDeviceClient
from azure.iot.device import exceptions
from dotenv import load_dotenv
logging.basicConfig(level=logging.DEBUG)
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")
async def main():
    print("Starting the device provisioning process")
    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 = "<Your Payload>"
    provisioning_result = None
    try:
        provisioning_result = await provisioning_device_client.register()
        print("Device successfully provisioned!")
        print("Provisioning result:")
        print(provisioning_result.registration_state)
        print(f"Device ID: {provisioning_result.registration_state.device_id}")
    except exceptions.ProvisioningError as pe:
        print(f"Provisioning failed with error: {pe}")
    except Exception as e:
        print(f"An error occurred provisioning the device: {e}")
if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("User initiated exit. Exiting.")

输出:


配置状态:


相关问题