@@ -39,25 +39,35 @@ 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 , status_neopixel = None , attempts = 2 ):
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 int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
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 = attempts
5456 requests .set_interface (self ._esp )
5557 if status_neopixel :
5658 self .neopix = neopixel .NeoPixel (status_neopixel , 1 , brightness = 0.2 )
5759 else :
5860 self .neopix = None
5961 self .neo_status (0 )
6062
63+ def reset (self ):
64+ """
65+ Perform a hard reset on the ESP32
66+ """
67+ if self .debug :
68+ print ("Resetting ESP32" )
69+ self ._esp .reset ()
70+
6171 def connect (self ):
6272 """
6373 Attempt to connect to WiFi using the current settings
@@ -69,15 +79,21 @@ def connect(self):
6979 print ("MAC addr:" , [hex (i ) for i in self ._esp .MAC_address ])
7080 for access_pt in self ._esp .scan_networks ():
7181 print ("\t %s\t \t RSSI: %d" % (str (access_pt ['ssid' ], 'utf-8' ), access_pt ['rssi' ]))
82+ failure_count = 0
7283 while not self ._esp .is_connected :
7384 try :
7485 if self .debug :
7586 print ("Connecting to AP..." )
7687 self .neo_status ((100 , 0 , 0 ))
7788 self ._esp .connect_AP (bytes (self .ssid , 'utf-8' ), bytes (self .password , 'utf-8' ))
89+ failure_count = 0
7890 self .neo_status ((0 , 100 , 0 ))
7991 except (ValueError , RuntimeError ) as error :
8092 print ("Failed to connect, retrying\n " , error )
93+ failure_count += 1
94+ if failure_count >= self .attempts :
95+ failure_count = 0
96+ self .reset ()
8197 continue
8298
8399 def get (self , url , ** kw ):
@@ -94,7 +110,7 @@ def get(self, url, **kw):
94110 """
95111 if not self ._esp .is_connected :
96112 self .connect ()
97- self .neo_status ((100 , 100 , 0 ))
113+ self .neo_status ((0 , 0 , 100 ))
98114 return_val = requests .get (url , ** kw )
99115 self .neo_status (0 )
100116 return return_val
@@ -113,9 +129,8 @@ def post(self, url, **kw):
113129 """
114130 if not self ._esp .is_connected :
115131 self .connect ()
116- self .neo_status ((100 , 100 , 0 ))
132+ self .neo_status ((0 , 0 , 100 ))
117133 return_val = requests .post (url , ** kw )
118- self .neo_status (0 )
119134 return return_val
120135
121136 def put (self , url , ** kw ):
@@ -132,7 +147,7 @@ def put(self, url, **kw):
132147 """
133148 if not self ._esp .is_connected :
134149 self .connect ()
135- self .neo_status ((100 , 100 , 0 ))
150+ self .neo_status ((0 , 0 , 100 ))
136151 return_val = requests .put (url , ** kw )
137152 self .neo_status (0 )
138153 return return_val
@@ -151,7 +166,7 @@ def patch(self, url, **kw):
151166 """
152167 if not self ._esp .is_connected :
153168 self .connect ()
154- self .neo_status ((100 , 100 , 0 ))
169+ self .neo_status ((0 , 0 , 100 ))
155170 return_val = requests .patch (url , ** kw )
156171 self .neo_status (0 )
157172 return return_val
@@ -170,7 +185,7 @@ def delete(self, url, **kw):
170185 """
171186 if not self ._esp .is_connected :
172187 self .connect ()
173- self .neo_status ((100 , 100 , 0 ))
188+ self .neo_status ((0 , 0 , 100 ))
174189 return_val = requests .delete (url , ** kw )
175190 self .neo_status (0 )
176191 return return_val
@@ -186,7 +201,7 @@ def ping(self, host, ttl=250):
186201 """
187202 if not self ._esp .is_connected :
188203 self .connect ()
189- self .neo_status ((100 , 100 , 0 ))
204+ self .neo_status ((0 , 0 , 100 ))
190205 response_time = self ._esp .ping (host , ttl = ttl )
191206 self .neo_status (0 )
192207 return response_time
0 commit comments