Skip to content

Commit b878f96

Browse files
author
Melissa LeBlanc-Williams
committed
Removed attempt code from get/post. Examples reset.
1 parent 73c4a56 commit b878f96

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class ESPSPI_WiFiManager:
3939
"""
4040
A class to help manage the Wifi connection
4141
"""
42-
def __init__(self, esp, settings, status_neopixel=None, attempts=1):
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 int attempts: (Optional) Failed attempts before resetting the ESP32 (default=1)
46+
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
4747
:param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None)
4848
:type status_neopixel: Pin
4949
"""
@@ -60,6 +60,14 @@ def __init__(self, esp, settings, status_neopixel=None, attempts=1):
6060
self.neopix = None
6161
self.neo_status(0)
6262

63+
def reset(self):
64+
"""
65+
Perform a hard reset on the ESP32
66+
"""
67+
if self.debug:
68+
print("Resetting ESP32\n", error)
69+
self._esp.reset()
70+
6371
def connect(self):
6472
"""
6573
Attempt to connect to WiFi using the current settings
@@ -85,8 +93,7 @@ def connect(self):
8593
failure_count += 1
8694
if failure_count >= self.attempts:
8795
failure_count = 0
88-
self._esp.reset()
89-
print("Resetting ESP32\n", error)
96+
self.reset()
9097
continue
9198

9299
def get(self, url, **kw):
@@ -104,16 +111,7 @@ def get(self, url, **kw):
104111
if not self._esp.is_connected:
105112
self.connect()
106113
self.neo_status((100, 100, 0))
107-
attempt_count = 0
108-
while attempt_count < self.attempts:
109-
try:
110-
attempt_count += 1
111-
return_val = requests.get(url, **kw)
112-
except(ValueError, RuntimeError) as error:
113-
if attempt_count >= self.attempts:
114-
attempt_count = 0
115-
self._esp.reset()
116-
print("Resetting ESP32\n", error)
114+
return_val = requests.get(url, **kw)
117115
self.neo_status(0)
118116
return return_val
119117

@@ -132,17 +130,7 @@ def post(self, url, **kw):
132130
if not self._esp.is_connected:
133131
self.connect()
134132
self.neo_status((100, 100, 0))
135-
attempt_count = 0
136-
while attempt_count < self.attempts:
137-
try:
138-
attempt_count += 1
139-
return_val = requests.post(url, **kw)
140-
except(ValueError, RuntimeError) as error:
141-
if attempt_count >= self.attempts:
142-
attempt_count = 0
143-
self._esp.reset()
144-
print("Resetting ESP32\n", error)
145-
self.neo_status(0)
133+
return_val = requests.post(url, **kw)
146134
return return_val
147135

148136
def put(self, url, **kw):

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)