-
Notifications
You must be signed in to change notification settings - Fork 13
Description
I realise Adafruit folks are working on this library at the moment, so perhaps this will work itself out as part of that.
However, I've been running into issues when trying to use IoTCentralDevice.
azureiot_central_simpletest.py does work for me
However if I remove the if statement if time.localtime().tm_year < 2022:
then the code runs once, but fails after a soft reset or reload.
A hard reset typically gets it going again, but the behaviour doesn't seem to be 100% repeatable.
I'd like to understand why, so I can use requests and MQTT reliably in the same code.
(I've run into similar weirdness when using AdafruitIO, which was my motivation to try Azure in the first place, but I think it could be a more fundamental issue with sockets in circuitpython / ESP32-S2)
I wonder if it is related to Anecdata's comment about ESP32-S2 only supporting 4 sockets adafruit/Adafruit_CircuitPython_Requests#62 (comment)
Perhaps after a soft reset the sockets aren't being cleared... somehow!
Digging into the libraries a little bit shows me the error occurs here:
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/blob/c4517bbbde70e2dc7fdb1a1fcfe120d1b6b2f530/adafruit_minimqtt/adafruit_minimqtt.py#L279
adding some print()s reveals that it fails to get a socket because of:
Failed SSL handshake
Unhandled ESP TLS error 0 0 8004 -1
Unhandled ESP TLS error 0 0 8004 -1
Unhandled ESP TLS error 0 0 8004 -1
Unhandled ESP TLS error 0 0 8004 -1
Would be great to understand what is going on so we can avoid it or handle it better.
My test code, slightly modified from the simpletest :
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import ssl
import time
import rtc
import socketpool
import wifi
import adafruit_requests
from adafruit_azureiot import IoTCentralDevice
import supervisor
from secrets import secrets
print("Connecting to WiFi...")
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to WiFi!")
# if time.localtime().tm_year < 2022:
# print("Setting System Time in UTC")
# pool = socketpool.SocketPool(wifi.radio)
# requests = adafruit_requests.Session(pool, ssl.create_default_context())
# response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
# if response:
# if response.status_code == 200:
# r = rtc.RTC()
# r.datetime = time.localtime(int(response.text))
# print(f"System Time: {r.datetime}")
# else:
# print("Setting time failed")
# else:
# print("Year seems good, skipping set time.")
print("Setting System Time in UTC")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
if response:
if response.status_code == 200:
r = rtc.RTC()
r.datetime = time.localtime(int(response.text))
print(f"System Time: {r.datetime}")
# Create an IoT Hub device client and connect
esp = None
# pool = socketpool.SocketPool(wifi.radio)
device = IoTCentralDevice(
pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"]
)
print("Connecting to Azure IoT Central...")
device.connect()
print("Connected to Azure IoT Central!")
supervisor.reload()