31
31
32
32
# pylint: disable=no-name-in-module
33
33
34
- import adafruit_esp32spi .adafruit_esp32spi_requests as requests
34
+ from time import sleep
35
+ from micropython import const
35
36
from adafruit_esp32spi import adafruit_esp32spi
37
+ import adafruit_esp32spi .adafruit_esp32spi_requests as requests
36
38
37
39
38
40
class ESPSPI_WiFiManager :
39
41
"""
40
42
A class to help manage the Wifi connection
41
43
"""
42
- # pylint: disable=too-many-arguments
43
- def __init__ (self , esp , secrets , status_pixel = None , attempts = 2 , debug = False ):
44
+ NORMAL = const (1 )
45
+ ENTERPRISE = const (2 )
46
+
47
+ # pylint: disable=too-many-arguments, line-too-long
48
+ def __init__ (self , esp , secrets , status_pixel = None , attempts = 2 , connection_type = NORMAL , debug = False ):
44
49
"""
45
50
:param ESP_SPIcontrol esp: The ESP object we are using
46
51
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
47
52
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
48
53
or RGB LED (default=None)
49
54
:type status_pixel: NeoPixel, DotStar, or RGB LED
50
55
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
56
+ :param const connection_type: (Optional) Type of WiFi connection: NORMAL or ENTERPRISE
51
57
"""
52
58
# Read the settings
53
59
self .esp = esp
54
60
self .debug = debug
55
61
self .ssid = secrets ['ssid' ]
56
62
self .password = secrets .get ('password' , None )
57
63
self .attempts = attempts
64
+ self ._connection_type = connection_type
58
65
requests .set_interface (self .esp )
59
66
self .statuspix = status_pixel
60
67
self .pixel_status (0 )
61
- # pylint: enable=too-many-arguments
68
+
69
+ # Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
70
+ if secrets .get ('ent_ssid' ):
71
+ self .ent_ssid = secrets ['ent_ssid' ]
72
+ else :
73
+ self .ent_ssid = secrets ['ssid' ]
74
+ if secrets .get ('ent_ident' ):
75
+ self .ent_ident = secrets ['ent_ident' ]
76
+ else :
77
+ self .ent_ident = ''
78
+ if secrets .get ('ent_user' ):
79
+ self .ent_user = secrets ['ent_user' ]
80
+ if secrets .get ('ent_password' ):
81
+ self .ent_password = secrets ['ent_password' ]
82
+ # pylint: enable=too-many-arguments, line-too-long
62
83
63
84
def reset (self ):
64
85
"""
@@ -79,6 +100,17 @@ def connect(self):
79
100
print ("MAC addr:" , [hex (i ) for i in self .esp .MAC_address ])
80
101
for access_pt in self .esp .scan_networks ():
81
102
print ("\t %s\t \t RSSI: %d" % (str (access_pt ['ssid' ], 'utf-8' ), access_pt ['rssi' ]))
103
+ if self ._connection_type == ESPSPI_WiFiManager .NORMAL :
104
+ self .connect_normal ()
105
+ elif self ._connection_type == ESPSPI_WiFiManager .ENTERPRISE :
106
+ self .connect_enterprise ()
107
+ else :
108
+ raise TypeError ("Invalid WiFi connection type specified" )
109
+
110
+ def connect_normal (self ):
111
+ """
112
+ Attempt a regular style WiFi connection
113
+ """
82
114
failure_count = 0
83
115
while not self .esp .is_connected :
84
116
try :
@@ -123,6 +155,33 @@ def create_ap(self):
123
155
continue
124
156
print ("Access Point created! Connect to ssid: {}" .format (self .ssid ))
125
157
158
+ def connect_enterprise (self ):
159
+ """
160
+ Attempt an enterprise style WiFi connection
161
+ """
162
+ failure_count = 0
163
+ self .esp .wifi_set_network (bytes (self .ent_ssid , 'utf-8' ))
164
+ self .esp .wifi_set_entidentity (bytes (self .ent_ident , 'utf-8' ))
165
+ self .esp .wifi_set_entusername (bytes (self .ent_user , 'utf-8' ))
166
+ self .esp .wifi_set_entpassword (bytes (self .ent_password , 'utf-8' ))
167
+ self .esp .wifi_set_entenable ()
168
+ while not self .esp .is_connected :
169
+ try :
170
+ if self .debug :
171
+ print ("Waiting for the ESP32 to connect to the WPA2 Enterprise AP..." )
172
+ self .pixel_status ((100 , 0 , 0 ))
173
+ sleep (1 )
174
+ failure_count = 0
175
+ self .pixel_status ((0 , 100 , 0 ))
176
+ sleep (1 )
177
+ except (ValueError , RuntimeError ) as error :
178
+ print ("Failed to connect, retrying\n " , error )
179
+ failure_count += 1
180
+ if failure_count >= self .attempts :
181
+ failure_count = 0
182
+ self .reset ()
183
+ continue
184
+
126
185
def get (self , url , ** kw ):
127
186
"""
128
187
Pass the Get request to requests and update status LED
0 commit comments