vladak / shield

code for ESP32 Adafruit Feather to get various sensor readings and publish them to MQTT broker
2 stars 0 forks source link

workaround wifi connect problems #13

Closed vladak closed 1 year ago

vladak commented 1 year ago

One of the QtPy's (ESP32 based with uFL and external antenna) running the code from this repository enters a state of exception (blinking the red diode twice every 5 seconds or so) after running for a day or so. Attaching a terminal reveals this:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24458.305: INFO - Running
24458.313: INFO - No library for the tmp117 sensor
24458.313: INFO - No library for the sht40 sensor
24458.445: INFO - No library for battery gauge (max17048)
24458.445: INFO - Connecting to wifi
24462.563: INFO - Connected to Foo
24462.563: INFO - Attempting to connect to MQTT broker 172.40.0.3:1883
24463.242: INFO - Connected to MQTT Broker!
24463.250: INFO - Temperature: 22.7 C
24463.258: INFO - Published to logs/foo/qtpy with PID 0
24463.258: INFO - Humidity: 53.2 %
24463.266: INFO - Published to logs/foo/qtpy with PID 0
24463.266: INFO - Publishing to devices/foo/qtpy
24463.273: INFO - Published to logs/foo/qtpy with PID 0
24463.273: INFO - Published to devices/foo/qtpy with PID 0
24463.281: INFO - Disconnected from MQTT Broker!
24463.281: INFO - Going to light sleep for 10 seconds
24473.281: INFO - Going to deep sleep for 60 seconds

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.
Pretending to deep sleep until alarm, CTRL-C or file write.
Woken up by alarm.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24533.914: INFO - Running
24533.922: INFO - No library for the tmp117 sensor
24533.922: INFO - No library for the sht40 sensor
24534.055: INFO - No library for battery gauge (max17048)
24534.055: INFO - Connecting to wifi
24539.063: INFO - Connected to Foo
24539.070: INFO - Attempting to connect to MQTT broker 172.40.0.3:1883
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 131, in main\r\n  File "adafruit_minimqtt/adafruit_minimqtt.py", line 561, in connect\r\nMMQTTException: No data received from broker for 10 seconds.\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24560.820: INFO - Running
24560.820: INFO - No library for the tmp117 sensor
24560.820: INFO - No library for the sht40 sensor
24560.953: INFO - No library for battery gauge (max17048)
24560.953: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24574.055: INFO - Running
24574.063: INFO - No library for the tmp117 sensor
24574.063: INFO - No library for the sht40 sensor
24574.195: INFO - No library for battery gauge (max17048)
24574.195: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24587.297: INFO - Running
24587.297: INFO - No library for the tmp117 sensor
24587.305: INFO - No library for the sht40 sensor
24587.430: INFO - No library for battery gauge (max17048)
24587.438: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24600.547: INFO - Running
24600.547: INFO - No library for the tmp117 sensor
24600.555: INFO - No library for the sht40 sensor
24600.680: INFO - No library for battery gauge (max17048)
24600.688: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24613.789: INFO - Running
24613.789: INFO - No library for the tmp117 sensor
24613.789: INFO - No library for the sht40 sensor
24613.922: INFO - No library for battery gauge (max17048)
24613.922: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24627.023: INFO - Running
24627.031: INFO - No library for the tmp117 sensor
24627.031: INFO - No library for the sht40 sensor
24627.164: INFO - No library for battery gauge (max17048)
24627.164: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24640.266: INFO - Running
24640.266: INFO - No library for the tmp117 sensor
24640.273: INFO - No library for the sht40 sensor
24640.406: INFO - No library for battery gauge (max17048)
24640.406: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24653.508: INFO - Running
24653.508: INFO - No library for the tmp117 sensor
24653.508: INFO - No library for the sht40 sensor
24653.656: INFO - No library for battery gauge (max17048)
24653.656: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24666.766: INFO - Running
24666.766: INFO - No library for the tmp117 sensor
24666.773: INFO - No library for the sht40 sensor
24666.906: INFO - No library for battery gauge (max17048)
24666.906: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24680.008: INFO - Running
24680.008: INFO - No library for the tmp117 sensor
24680.008: INFO - No library for the sht40 sensor
24680.141: INFO - No library for battery gauge (max17048)
24680.148: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24693.242: INFO - Running
24693.250: INFO - No library for the tmp117 sensor
24693.250: INFO - No library for the sht40 sensor
24693.383: INFO - No library for battery gauge (max17048)
24693.383: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24706.484: INFO - Running
24706.492: INFO - No library for the tmp117 sensor
24706.492: INFO - No library for the sht40 sensor
24706.617: INFO - No library for battery gauge (max17048)
24706.625: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24719.734: INFO - Running
24719.742: INFO - No library for the tmp117 sensor
24719.742: INFO - No library for the sht40 sensor
24719.875: INFO - No library for battery gauge (max17048)
24719.875: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24732.977: INFO - Running
24732.977: INFO - No library for the tmp117 sensor
24732.984: INFO - No library for the sht40 sensor
24733.117: INFO - No library for battery gauge (max17048)
24733.125: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24746.227: INFO - Running
24746.227: INFO - No library for the tmp117 sensor
24746.234: INFO - No library for the sht40 sensor
24746.375: INFO - No library for battery gauge (max17048)
24746.375: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24759.477: INFO - Running
24759.477: INFO - No library for the tmp117 sensor
24759.484: INFO - No library for the sht40 sensor
24759.609: INFO - No library for battery gauge (max17048)
24759.617: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24772.719: INFO - Running
24772.719: INFO - No library for the tmp117 sensor
24772.719: INFO - No library for the sht40 sensor
24772.852: INFO - No library for battery gauge (max17048)
24772.852: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24785.953: INFO - Running
24785.961: INFO - No library for the tmp117 sensor
24785.961: INFO - No library for the sht40 sensor
24786.094: INFO - No library for battery gauge (max17048)
24786.094: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24799.203: INFO - Running
24799.211: INFO - No library for the tmp117 sensor
24799.211: INFO - No library for the sht40 sensor
24799.352: INFO - No library for battery gauge (max17048)
24799.352: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24812.453: INFO - Running
24812.461: INFO - No library for the tmp117 sensor
24812.461: INFO - No library for the sht40 sensor
24812.594: INFO - No library for battery gauge (max17048)
24812.594: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24825.695: INFO - Running
24825.703: INFO - No library for the tmp117 sensor
24825.703: INFO - No library for the sht40 sensor
24825.836: INFO - No library for battery gauge (max17048)
24825.836: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24838.938: INFO - Running
24838.938: INFO - No library for the tmp117 sensor
24838.938: INFO - No library for the sht40 sensor
24839.070: INFO - No library for battery gauge (max17048)
24839.078: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24852.180: INFO - Running
24852.180: INFO - No library for the tmp117 sensor
24852.180: INFO - No library for the sht40 sensor
24852.313: INFO - No library for battery gauge (max17048)
24852.313: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24865.414: INFO - Running
24865.422: INFO - No library for the tmp117 sensor
24865.422: INFO - No library for the sht40 sensor
24865.555: INFO - No library for battery gauge (max17048)
24865.555: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24878.656: INFO - Running
24878.664: INFO - No library for the tmp117 sensor
24878.664: INFO - No library for the sht40 sensor
24878.797: INFO - No library for battery gauge (max17048)
24878.797: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24891.898: INFO - Running
24891.898: INFO - No library for the tmp117 sensor
24891.898: INFO - No library for the sht40 sensor
24892.031: INFO - No library for battery gauge (max17048)
24892.031: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
24905.141: INFO - Running
24905.141: INFO - No library for the tmp117 sensor
24905.141: INFO - No library for the sht40 sensor
24905.273: INFO - No library for battery gauge (max17048)
24905.273: INFO - Connecting to wifi
Code stopped by unhandled exception:
['Traceback (most recent call last):\r\n  File "code.py", line 179, in <module>\r\n  File "code.py", line 109, in main\r\nConnectionError: No network with that ssid\n']
Performing a supervisor reload in 10 seconds

Code done running.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
  File "code.py", line 30, in <module>
MemoryError: Failed to allocate Wifi memory

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

Adafruit CircuitPython 8.0.0-beta.4 on 2022-10-30; Adafruit QT Py ESP32S2 with ESP32S2
>

So, basically there was a singular case of MMQTTException (due to not receiving any data from the broker for 10 seconds) followed by couple of ConnectionError cases (27 in total) followed by MemoryError when import wifi was executed (this is the line number 30 in code.py).

In the meantime, I broadened the except block in the code via ddebb245e29b2649af4e69a7c098f8021144cc01, however that would have not helped because the exception is raised from an import that is not covered by the try/except blocks.

vladak commented 1 year ago

When catching Exception (such as the ConnectionError that is based on OSError that is based on Exception), the code performs supervisor.reload() and exits the program, so that it can be run again thanks to the auto reload. It seems there is a resource leak (and possibly some other problem because it never recovers even though other microcontrollers can connect to the same wifi network just fine in the mean time) in the microcontroller firmware because after number of these it results in the MemoryError.

One way to deal with this would be to do microcontroller.reset() instead of supervisor.reload() on ConnectionError to avoid the MemoryError later on. Alternatively, add a try/except block around the import wifi statement. The latter would not work too well with the planned logging changes in #12.