@@ -411,25 +411,14 @@ def wifi_set_entenable(self):
411411 if resp [0 ][0 ] != 1 :
412412 raise RuntimeError ("Failed to enable enterprise mode" )
413413
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"""
421416 resp = self ._send_command_get_response (_SET_AP_NET_CMD , [ssid , channel ])
422417 if resp [0 ][0 ] != 1 :
423418 raise RuntimeError ("Failed to setup AP network" )
424419
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"""
433422 resp = self ._send_command_get_response (_SET_AP_PASSPHRASE_CMD , [ssid , passphrase , channel ])
434423 if resp [0 ][0 ] != 1 :
435424 raise RuntimeError ("Failed to setup AP password" )
@@ -470,7 +459,7 @@ def is_connected(self):
470459
471460 @property
472461 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"""
474463 try :
475464 return self .status == WL_AP_LISTENING
476465 except RuntimeError :
@@ -482,10 +471,16 @@ def connect(self, secrets):
482471 that contains a 'ssid' and 'password' entry"""
483472 self .connect_AP (secrets ['ssid' ], secrets ['password' ])
484473
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+ """
489484 if self ._debug :
490485 print ("Connect to AP" , ssid , password )
491486 if isinstance (ssid , str ):
@@ -496,32 +491,53 @@ def connect_AP(self, ssid, password): # pylint: disable=invalid-name
496491 self .wifi_set_passphrase (ssid , password )
497492 else :
498493 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
500496 stat = self .status
501497 if stat == WL_CONNECTED :
502498 return stat
503- time .sleep (1 )
499+ time .sleep (0.05 )
504500 if stat in (WL_CONNECT_FAILED , WL_CONNECTION_LOST , WL_DISCONNECTED ):
505501 raise RuntimeError ("Failed to connect to ssid" , ssid )
506502 if stat == WL_NO_SSID_AVAIL :
507503 raise RuntimeError ("No such ssid" , ssid )
508504 raise RuntimeError ("Unknown error 0x%02X" % stat )
509505
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 )
512526 if isinstance (ssid , str ):
513527 ssid = bytes (ssid , 'utf-8' )
514528 if password :
515529 if isinstance (password , str ):
516530 password = bytes (password , 'utf-8' )
517- self .wifi_set_ap_passphrase (ssid , password , channel )
531+ self ._wifi_set_ap_passphrase (ssid , password , channel )
518532 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
521537 stat = self .status
522538 if stat == WL_AP_LISTENING :
523539 return stat
524- time .sleep (1 )
540+ time .sleep (0.05 )
525541 if stat == WL_AP_FAILED :
526542 raise RuntimeError ("Failed to create AP" , ssid )
527543 raise RuntimeError ("Unknown error 0x%02x" % stat )
0 commit comments