Skip to content

Commit 7d37fe9

Browse files
authored
Merge pull request #6 from makermelissa/master
I added a reset after a certain number of connection attempts.
2 parents ebcb723 + 2bd2a3b commit 7d37fe9

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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\tRSSI: %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

examples/esp32spi_aio_post.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
print("OK")
3939
except (ValueError, RuntimeError) as e:
4040
print("Failed to get data, retrying\n", e)
41+
wifi.reset()
4142
continue
4243
response = None
4344
time.sleep(15)

examples/esp32spi_cheerlights.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
response.close()
4848
except (ValueError, RuntimeError) as e:
4949
print("Failed to get data, retrying\n", e)
50+
wifi.reset()
5051
continue
5152

5253
if not value:

0 commit comments

Comments
 (0)