@@ -39,25 +39,35 @@ 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 , status_neopixel = None , attempts = 2 ):
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 int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
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 = attempts
54
56
requests .set_interface (self ._esp )
55
57
if status_neopixel :
56
58
self .neopix = neopixel .NeoPixel (status_neopixel , 1 , brightness = 0.2 )
57
59
else :
58
60
self .neopix = None
59
61
self .neo_status (0 )
60
62
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
+
61
71
def connect (self ):
62
72
"""
63
73
Attempt to connect to WiFi using the current settings
@@ -69,15 +79,21 @@ def connect(self):
69
79
print ("MAC addr:" , [hex (i ) for i in self ._esp .MAC_address ])
70
80
for access_pt in self ._esp .scan_networks ():
71
81
print ("\t %s\t \t RSSI: %d" % (str (access_pt ['ssid' ], 'utf-8' ), access_pt ['rssi' ]))
82
+ failure_count = 0
72
83
while not self ._esp .is_connected :
73
84
try :
74
85
if self .debug :
75
86
print ("Connecting to AP..." )
76
87
self .neo_status ((100 , 0 , 0 ))
77
88
self ._esp .connect_AP (bytes (self .ssid , 'utf-8' ), bytes (self .password , 'utf-8' ))
89
+ failure_count = 0
78
90
self .neo_status ((0 , 100 , 0 ))
79
91
except (ValueError , RuntimeError ) as error :
80
92
print ("Failed to connect, retrying\n " , error )
93
+ failure_count += 1
94
+ if failure_count >= self .attempts :
95
+ failure_count = 0
96
+ self .reset ()
81
97
continue
82
98
83
99
def get (self , url , ** kw ):
@@ -94,7 +110,7 @@ def get(self, url, **kw):
94
110
"""
95
111
if not self ._esp .is_connected :
96
112
self .connect ()
97
- self .neo_status ((100 , 100 , 0 ))
113
+ self .neo_status ((0 , 0 , 100 ))
98
114
return_val = requests .get (url , ** kw )
99
115
self .neo_status (0 )
100
116
return return_val
@@ -113,9 +129,8 @@ def post(self, url, **kw):
113
129
"""
114
130
if not self ._esp .is_connected :
115
131
self .connect ()
116
- self .neo_status ((100 , 100 , 0 ))
132
+ self .neo_status ((0 , 0 , 100 ))
117
133
return_val = requests .post (url , ** kw )
118
- self .neo_status (0 )
119
134
return return_val
120
135
121
136
def put (self , url , ** kw ):
@@ -132,7 +147,7 @@ def put(self, url, **kw):
132
147
"""
133
148
if not self ._esp .is_connected :
134
149
self .connect ()
135
- self .neo_status ((100 , 100 , 0 ))
150
+ self .neo_status ((0 , 0 , 100 ))
136
151
return_val = requests .put (url , ** kw )
137
152
self .neo_status (0 )
138
153
return return_val
@@ -151,7 +166,7 @@ def patch(self, url, **kw):
151
166
"""
152
167
if not self ._esp .is_connected :
153
168
self .connect ()
154
- self .neo_status ((100 , 100 , 0 ))
169
+ self .neo_status ((0 , 0 , 100 ))
155
170
return_val = requests .patch (url , ** kw )
156
171
self .neo_status (0 )
157
172
return return_val
@@ -170,7 +185,7 @@ def delete(self, url, **kw):
170
185
"""
171
186
if not self ._esp .is_connected :
172
187
self .connect ()
173
- self .neo_status ((100 , 100 , 0 ))
188
+ self .neo_status ((0 , 0 , 100 ))
174
189
return_val = requests .delete (url , ** kw )
175
190
self .neo_status (0 )
176
191
return return_val
@@ -186,7 +201,7 @@ def ping(self, host, ttl=250):
186
201
"""
187
202
if not self ._esp .is_connected :
188
203
self .connect ()
189
- self .neo_status ((100 , 100 , 0 ))
204
+ self .neo_status ((0 , 0 , 100 ))
190
205
response_time = self ._esp .ping (host , ttl = ttl )
191
206
self .neo_status (0 )
192
207
return response_time
0 commit comments