adafruit / adafruit/Adafruit_CircuitPython_AzureIoT

RuntimeError: Repeated socket failures on ESP32-S2

Open
#44 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
19
Forks
13
PR merge metrics
No merged PRs in 30d

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 https://github.com/adafruit/Adafruit_CircuitPython_Requests/issues/62#issuecomment-865173242
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()

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with azureiot_central_simpletest.py, especially after the soft reset, and start at the referenced adafruit_minimqtt.py location where socket creation fails. Inspect the reported SSL handshake and ESP TLS errors alongside socket reuse, then define done as identifying the reset-related cause and making the requests/MQTT sequence reliable or documenting the required handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, python
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.