Skip to content

Commit 1ff8edf

Browse files
committed
Refactored wifimanager connect function into 3 separate functions; refactored loading of WPA2 Enterprise secrets so if they're not defined, we silently move on; tweaked the two WPA2 examples to use the newer way of defining board pins
1 parent 1eca01c commit 1ff8edf

File tree

3 files changed

+87
-58
lines changed

3 files changed

+87
-58
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
# pylint: disable=no-name-in-module
3333

34+
from time import sleep
3435
from micropython import const
3536
from adafruit_esp32spi import adafruit_esp32spi
3637
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
@@ -42,6 +43,7 @@ class ESPSPI_WiFiManager:
4243
NORMAL = const(1)
4344
ENTERPRISE = const(2)
4445

46+
# pylint: disable=too-many-arguments
4547
def __init__(self, esp, secrets, status_pixel=None, attempts=2, connection_type=NORMAL):
4648
"""
4749
:param ESP_SPIcontrol esp: The ESP object we are using
@@ -59,16 +61,27 @@ def __init__(self, esp, secrets, status_pixel=None, attempts=2, connection_type=
5961
self.debug = False
6062
self.ssid = secrets['ssid']
6163
self.password = secrets['password']
62-
self.ent_ssid = secrets['ent_ssid']
63-
self.ent_ident = secrets['ent_ident']
64-
self.ent_user = secrets['ent_user']
65-
self.ent_password = secrets['ent_password']
6664
self.attempts = attempts
6765
self._connection_type = connection_type
6866
requests.set_interface(self._esp)
6967
self.statuspix = status_pixel
7068
self.pixel_status(0)
7169

70+
# Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
71+
if secrets.get('ent_ssid'):
72+
self.ent_ssid = secrets['ent_ssid']
73+
else:
74+
self.ent_ssid = secrets['ssid']
75+
if secrets.get('ent_ident'):
76+
self.ent_ident = secrets['ent_ident']
77+
else:
78+
self.ent_ident = ''
79+
if secrets.get('ent_user'):
80+
self.ent_user = secrets['ent_user']
81+
if secrets.get('ent_password'):
82+
self.ent_password = secrets['ent_password']
83+
# pylint: enable=too-many-arguments
84+
7285
def reset(self):
7386
"""
7487
Perform a hard reset on the ESP32
@@ -88,46 +101,59 @@ def connect(self):
88101
print("MAC addr:", [hex(i) for i in self._esp.MAC_address])
89102
for access_pt in self._esp.scan_networks():
90103
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
91-
failure_count = 0
92104
if self._connection_type == ESPSPI_WiFiManager.NORMAL:
93-
while not self._esp.is_connected:
94-
try:
95-
if self.debug:
96-
print("Connecting to AP...")
97-
self.pixel_status((100, 0, 0))
98-
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
99-
failure_count = 0
100-
self.pixel_status((0, 100, 0))
101-
except (ValueError, RuntimeError) as error:
102-
print("Failed to connect, retrying\n", error)
103-
failure_count += 1
104-
if failure_count >= self.attempts:
105-
failure_count = 0
106-
self.reset()
107-
continue
105+
self.connect_normal()
108106
elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE:
109-
self._esp.wifi_set_network(bytes(self.ent_ssid, 'utf-8'))
110-
self._esp.wifi_set_entidentity(bytes(self.ent_ident, 'utf-8'))
111-
self._esp.wifi_set_entusername(bytes(self.ent_user, 'utf-8'))
112-
self._esp.wifi_set_entpassword(bytes(self.ent_password, 'utf-8'))
113-
self._esp.wifi_set_entenable()
114-
while not self._esp.is_connected:
115-
try:
116-
if self.debug:
117-
print("Connecting to WPA2 Enterprise AP...")
118-
self.pixel_status((100, 0, 0))
119-
failure_count = 0
120-
self.pixel_status((0, 100, 0))
121-
except (ValueError, RuntimeError) as error:
122-
print("Failed to connect, retrying\n", error)
123-
failure_count += 1
124-
if failure_count >= self.attempts:
125-
failure_count = 0
126-
self.reset()
127-
continue
107+
self.connect_enterprise()
128108
else:
129109
raise TypeError("Invalid WiFi connection type specified")
130110

111+
def connect_normal(self):
112+
"""
113+
Attempt a regular style WiFi connection
114+
"""
115+
while not self._esp.is_connected:
116+
try:
117+
if self.debug:
118+
print("Connecting to AP...")
119+
self.pixel_status((100, 0, 0))
120+
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
121+
failure_count = 0
122+
self.pixel_status((0, 100, 0))
123+
except (ValueError, RuntimeError) as error:
124+
print("Failed to connect, retrying\n", error)
125+
failure_count += 1
126+
if failure_count >= self.attempts:
127+
failure_count = 0
128+
self.reset()
129+
continue
130+
131+
def connect_enterprise(self):
132+
"""
133+
Attempt an enterprise style WiFi connection
134+
"""
135+
self._esp.wifi_set_network(bytes(self.ent_ssid, 'utf-8'))
136+
self._esp.wifi_set_entidentity(bytes(self.ent_ident, 'utf-8'))
137+
self._esp.wifi_set_entusername(bytes(self.ent_user, 'utf-8'))
138+
self._esp.wifi_set_entpassword(bytes(self.ent_password, 'utf-8'))
139+
self._esp.wifi_set_entenable()
140+
while not self._esp.is_connected:
141+
try:
142+
if self.debug:
143+
print("Waiting for the ESP32 to connect to the WPA2 Enterprise AP...")
144+
self.pixel_status((100, 0, 0))
145+
sleep(1)
146+
failure_count = 0
147+
self.pixel_status((0, 100, 0))
148+
sleep(1)
149+
except (ValueError, RuntimeError) as error:
150+
print("Failed to connect, retrying\n", error)
151+
failure_count += 1
152+
if failure_count >= self.attempts:
153+
failure_count = 0
154+
self.reset()
155+
continue
156+
131157
def get(self, url, **kw):
132158
"""
133159
Pass the Get request to requests and update status LED

examples/esp32spi_wpa2ent_aio_post.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
print("WiFi secrets are kept in secrets.py, please add them there!")
1616
raise
1717

18-
# If you are using a board with pre-defined ESP32 Pins:
19-
esp32_cs = DigitalInOut(board.ESP_CS)
20-
esp32_ready = DigitalInOut(board.ESP_BUSY)
21-
esp32_reset = DigitalInOut(board.ESP_RESET)
22-
23-
# If you have an externally connected ESP32:
24-
# esp32_cs = DigitalInOut(board.D9)
25-
# esp32_ready = DigitalInOut(board.D10)
26-
# esp32_reset = DigitalInOut(board.D5)
18+
# ESP32 setup
19+
# If your board does define the three pins listed below,
20+
# you can set the correct pins in the second block
21+
try:
22+
esp32_cs = DigitalInOut(board.ESP_CS)
23+
esp32_ready = DigitalInOut(board.ESP_BUSY)
24+
esp32_reset = DigitalInOut(board.ESP_RESET)
25+
except AttributeError:
26+
esp32_cs = DigitalInOut(board.D9)
27+
esp32_ready = DigitalInOut(board.D10)
28+
esp32_reset = DigitalInOut(board.D5)
2729

2830
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2931
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

examples/esp32spi_wpa2ent_simpletest.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ def normalize(v):
2525

2626
print("ESP32 SPI WPA2 Enterprise test")
2727

28-
# For running on the PyPortal, use this block
29-
esp32_cs = DigitalInOut(board.ESP_CS)
30-
esp32_ready = DigitalInOut(board.ESP_BUSY)
31-
esp32_reset = DigitalInOut(board.ESP_RESET)
32-
33-
# For a board that doesn't have the ESP pin definitions, use this block and
34-
# set the pins as needed.
35-
#esp32_cs = DigitalInOut(board.D8)
36-
#esp32_ready = DigitalInOut(board.D5)
37-
#esp32_reset = DigitalInOut(board.D7)
28+
# ESP32 setup
29+
# If your board does define the three pins listed below,
30+
# you can set the correct pins in the second block
31+
try:
32+
esp32_cs = DigitalInOut(board.ESP_CS)
33+
esp32_ready = DigitalInOut(board.ESP_BUSY)
34+
esp32_reset = DigitalInOut(board.ESP_RESET)
35+
except AttributeError:
36+
esp32_cs = DigitalInOut(board.D9)
37+
esp32_ready = DigitalInOut(board.D10)
38+
esp32_reset = DigitalInOut(board.D5)
3839

3940
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4041
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

0 commit comments

Comments
 (0)