@@ -411,25 +411,14 @@ def wifi_set_entenable(self):
411
411
if resp [0 ][0 ] != 1 :
412
412
raise RuntimeError ("Failed to enable enterprise mode" )
413
413
414
- def wifi_set_ap_network (self , ssid , channel ):
415
- """
416
- Creates an Access point with SSID and Channel
417
-
418
- :param ssid: the SSID of the created Access Point. Must be 8 or more characters
419
- :param channel: channel of created Access Point (1 - 14).
420
- """
414
+ def _wifi_set_ap_network (self , ssid , channel ):
415
+ """Creates an Access point with SSID and Channel"""
421
416
resp = self ._send_command_get_response (_SET_AP_NET_CMD , [ssid , channel ])
422
417
if resp [0 ][0 ] != 1 :
423
418
raise RuntimeError ("Failed to setup AP network" )
424
419
425
- def wifi_set_ap_passphrase (self , ssid , passphrase , channel ):
426
- """
427
- Creates an Access point with SSID, passphrase, and Channel
428
-
429
- :param ssid: the SSID of the created Access Point. Must be 8 or more characters
430
- :param passphrase: the password of the created Access Point. Must be 8 or more characters.
431
- :param channel: channel of created Access Point (1 - 14).
432
- """
420
+ def _wifi_set_ap_passphrase (self , ssid , passphrase , channel ):
421
+ """Creates an Access point with SSID, passphrase, and Channel"""
433
422
resp = self ._send_command_get_response (_SET_AP_PASSPHRASE_CMD , [ssid , passphrase , channel ])
434
423
if resp [0 ][0 ] != 1 :
435
424
raise RuntimeError ("Failed to setup AP password" )
@@ -470,7 +459,7 @@ def is_connected(self):
470
459
471
460
@property
472
461
def ap_listening (self ):
473
- """Whether the ESP32 is in access point mode and is listening for connections"""
462
+ """Returns if the ESP32 is in access point mode and is listening for connections"""
474
463
try :
475
464
return self .status == WL_AP_LISTENING
476
465
except RuntimeError :
@@ -482,10 +471,16 @@ def connect(self, secrets):
482
471
that contains a 'ssid' and 'password' entry"""
483
472
self .connect_AP (secrets ['ssid' ], secrets ['password' ])
484
473
485
- def connect_AP (self , ssid , password ): # pylint: disable=invalid-name
486
- """Connect to an access point with given name and password.
487
- Will retry up to 10 times and return on success or raise
488
- an exception on failure"""
474
+ def connect_AP (self , ssid , password , timeout_s = 10 ): # pylint: disable=invalid-name
475
+ """
476
+ Connect to an access point with given name and password.
477
+ Will wait until specified timeout seconds and return on success
478
+ or raise an exception on failure.
479
+
480
+ :param ssid: the SSID to connect to
481
+ :param passphrase: the password of the access point
482
+ :param timeout_s: number of seconds until we time out and fail to create AP
483
+ """
489
484
if self ._debug :
490
485
print ("Connect to AP" , ssid , password )
491
486
if isinstance (ssid , str ):
@@ -496,32 +491,53 @@ def connect_AP(self, ssid, password): # pylint: disable=invalid-name
496
491
self .wifi_set_passphrase (ssid , password )
497
492
else :
498
493
self .wifi_set_network (ssid )
499
- for _ in range (10 ): # retries
494
+ times = time .monotonic ()
495
+ while (time .monotonic () - times ) < timeout_s : # wait up until timeout
500
496
stat = self .status
501
497
if stat == WL_CONNECTED :
502
498
return stat
503
- time .sleep (1 )
499
+ time .sleep (0.05 )
504
500
if stat in (WL_CONNECT_FAILED , WL_CONNECTION_LOST , WL_DISCONNECTED ):
505
501
raise RuntimeError ("Failed to connect to ssid" , ssid )
506
502
if stat == WL_NO_SSID_AVAIL :
507
503
raise RuntimeError ("No such ssid" , ssid )
508
504
raise RuntimeError ("Unknown error 0x%02X" % stat )
509
505
510
- def create_ap (self , ssid , password , channel = b'\x01 ' ):
511
- """Create an access point with the given name and password."""
506
+ def create_AP (self , ssid , password , channel = 1 , timeout_s = 10 ): # pylint: disable=invalid-name
507
+ """
508
+ Create an access point with the given name, password, and channel.
509
+ Will wait until specified timeout seconds and return on success
510
+ or raise an exception on failure.
511
+
512
+ :param ssid: the SSID of the created Access Point. Must be 8 or more characters
513
+ :param passphrase: the password of the created Access Point. Must be 8 or more characters.
514
+ :param channel: channel of created Access Point (1 - 14).
515
+ :param timeout_s: number of seconds until we time out and fail to create AP
516
+ """
517
+ if len (ssid ) < 8 :
518
+ raise RuntimeError ("ssid must be 8 more more characters" )
519
+ if len (password ) < 8 :
520
+ raise RuntimeError ("password must be 8 or more characters" )
521
+ if channel < 1 or channel > 14 :
522
+ raise RuntimeError ("channel must be between 1 and 14" )
523
+
524
+ if isinstance (channel , int ):
525
+ channel = bytes (channel )
512
526
if isinstance (ssid , str ):
513
527
ssid = bytes (ssid , 'utf-8' )
514
528
if password :
515
529
if isinstance (password , str ):
516
530
password = bytes (password , 'utf-8' )
517
- self .wifi_set_ap_passphrase (ssid , password , channel )
531
+ self ._wifi_set_ap_passphrase (ssid , password , channel )
518
532
else :
519
- self .wifi_set_ap_network (ssid , channel )
520
- for _ in range (10 ): # retries
533
+ self ._wifi_set_ap_network (ssid , channel )
534
+
535
+ times = time .monotonic ()
536
+ while (time .monotonic () - times ) < timeout_s : # wait up until timeout
521
537
stat = self .status
522
538
if stat == WL_AP_LISTENING :
523
539
return stat
524
- time .sleep (1 )
540
+ time .sleep (0.05 )
525
541
if stat == WL_AP_FAILED :
526
542
raise RuntimeError ("Failed to create AP" , ssid )
527
543
raise RuntimeError ("Unknown error 0x%02x" % stat )
0 commit comments