Skip to content

Commit

Permalink
Merge pull request #6 from makermelissa/master
Browse files Browse the repository at this point in the history
I added a reset after a certain number of connection attempts.
  • Loading branch information
makermelissa authored Feb 19, 2019
2 parents ebcb723 + 2bd2a3b commit 7d37fe9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
33 changes: 24 additions & 9 deletions adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,35 @@ class ESPSPI_WiFiManager:
"""
A class to help manage the Wifi connection
"""
def __init__(self, esp, settings, status_neopixel=None):
def __init__(self, esp, settings, status_neopixel=None, attempts=2):
"""
:param ESP_SPIcontrol esp: The ESP object we are using
:param dict settings: The WiFi and Adafruit IO Settings (See examples)
:param status_neopixel: (Pptional) The neopixel pin - Usually board.NEOPIXEL (default=None)
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
:param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None)
:type status_neopixel: Pin
"""
# Read the settings
self._esp = esp
self.debug = False
self.ssid = settings['ssid']
self.password = settings['password']
self.attempts = attempts
requests.set_interface(self._esp)
if status_neopixel:
self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2)
else:
self.neopix = None
self.neo_status(0)

def reset(self):
"""
Perform a hard reset on the ESP32
"""
if self.debug:
print("Resetting ESP32")
self._esp.reset()

def connect(self):
"""
Attempt to connect to WiFi using the current settings
Expand All @@ -69,15 +79,21 @@ def connect(self):
print("MAC addr:", [hex(i) for i in self._esp.MAC_address])
for access_pt in self._esp.scan_networks():
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
failure_count = 0
while not self._esp.is_connected:
try:
if self.debug:
print("Connecting to AP...")
self.neo_status((100, 0, 0))
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
failure_count = 0
self.neo_status((0, 100, 0))
except (ValueError, RuntimeError) as error:
print("Failed to connect, retrying\n", error)
failure_count += 1
if failure_count >= self.attempts:
failure_count = 0
self.reset()
continue

def get(self, url, **kw):
Expand All @@ -94,7 +110,7 @@ def get(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((100, 100, 0))
self.neo_status((0, 0, 100))
return_val = requests.get(url, **kw)
self.neo_status(0)
return return_val
Expand All @@ -113,9 +129,8 @@ def post(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((100, 100, 0))
self.neo_status((0, 0, 100))
return_val = requests.post(url, **kw)
self.neo_status(0)
return return_val

def put(self, url, **kw):
Expand All @@ -132,7 +147,7 @@ def put(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((100, 100, 0))
self.neo_status((0, 0, 100))
return_val = requests.put(url, **kw)
self.neo_status(0)
return return_val
Expand All @@ -151,7 +166,7 @@ def patch(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((100, 100, 0))
self.neo_status((0, 0, 100))
return_val = requests.patch(url, **kw)
self.neo_status(0)
return return_val
Expand All @@ -170,7 +185,7 @@ def delete(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((100, 100, 0))
self.neo_status((0, 0, 100))
return_val = requests.delete(url, **kw)
self.neo_status(0)
return return_val
Expand All @@ -186,7 +201,7 @@ def ping(self, host, ttl=250):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((100, 100, 0))
self.neo_status((0, 0, 100))
response_time = self._esp.ping(host, ttl=ttl)
self.neo_status(0)
return response_time
Expand Down
1 change: 1 addition & 0 deletions examples/esp32spi_aio_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
print("OK")
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
continue
response = None
time.sleep(15)
1 change: 1 addition & 0 deletions examples/esp32spi_cheerlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
response.close()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
continue

if not value:
Expand Down

0 comments on commit 7d37fe9

Please sign in to comment.