|
20 | 20 | import adafruit_esp32spi.adafruit_esp32spi_socket as socket
|
21 | 21 |
|
22 | 22 |
|
| 23 | +# pylint: disable=too-many-instance-attributes |
23 | 24 | class ESPSPI_WiFiManager:
|
24 | 25 | """
|
25 | 26 | A class to help manage the Wifi connection
|
@@ -57,6 +58,7 @@ def __init__(
|
57 | 58 | requests.set_socket(socket, esp)
|
58 | 59 | self.statuspix = status_pixel
|
59 | 60 | self.pixel_status(0)
|
| 61 | + self._ap_index = 0 |
60 | 62 |
|
61 | 63 | # Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
|
62 | 64 | if secrets.get("ent_ssid"):
|
@@ -103,26 +105,47 @@ def connect(self):
|
103 | 105 | else:
|
104 | 106 | raise TypeError("Invalid WiFi connection type specified")
|
105 | 107 |
|
| 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 | + |
106 | 129 | def connect_normal(self):
|
107 | 130 | """
|
108 | 131 | Attempt a regular style WiFi connection
|
109 | 132 | """
|
110 | 133 | failure_count = 0
|
| 134 | + (ssid, password) = self._get_next_ap() |
111 | 135 | while not self.esp.is_connected:
|
112 | 136 | try:
|
113 | 137 | if self.debug:
|
114 | 138 | print("Connecting to AP...")
|
115 | 139 | 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")) |
119 | 141 | failure_count = 0
|
120 | 142 | self.pixel_status((0, 100, 0))
|
121 | 143 | except (ValueError, RuntimeError) as error:
|
122 | 144 | print("Failed to connect, retrying\n", error)
|
123 | 145 | failure_count += 1
|
124 | 146 | if failure_count >= self.attempts:
|
125 | 147 | failure_count = 0
|
| 148 | + (ssid, password) = self._get_next_ap() |
126 | 149 | self.reset()
|
127 | 150 | continue
|
128 | 151 |
|
|
0 commit comments