Skip to content

Commit 6882e51

Browse files
committed
Added capability to use multiple access points
1 parent 6fc889d commit 6882e51

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
2121

2222

23+
# pylint: disable=too-many-instance-attributes
2324
class ESPSPI_WiFiManager:
2425
"""
2526
A class to help manage the Wifi connection
@@ -57,6 +58,7 @@ def __init__(
5758
requests.set_socket(socket, esp)
5859
self.statuspix = status_pixel
5960
self.pixel_status(0)
61+
self._ap_index = 0
6062

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

108+
def _get_next_ap(self):
109+
if isinstance(self.ssid, (tuple, list)) and isinstance(
110+
self.password, (tuple, list)
111+
):
112+
if not self.ssid or not self.password:
113+
raise ValueError("SSID and Password should contain at least 1 value")
114+
if len(self.ssid) != len(self.password):
115+
raise ValueError("The length of SSIDs and Passwords should match")
116+
access_point = (self.ssid[self._ap_index], self.password[self._ap_index])
117+
self._ap_index += 1
118+
if self._ap_index >= len(self.ssid):
119+
self._ap_index = 0
120+
return access_point
121+
if isinstance(self.ssid, (tuple, list)) or isinstance(
122+
self.password, (tuple, list)
123+
):
124+
raise NotImplementedError(
125+
"If using multiple passwords, both SSID and Password should be lists or tuples"
126+
)
127+
return (self.ssid, self.password)
128+
106129
def connect_normal(self):
107130
"""
108131
Attempt a regular style WiFi connection
109132
"""
110133
failure_count = 0
134+
(ssid, password) = self._get_next_ap()
111135
while not self.esp.is_connected:
112136
try:
113137
if self.debug:
114138
print("Connecting to AP...")
115139
self.pixel_status((100, 0, 0))
116-
self.esp.connect_AP(
117-
bytes(self.ssid, "utf-8"), bytes(self.password, "utf-8")
118-
)
140+
self.esp.connect_AP(bytes(ssid, "utf-8"), bytes(password, "utf-8"))
119141
failure_count = 0
120142
self.pixel_status((0, 100, 0))
121143
except (ValueError, RuntimeError) as error:
122144
print("Failed to connect, retrying\n", error)
123145
failure_count += 1
124146
if failure_count >= self.attempts:
125147
failure_count = 0
148+
(ssid, password) = self._get_next_ap()
126149
self.reset()
127150
continue
128151

0 commit comments

Comments
 (0)