Can't connect Arduino Nano RP2040 Connect to Azure IoT HUB

I have followed multiple tutorials to try to connect to Azure IoT Hub with my Nano RP2040 Connect and I keep getting stuck on this error:

256.705: INFO - - iot_mqtt :: connect :: redacted123.azure-devices.net
256.711: DEBUG - - iot_mqtt :: _on_connect :: username = redacted123.azure-devices.net/MyArduinoConnect/?api-version=2019-10-01, password = SharedAccessSignature sr=myiothub.azure-devices.net%2Fdevices%2Fredacted123ArduinoConnect&sig=OKeIy1cgk%2FQ9ok%2BiO359CWEBWDH1erL1lzQiZP7rwbk%3D&se=1637119964
256.719: DEBUG - Attempting to establish MQTT connection...
256.725: INFO - Establishing a SECURE SSL connection to redacted123iothub.azure-devices.net:8883
Connection error, reconnecting
    Timed out waiting for SPI char

I have this firmware: adafruit-circuitpython-arduino_nano_rp2040_connect-sv-7.0.0
I have these libs: adafruit-circuitpython-bundle-7.x-mpy-20211116

And I'm using this code, which is as small as I can make it to reproduce:

import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi

# azure imports
from adafruit_azureiot import IoTHubDevice  # pylint: disable=wrong-import-position
from adafruit_ntp import NTP

from secrets import secrets

#  ESP32 pins
esp32_cs = DigitalInOut(board.CS1)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

#  uses the secondary SPI connected through the ESP32
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
    

print("Connecting to WiFi.")

requests.set_socket(socket, esp)
esp.connect_AP(secrets["ssid"], secrets["password"])

print("Connected: ", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
print("IP: ", esp.pretty_ip(esp.ip_address))

ntp = NTP(esp)

# Wait for a valid time to be received
while not ntp.valid_time:
    print("waiting for NTP Time")
    time.sleep(5)
    ntp.set_time()

print("Wifi connected.")

device = IoTHubDevice(socket, esp, secrets["azure_device_connection_string"])

attempts = 0
while not device.is_connected() and attempts < 3:
    attempts = attempts + 1
    print("Connecting to Azure: #" + str(attempts));
    try:
        print("begin")
        device.connect()
        print("SUCCESSS!")
    except BaseException as e:
        print("Connection error, reconnecting\n", str(e))
        time.sleep(2)
        
        # need to reset this or self.device.is_connected() will explode on loop.
        device = IoTHubDevice(socket, esp, secrets["azure_device_connection_string"])

    if(attempts>=3):
        print("failed to reach the cloud.")
        time.sleep(60) # sleep for a minute before loops start over.
    else:
        print("Connected to cloud")

I never manage to connect to the Azure IoT Hub. Always the same SPI Error.
The error comes from this line: device.connect()

I'd appreciate any help; keep in mind I'm very new to Arduino, CircuitPython and IoT.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.