adafruit / Adafruit_CircuitPython_AzureIoT

Access to Microsoft Azure IoT device, messaging, and job services from CircuitPython!
MIT License
19 stars 15 forks source link

CPython sockets compatibility #24

Closed askpatrickw closed 3 years ago

askpatrickw commented 3 years ago

A typical setup requires an esp object which is related to the ESP32 wifi add-on boards.

device = IoTCentralDevice(socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"])

With the ESP32-s2 and its native wifi support, there is now a CPython-like socketpool. IoTCentralDevice needs to be udpated to support both methods similarly if not the same way Requests does and MiniMQTT is going to be.

brentru commented 3 years ago

@askpatrickw CPython sockets now work in v5.0.0 of MiniMQTT. Do you want to tackle this issue?

askpatrickw commented 3 years ago

@brentru woot!!

I may not get to it this week, but 100% Yes I will give it a go!

brentru commented 3 years ago

Thanks!

askpatrickw commented 3 years ago

I finally have time to look at this. I'll commit to my fork and ping back if I get stuck.

askpatrickw commented 3 years ago

Gotta take a break... but I'm moving along getting it working first just with native wifi, then I was thinking I'd figure out how to add the esp32spi support back.

Currently hitting the same error as this, not sure its the same problem yet. My current revisions are in my fork. https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/73

Traceback (most recent call last):
  File "code.py", line 88, in <module>
  File "/lib/adafruit_azureiot/iotcentral_device.py", line 162, in connect
  File "/lib/adafruit_azureiot/iot_mqtt.py", line 397, in connect
  File "/lib/adafruit_azureiot/iot_mqtt.py", line 142, in _create_mqtt_client
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 437, in connect
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 271, in _get_connect_socket
RuntimeError: Repeated socket failures
askpatrickw commented 3 years ago

Quick update from me... I've not given up on this.

I wanted to be able to side-by-side test\verify with the SPI implementation. I only have ESP32S2 boards, so I ordered some hardware from Adafruit back on 5 March and it looks like its finally going to be delivered today 21 March. (Yay USPS!) so over the next week I'll look at this again.

brentru commented 3 years ago

@askpatrickw I also have hardware handy if you'd like me to test a PR.

askpatrickw commented 3 years ago

I'm not finding great docs (which is weird for Msft) on the API version changes, but I do see this library was coded against the following version (constants.py)

# The version of the IoT Central MQTT API this code is built against
IOTC_API_VERSION = "2016-11-14"

# The version of the Azure Device Provisioning Service this code is built against
DPS_API_VERSION = "2018-11-01"

I changed these to IOTC_API_VERSION = "2018-06-30" and DPS_API_VERSION = "2019-03-31"

This is what IOT_MQTT looks like at _create_mqtt_client

{
    "_passwd": "SharedAccessSignature sig=snip1NN9%2B7y%2F8wZZftBtl8s%3D&se=1616847681&sr=iotc-snip-03dd-4ad0-be4a-snip.azure-devices.net%2Fdevices%snip",
    "_device_id": "snip",
    "_token_expires": 21600,
    "_logger": <Logger object at 200034b0>,
    "_auth_response_received": False,
    "_socket": <module "adafruit_esp32spi.adafruit_esp32spi_socket" from "/lib/adafruit_esp32spi/adafruit_esp32spi_socket.mpy">,
    "_hostname": "iotc-snip-03dd-4ad0-be4a-snip.azure-devices.net",
    "_mqtts": None,
    "_username": "iotc-snip-03dd-4ad0-be4a-snip.azure-devices.net/snip/api-version=2018-06-30",
    "_key": "snip",
    "_mqtt_connected": False,
    "_iface": <ESP_SPIcontrol object at 200035e0>,
    "_callback": <IoTCentralDevice object at 20003460>,
    "_is_subscribed_to_twins": False
}

username, passwd, and hostname and device_id all look good to me and look like the IoT Hub MQTT docs.