3131
3232# pylint: disable=no-name-in-module
3333
34+ from time import sleep
3435from micropython import const
3536from adafruit_esp32spi import adafruit_esp32spi
3637import 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 \t RSSI: %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
0 commit comments