Skip to content

Added capability to use multiple access points #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import adafruit_esp32spi.adafruit_esp32spi_socket as socket


# pylint: disable=too-many-instance-attributes
class ESPSPI_WiFiManager:
"""
A class to help manage the Wifi connection
Expand Down Expand Up @@ -57,6 +58,7 @@ def __init__(
requests.set_socket(socket, esp)
self.statuspix = status_pixel
self.pixel_status(0)
self._ap_index = 0

# Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
if secrets.get("ent_ssid"):
Expand Down Expand Up @@ -103,26 +105,47 @@ def connect(self):
else:
raise TypeError("Invalid WiFi connection type specified")

def _get_next_ap(self):
if isinstance(self.ssid, (tuple, list)) and isinstance(
self.password, (tuple, list)
):
if not self.ssid or not self.password:
raise ValueError("SSID and Password should contain at least 1 value")
if len(self.ssid) != len(self.password):
raise ValueError("The length of SSIDs and Passwords should match")
access_point = (self.ssid[self._ap_index], self.password[self._ap_index])
self._ap_index += 1
if self._ap_index >= len(self.ssid):
self._ap_index = 0
return access_point
if isinstance(self.ssid, (tuple, list)) or isinstance(
self.password, (tuple, list)
):
raise NotImplementedError(
"If using multiple passwords, both SSID and Password should be lists or tuples"
)
return (self.ssid, self.password)

def connect_normal(self):
"""
Attempt a regular style WiFi connection
"""
failure_count = 0
(ssid, password) = self._get_next_ap()
while not self.esp.is_connected:
try:
if self.debug:
print("Connecting to AP...")
self.pixel_status((100, 0, 0))
self.esp.connect_AP(
bytes(self.ssid, "utf-8"), bytes(self.password, "utf-8")
)
self.esp.connect_AP(bytes(ssid, "utf-8"), bytes(password, "utf-8"))
failure_count = 0
self.pixel_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
(ssid, password) = self._get_next_ap()
self.reset()
continue

Expand Down