Azure / iot-central-micropython-client

A micropython SDK for connecting devices to Microsoft Azure IoT Central
MIT License
13 stars 12 forks source link

KeyError: operationId at provision.py #7

Open Kodalem opened 9 months ago

Kodalem commented 9 months ago

Following the sample.py, my PicoW on Micropython throws the following error at the client.connect(), even running the pull request version of the library threw the same error.

Traceback (most recent call last):
  File "<stdin>", line 52, in <module>
  File "/lib/iotc/__init__.py", line 104, in connect
  File "/lib/iotc/provision.py", line 131, in register
KeyError: operationId

Here's the following slightly modified sample code.

import sys
from utime import sleep
from random import randint

import secrets
import network

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

print("Connecting to WiFi Network...")
wlan.connect(secrets.WIFI_SSID,secrets.WIFI_PASSWORD)
while not wlan.isconnected():
    pass

print(wlan.isconnected())
print(wlan.ifconfig())

try:
    import iotc
except:
    import mip
    mip.install('github:Azure/iot-central-micropython-client/package.json')
    import iotc

from iotc import IoTCClient,IoTCConnectType,IoTCLogLevel,IoTCEvents

scope_id = secrets.id_scope
device_id = secrets.device_id
print(secrets.device_primary_key)
key = secrets.device_primary_key
conn_type=IoTCConnectType.SYMM_KEY

client=IoTCClient(scope_id,device_id,conn_type,key)
client.set_log_level(IoTCLogLevel.ALL)

def on_properties(name, value):
    print('Received property {} with value {}'.format(name, value))
    return value

def on_commands(command, ack):
    print('Command {}.'.format(command.name))
    ack(command, command.payload)

def on_enqueued(command):
    print('Enqueued Command {}.'.format(command.name))

client.on(IoTCEvents.PROPERTIES, on_properties)
client.on(IoTCEvents.COMMANDS, on_commands)
client.connect()

client.send_property({'readOnlyProp':40})

while client.is_connected():
    client.listen()
    print('Sending telemetry')
    client.send_telemetry({'temperature':randint(0,20),'pressure':randint(0,20),'acceleration':{'x':randint(0,20),'y':randint(0,20)}})
    sleep(2)

Upon further inspection, it appears the uri that is received indeed has a certificate and it is received, but it appears somewhere here lies the issue, but unsure what exactly is going on under the hood to fix this.

response = urequests.put(
            uri, data=json.dumps(body), headers=self._headers)
operation_id = json.loads(response.text)['operationId']

Here's the certificate of the uri.

Common Name: *.azure-devices-provisioning.net
Issuing CA: MSFT BALT RS256 CA
Organization: Microsoft Corporation
Valid: January 28, 2024 to June 28, 2024
Key Size: 2048 bits 

Is this a hardware issue or is something wrong from the IoT Hub setup in my Azure?

Kodalem commented 9 months ago

By learning more about .json with Python, the following response.text from the uri is this:

{"errorCode":400004,"trackingId":"12a8445c-5bcf-4028-82a2-b2adda56fb7c","message":"Unsupported API version 2019-01-15.","timestampUtc":"2024-02-04T20:29:26.8360444Z"}

Which explains why it cannot find the operationId.

Can the following API version be upgraded?

Kodalem commented 9 months ago

Yeah, as I was afraid, just changing into latest self._api_version = '2022-07-31' at provision.py didn't make it work out of the box and throws another new error. Likely I assume due to new standards to be following with the header?

{"Error":{"Code":"UnsupportedApiVersion","Message":"The HTTP resource that matches the request URI \u0027https://global.azure-devices-provisioning.net/0ne00B7339E/registrations/477qz26jl6/register\u0027 does not support the API version \u00272020-08-01\u0027.","InnerError":null}}
mheskandari commented 8 months ago

I have the same issue `on Registering... Traceback (most recent call last): File "", line 45, in File "lib/iotc/init.py", line 104, in connect File "/lib/iotc/provision.py", line 124, in register KeyError: operationId

kohenro19 commented 3 months ago

@mheskandari I have the same issue too. Did you solve it ?

Kodalem commented 3 months ago

@mheskandari I have the same issue too. Did you solve it ?

Nope, went to AWS IoT solution instead. This is likely broken till the package maintainer (Microsoft) fixes the issue with the outdated API solution. Kind of a shame because the client had preferred to use Power BI instead of creating house-built data visualiser.

kohenro19 commented 3 months ago

@mheskandari Thank you for your replying. Like you, I am giving up connecting pico w to Azure and moving to AWS

kohenro19 commented 3 months ago

@mheskandari I switched from Azure to AWS IoT Core like you , but encountered an error of "bytes index out of range" and still am not able to solve it. If you don't mind, would you share your MicroPython code successful to connect to AWS IoT.