adafruit / Adafruit_CircuitPython_AzureIoT

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

I can't connect to Azure IoT Hub -> Timed out waiting for SPI char #36

Closed SyntaxWarrior closed 2 years ago

SyntaxWarrior commented 2 years ago

Im using an Arduino Nano RP2040 Connect .

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

I keep getting this error: Timed out waiting for SPI char

Full debug log from my little program (keys have been edited along with urls).

Connecting to WiFi.
Connected:  SKYNET  RSSI: -61
IP:  10.10.10.106
Wifi connected.
waiting for NTP Time
52.215: DEBUG - Hostname: myiothub.azure-devices.net
52.219: DEBUG - Device Id: ArduinoConnect
52.223: DEBUG - Shared Access Key: ca1b11OP+x+cnasrN5Aa5qNZsaYNyIqeaGKaFLd7+OI=
Connecting to Azure: #1
begin
52.808: INFO - - iot_mqtt :: connect :: myiothub.azure-devices.net
52.815: DEBUG - - iot_mqtt :: _on_connect :: username = myiothub.azure-devices.net/ArduinoConnect/?api-version=2019-10-01, password = SharedAccessSignature sr=myiothub.azure-devices.net%2Fdevices%2FArduinoConnect&sig=1Adqlrrc1IGVnA4apwr2L6RBAzooPPlm08SM%2Fik2wWk%3D&se=1637207171
52.821: DEBUG - Attempting to establish MQTT connection...
52.826: INFO - Establishing a SECURE SSL connection to myiothub.azure-devices.net:8883
Connection error, reconnecting
 **Timed out waiting for SPI char**

This is the code that produces this error:

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))

print("Wifi connected.")

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() 

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")
SyntaxWarrior commented 2 years ago

I'm new to the whole MicroPython / Arduino scene, so I hope I'm just doing something simple wrong. Would appreciate any help.

SyntaxWarrior commented 2 years ago

The solution was to upgrade the Nina Wifi firmware as explained here:

https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-upgrading-nina-firmware

tannewt commented 2 years ago

Glad you got it working!