Skip to content

Commit 0d7c5b5

Browse files
committed
Adding WPA2 Enterprise support to the WiFi manager library
1 parent 92182ea commit 0d7c5b5

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@ class ESPSPI_WiFiManager:
3838
"""
3939
A class to help manage the Wifi connection
4040
"""
41-
def __init__(self, esp, secrets, status_pixel=None, attempts=2):
41+
def __init__(self, esp, secrets, status_pixel=None, attempts=2, con_type=1):
4242
"""
4343
:param ESP_SPIcontrol esp: The ESP object we are using
4444
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
4545
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
4646
or RGB LED (default=None)
4747
:type status_pixel: NeoPixel, DotStar, or RGB LED
4848
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
49+
:param int con_type: (Optional) Type of WiFi connection to make: normal=1, WPA2 Enterprise=2
4950
"""
5051
# Read the settings
5152
self._esp = esp
5253
self.debug = False
5354
self.ssid = secrets['ssid']
5455
self.password = secrets['password']
56+
self.ent_ssid = secrets['ent_ssid']
57+
self.ent_ident = secrets['ent_ident']
58+
self.ent_user = secrets['ent_user']
59+
self.ent_passwd = secrets['ent_passwd']
5560
self.attempts = attempts
61+
self.con_type = con_type
5662
requests.set_interface(self._esp)
5763
self.statuspix = status_pixel
5864
self.pixel_status(0)
@@ -77,21 +83,45 @@ def connect(self):
7783
for access_pt in self._esp.scan_networks():
7884
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
7985
failure_count = 0
80-
while not self._esp.is_connected:
81-
try:
82-
if self.debug:
83-
print("Connecting to AP...")
84-
self.pixel_status((100, 0, 0))
85-
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
86-
failure_count = 0
87-
self.pixel_status((0, 100, 0))
88-
except (ValueError, RuntimeError) as error:
89-
print("Failed to connect, retrying\n", error)
90-
failure_count += 1
91-
if failure_count >= self.attempts:
86+
if self.con_type == 1:
87+
while not self._esp.is_connected:
88+
try:
89+
if self.debug:
90+
print("Connecting to AP...")
91+
self.pixel_status((100, 0, 0))
92+
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
9293
failure_count = 0
93-
self.reset()
94-
continue
94+
self.pixel_status((0, 100, 0))
95+
except (ValueError, RuntimeError) as error:
96+
print("Failed to connect, retrying\n", error)
97+
failure_count += 1
98+
if failure_count >= self.attempts:
99+
failure_count = 0
100+
self.reset()
101+
continue
102+
elif self.con_type == 2:
103+
self._esp.wifi_set_network(bytes(self.ent_ssid, 'utf-8'))
104+
self._esp.wifi_set_entidentity(bytes(self.ent_ident, 'utf-8'))
105+
self._esp.wifi_set_entusername(bytes(self.ent_user, 'utf-8'))
106+
self._esp.wifi_set_entpassword(bytes(self.ent_passwd, 'utf-8'))
107+
self._esp.wifi_set_entenable()
108+
while not self._esp.is_connected:
109+
try:
110+
if self.debug:
111+
print("Connecting to WPA2 Enterprise AP...")
112+
self.pixel_status((100, 0, 0))
113+
failure_count = 0
114+
self.pixel_status((0, 100, 0))
115+
except (ValueError, RuntimeError) as error:
116+
print("Failed to connect, retrying\n", error)
117+
failure_count += 1
118+
if failure_count >= self.attempts:
119+
failure_count = 0
120+
self.reset()
121+
continue
122+
else:
123+
print("Invalid connection type! Exiting...")
124+
exit(1)
95125

96126
def get(self, url, **kw):
97127
"""

0 commit comments

Comments
 (0)