adafruit / Adafruit_CircuitPython_AzureIoT

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

Issues using on Arduino Nano RP2040 Connect #46

Open henrikanderssoncompago opened 2 years ago

henrikanderssoncompago commented 2 years ago

Hi,

I´m trying to get this library to work on my Arduino RP2040 Nano Connect board. Got Wifi working and connected

But when running this code:

from adafruit_azureiot import IoTHubDevice device = IoTHubDevice(pool, esp, secrets["azure_device_connection_string"]) device.connect

I get this error:

code.py output: Traceback (most recent call last): File "code.py", line 76, in File "adafruit_azureiot/init.py", line 35, in File "adafruit_azureiot/iot_mqtt.py", line 17, in ImportError: no module named 'ssl'

The azure_device_connection_string is copied from the IoT hub device page in Azure (and validated by using a VS Code extension to post message on the default service bus)

Using the latest CircuitPython library from https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases

I have also tried to use the Adafruit IO bundle and the MQTT client, but are unable to find any documentation how to use it against an Azure IoT Hub

Please advice

brentru commented 2 years ago

Are you using one of the esp32spi examples within https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT/tree/main/examples/azureiot_esp32spi?

henrikanderssoncompago commented 2 years ago

Hi, Using the latest sample again. Had to change line 32 to: spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)

Error message: Connecting to WiFi... Connected to WiFi! Getting the time... Time: 1653632230 Traceback (most recent call last): File "code.py", line 95, in File "adafruit_azureiot/init.py", line 35, in File "adafruit_azureiot/iot_mqtt.py", line 17, in ImportError: no module named 'ssl

Looking at the CircuitPython download page for RP2040 (https://circuitpython.org/board/arduino_nano_rp2040_connect/), no SSL module is included in the build.

Might this the root cause?

Regards // Henrik

brentru commented 2 years ago

I think this got modified by @jimbobbennett, ssl is a non-native module for ESP32-SPI ("AirLift") hardware but is native for specific boards (native WiFi) https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT/blob/main/adafruit_azureiot/iot_mqtt.py#L17

We'd need to make a pull request to better handle this within https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT/blob/f43a8e7b7cac3861b8d7a6f355d71a641fe4a508/adafruit_azureiot/iothub_device.py#L141

BlitzCityDIY commented 1 year ago

I'm running into this issue as well with a PyPortal after looking into this post on the forums: https://forums.adafruit.com/viewtopic.php?p=944133

I added a try/except in iot_mqtt.py and device_registration.py for importing SSL

try: import ssl except ImportError: pass

And that at least gets past the SSL import error. However, in device_registration.py, during self._mqtt = MQTT.MQTT() SSL is called with ssl_context=ssl.create_default_context(). I added an additional try/except for that:

try: self._mqtt = MQTT.MQTT( broker=constants.DPS_END_POINT, username=username, password=auth_string, port=8883, keep_alive=120, client_id=self._device_id, ssl_context=ssl.create_default_context(), ) except NameError: self._mqtt = MQTT.MQTT( broker=constants.DPS_END_POINT, username=username, password=auth_string, port=8883, keep_alive=120, is_ssl=True, client_id=self._device_id, )

And the PyPortal does attempt a connection with Azure. However, it seems to lock up at this step:

`744.949: DEBUG - Receiving SUBSCRIBE Topic: $dps/registrations/res/202/?$rid=2bz2xdk3z22&retry-after=3 Msg: b'{"operationId":"5.7142ef3ec990ecb3.6d658d4c-d67a-4863-bb8b-7626a3e5459b","status":"assigning"}'

745.141: INFO - Received registration results on topic $dps/registrations/res/202/?$rid=2bz2xdk3z22&retry-after=3 - {"operationId":"5.7142ef3ec990ecb3.6d658d4c-d67a-4863-bb8b-7626a3e5459b","status":"assigning"} 745.146: DEBUG - Retrying after 3s`

and does not retry after three seconds. When I do a KeyboardInterrupt, this is the printout to the REPL:

`Traceback (most recent call last): File "code.py", line 110, in File "/lib/adafruit_azureiot/iotcentral_device.py", line 159, in connect File "/lib/adafruit_azureiot/device_registration.py", line 217, in register_device File "/lib/adafruit_azureiot/device_registration.py", line 144, in _start_registration File "adafruit_minimqtt/adafruit_minimqtt.py", line 865, in loop File "adafruit_minimqtt/adafruit_minimqtt.py", line 890, in _wait_for_msg File "adafruit_minimqtt/adafruit_minimqtt.py", line 984, in _sock_exact_recv File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 138, in recv File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 210, in available File "adafruit_esp32spi/adafruit_esp32spi.py", line 776, in socket_available File "adafruit_esp32spi/adafruit_esp32spi.py", line 331, in _send_command_get_response File "adafruit_esp32spi/adafruit_esp32spi.py", line 238, in _send_command File "adafruit_esp32spi/adafruit_esp32spi.py", line 195, in _wait_for_ready KeyboardInterrupt:

Code done running.`

It seems like maybe something is locking up in esp32spi or perhaps the MQTT() function needs to be modified differently in device_registration.py?

BlitzCityDIY commented 1 year ago

hihi @dhalbert - this is the issue I was talking about last night in the meeting