@@ -39,18 +39,20 @@ class ESPSPI_WiFiManager:
39
39
"""
40
40
A class to help manage the Wifi connection
41
41
"""
42
- def __init__ (self , esp , settings , status_neopixel = None ):
42
+ def __init__ (self , esp , settings , attempts = 3 , status_neopixel = None ):
43
43
"""
44
44
:param ESP_SPIcontrol esp: The ESP object we are using
45
45
: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)
47
48
:type status_neopixel: Pin
48
49
"""
49
50
# Read the settings
50
51
self ._esp = esp
51
52
self .debug = False
52
53
self .ssid = settings ['ssid' ]
53
54
self .password = settings ['password' ]
55
+ self .attempts = 3
54
56
requests .set_interface (self ._esp )
55
57
if status_neopixel :
56
58
self .neopix = neopixel .NeoPixel (status_neopixel , 1 , brightness = 0.2 )
@@ -69,15 +71,22 @@ def connect(self):
69
71
print ("MAC addr:" , [hex (i ) for i in self ._esp .MAC_address ])
70
72
for access_pt in self ._esp .scan_networks ():
71
73
print ("\t %s\t \t RSSI: %d" % (str (access_pt ['ssid' ], 'utf-8' ), access_pt ['rssi' ]))
74
+ failure_count = 0
72
75
while not self ._esp .is_connected :
73
76
try :
74
77
if self .debug :
75
78
print ("Connecting to AP..." )
76
79
self .neo_status ((100 , 0 , 0 ))
77
80
self ._esp .connect_AP (bytes (self .ssid , 'utf-8' ), bytes (self .password , 'utf-8' ))
81
+ failure_count = 0
78
82
self .neo_status ((0 , 100 , 0 ))
79
83
except (ValueError , RuntimeError ) as error :
80
84
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 )
81
90
continue
82
91
83
92
def get (self , url , ** kw ):
0 commit comments