Skip to content

Commit a7b8d4a

Browse files
author
Melissa LeBlanc-Williams
committed
Added reset after a certain number of attempts
1 parent 4685648 commit a7b8d4a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ class ESPSPI_WiFiManager:
3939
"""
4040
A class to help manage the Wifi connection
4141
"""
42-
def __init__(self, esp, settings, status_neopixel=None):
42+
def __init__(self, esp, settings, attempts=3, status_neopixel=None):
4343
"""
4444
:param ESP_SPIcontrol esp: The ESP object we are using
4545
:param dict settings: The WiFi and Adafruit IO Settings (See examples)
46-
:param status_neopixel: (Pptional) The neopixel pin - Usually board.NEOPIXEL (default=None)
46+
:param attempts: (Optional) Failed attempts before resetting the ESP32 (default=3)
47+
:param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None)
4748
:type status_neopixel: Pin
4849
"""
4950
# Read the settings
5051
self._esp = esp
5152
self.debug = False
5253
self.ssid = settings['ssid']
5354
self.password = settings['password']
55+
self.attempts = 3
5456
requests.set_interface(self._esp)
5557
if status_neopixel:
5658
self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2)
@@ -69,15 +71,22 @@ def connect(self):
6971
print("MAC addr:", [hex(i) for i in self._esp.MAC_address])
7072
for access_pt in self._esp.scan_networks():
7173
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
74+
failure_count = 0
7275
while not self._esp.is_connected:
7376
try:
7477
if self.debug:
7578
print("Connecting to AP...")
7679
self.neo_status((100, 0, 0))
7780
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
81+
failure_count = 0
7882
self.neo_status((0, 100, 0))
7983
except (ValueError, RuntimeError) as error:
8084
print("Failed to connect, retrying\n", error)
85+
failure_count += 1
86+
if failure_count >= self.attempts:
87+
failure_count = 0
88+
self._esp.reset()
89+
print("Resetting ESP32\n", error)
8190
continue
8291

8392
def get(self, url, **kw):

0 commit comments

Comments
 (0)