31
31
32
32
# pylint: disable=no-name-in-module
33
33
34
+ from time import sleep
34
35
from micropython import const
35
36
from adafruit_esp32spi import adafruit_esp32spi
36
37
import adafruit_esp32spi .adafruit_esp32spi_requests as requests
@@ -42,6 +43,7 @@ class ESPSPI_WiFiManager:
42
43
NORMAL = const (1 )
43
44
ENTERPRISE = const (2 )
44
45
46
+ # pylint: disable=too-many-arguments
45
47
def __init__ (self , esp , secrets , status_pixel = None , attempts = 2 , connection_type = NORMAL ):
46
48
"""
47
49
: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=
59
61
self .debug = False
60
62
self .ssid = secrets ['ssid' ]
61
63
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' ]
66
64
self .attempts = attempts
67
65
self ._connection_type = connection_type
68
66
requests .set_interface (self ._esp )
69
67
self .statuspix = status_pixel
70
68
self .pixel_status (0 )
71
69
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
+
72
85
def reset (self ):
73
86
"""
74
87
Perform a hard reset on the ESP32
@@ -88,46 +101,59 @@ def connect(self):
88
101
print ("MAC addr:" , [hex (i ) for i in self ._esp .MAC_address ])
89
102
for access_pt in self ._esp .scan_networks ():
90
103
print ("\t %s\t \t RSSI: %d" % (str (access_pt ['ssid' ], 'utf-8' ), access_pt ['rssi' ]))
91
- failure_count = 0
92
104
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 ()
108
106
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 ()
128
108
else :
129
109
raise TypeError ("Invalid WiFi connection type specified" )
130
110
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
+
131
157
def get (self , url , ** kw ):
132
158
"""
133
159
Pass the Get request to requests and update status LED
0 commit comments