Description
Hello, I have a MatrixPortal M4 with an esp32 getting some requests from the internet and drawing text and a bitmap graph based on the response onto a matrix led screen.
After an irregular amount of time it times out (sometimes 1 hour, 3 hours, longest was 7 hours)
I'd like to know what I can do to try and reset and retry the connection when it does freeze. This is my code for the request:
def grab_data():
retry_count = 5
for attempt in range(retry_count):
try:
log_message("Calling grab data")
print("ESP32 is connected to the INTERNET:", str(esp.is_connected))
socket_status() # tells status of all sockets
response = requests.get(url_list)
data_website = response.json()
response.close() # Close response to free up memory
del response
gc.collect() # Explicitly call garbage collection
log_message("Returned grab data")
socket_status()
return data_website
except Exception as e:
log_message("ERROR!!!!!!! grabbing data (attempt {} of {})".format(attempt + 1, retry_count))
traceback.print_exception(e)
for x in range(8):
if esp.socket_status(x) != 0:
log_message(f"closing socket number {x}")
esp.socket_close(x)
esp.reset() # added reset
esp.disconnect()
print("disconnect from the internet")
print("ESP32 is connected to the INTERNET:", str(esp.is_connected))
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))
time.sleep(10) # Wait before retrying
These are the type of errors I get, the first error will always be some sort of time out:
Traceback (most recent call last):
File "code.py", line 175, in grab_data
File "adafruit_requests.py", line 591, in get
File "adafruit_requests.py", line 537, in request
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 98, in recv
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 143, in recv_into
timeout: timed out
or
File "code.py", line 175, in grab_data
File "adafruit_requests.py", line 591, in get
File "adafruit_requests.py", line 537, in request
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 98, in recv
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 129, in recv_into
File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 155, in _available
File "adafruit_esp32spi/adafruit_esp32spi.py", line 789, in socket_available
File "adafruit_esp32spi/adafruit_esp32spi.py", line 341, in _send_command_get_response
File "adafruit_esp32spi/adafruit_esp32spi.py", line 308, in _wait_response_cmd
File "adafruit_esp32spi/adafruit_esp32spi.py", line 287, in _wait_spi_char
TimeoutError: Timed out waiting for SPI char
After that you'll see my code attempts to reset connection and retry. It'll either get the same error again or it will give me this upon 2nd and further retries:
Traceback (most recent call last):
File "code.py", line 175, in grab_data
File "adafruit_requests.py", line 591, in get
File "adafruit_requests.py", line 525, in request
File "adafruit_connection_manager.py", line 226, in get_socket
RuntimeError: Socket already connected to http://pythonvfxer.pythonanywhere.com:80
I added some socket management to my code and I'm trying to close all connected sockets before it tries the loop again as you can see. This does nothing, on further retries it just keeps saying the socket is already connected, but when I query all sockets they all return 0.
Thanks in advance, I'm new to all of this and just tinkering, punching way above my weightclass I just want a way to reset all connections and sockets and let the esp32 try and connect again if it times out. I appreciate no network connection is always stable.
Running all the latest available as of today:
Firmware vers. 1.7.7
Circuit python 9.0.5
I updated the adafruit_esp32spi library based on this github too.