31
31
32
32
# pylint: disable=no-name-in-module
33
33
34
+ from time import sleep
35
+ from micropython import const
34
36
from adafruit_esp32spi import adafruit_esp32spi
35
37
import adafruit_esp32spi .adafruit_esp32spi_requests as requests
36
38
37
39
class ESPSPI_WiFiManager :
38
40
"""
39
41
A class to help manage the Wifi connection
40
42
"""
41
- def __init__ (self , esp , secrets , status_pixel = None , attempts = 2 ):
43
+ NORMAL = const (1 )
44
+ ENTERPRISE = const (2 )
45
+
46
+ # pylint: disable=too-many-arguments
47
+ def __init__ (self , esp , secrets , status_pixel = None , attempts = 2 , connection_type = NORMAL ):
42
48
"""
43
49
:param ESP_SPIcontrol esp: The ESP object we are using
44
50
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
45
51
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
46
52
or RGB LED (default=None)
47
53
:type status_pixel: NeoPixel, DotStar, or RGB LED
48
54
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
55
+ :param const connection_type: (Optional) Type of WiFi connection: NORMAL or ENTERPRISE
49
56
"""
50
57
# Read the settings
51
58
self .esp = esp
52
59
self .debug = False
53
60
self .ssid = secrets ['ssid' ]
54
61
self .password = secrets ['password' ]
55
62
self .attempts = attempts
63
+ self ._connection_type = connection_type
56
64
requests .set_interface (self .esp )
57
65
self .statuspix = status_pixel
58
66
self .pixel_status (0 )
59
67
68
+ # Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
69
+ if secrets .get ('ent_ssid' ):
70
+ self .ent_ssid = secrets ['ent_ssid' ]
71
+ else :
72
+ self .ent_ssid = secrets ['ssid' ]
73
+ if secrets .get ('ent_ident' ):
74
+ self .ent_ident = secrets ['ent_ident' ]
75
+ else :
76
+ self .ent_ident = ''
77
+ if secrets .get ('ent_user' ):
78
+ self .ent_user = secrets ['ent_user' ]
79
+ if secrets .get ('ent_password' ):
80
+ self .ent_password = secrets ['ent_password' ]
81
+ # pylint: enable=too-many-arguments
82
+
60
83
def reset (self ):
61
84
"""
62
85
Perform a hard reset on the ESP32
@@ -76,6 +99,17 @@ def connect(self):
76
99
print ("MAC addr:" , [hex (i ) for i in self .esp .MAC_address ])
77
100
for access_pt in self .esp .scan_networks ():
78
101
print ("\t %s\t \t RSSI: %d" % (str (access_pt ['ssid' ], 'utf-8' ), access_pt ['rssi' ]))
102
+ if self ._connection_type == ESPSPI_WiFiManager .NORMAL :
103
+ self .connect_normal ()
104
+ elif self ._connection_type == ESPSPI_WiFiManager .ENTERPRISE :
105
+ self .connect_enterprise ()
106
+ else :
107
+ raise TypeError ("Invalid WiFi connection type specified" )
108
+
109
+ def connect_normal (self ):
110
+ """
111
+ Attempt a regular style WiFi connection
112
+ """
79
113
failure_count = 0
80
114
while not self .esp .is_connected :
81
115
try :
@@ -93,6 +127,33 @@ def connect(self):
93
127
self .reset ()
94
128
continue
95
129
130
+ def connect_enterprise (self ):
131
+ """
132
+ Attempt an enterprise style WiFi connection
133
+ """
134
+ failure_count = 0
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
+
96
157
def get (self , url , ** kw ):
97
158
"""
98
159
Pass the Get request to requests and update status LED
0 commit comments