|
54 | 54 | # pylint: disable=bad-whitespace
|
55 | 55 | _SET_NET_CMD = const(0x10)
|
56 | 56 | _SET_PASSPHRASE_CMD = const(0x11)
|
| 57 | +_SET_AP_NET_CMD = const(0x18) |
| 58 | +_SET_AP_PASSPHRASE_CMD = const(0x19) |
57 | 59 | _SET_DEBUG_CMD = const(0x1A)
|
58 | 60 |
|
59 | 61 | _GET_CONN_STATUS_CMD = const(0x20)
|
@@ -409,6 +411,18 @@ def wifi_set_entenable(self):
|
409 | 411 | if resp[0][0] != 1:
|
410 | 412 | raise RuntimeError("Failed to enable enterprise mode")
|
411 | 413 |
|
| 414 | + def wifi_set_ap_network(self, ssid, channel): |
| 415 | + """TODO Docs""" |
| 416 | + resp = self._send_command_get_response(_SET_AP_NET_CMD, [ssid, channel]) |
| 417 | + if resp[0][0] != 1: |
| 418 | + raise RuntimeError("Failed to setup AP network") |
| 419 | + |
| 420 | + def wifi_set_ap_passphrase(self, ssid, passphrase, channel): |
| 421 | + """TODO Docs""" |
| 422 | + resp = self._send_command_get_response(_SET_AP_PASSPHRASE_CMD, [ssid, passphrase, channel]) |
| 423 | + if resp[0][0] != 1: |
| 424 | + raise RuntimeError("Failed to setup AP network") |
| 425 | + |
412 | 426 | @property
|
413 | 427 | def ssid(self):
|
414 | 428 | """The name of the access point we're connected to"""
|
@@ -443,6 +457,15 @@ def is_connected(self):
|
443 | 457 | self.reset()
|
444 | 458 | return False
|
445 | 459 |
|
| 460 | + @property |
| 461 | + def ap_listening(self): |
| 462 | + """Whether the ESP32 is in access point mode and is listening for connections""" |
| 463 | + try: |
| 464 | + return self.status == WL_AP_LISTENING |
| 465 | + except RuntimeError: |
| 466 | + self.reset() |
| 467 | + return False |
| 468 | + |
446 | 469 | def connect(self, secrets):
|
447 | 470 | """Connect to an access point using a secrets dictionary
|
448 | 471 | that contains a 'ssid' and 'password' entry"""
|
@@ -473,6 +496,25 @@ def connect_AP(self, ssid, password): # pylint: disable=invalid-name
|
473 | 496 | raise RuntimeError("No such ssid", ssid)
|
474 | 497 | raise RuntimeError("Unknown error 0x%02X" % stat)
|
475 | 498 |
|
| 499 | + def create_AP(self, ssid, password, channel=1): |
| 500 | + """Create an access point with the given name and password.""" |
| 501 | + if isinstance(ssid, str): |
| 502 | + ssid = bytes(ssid, 'utf-8') |
| 503 | + if password: |
| 504 | + if isinstance(password, str): |
| 505 | + password = bytes(password, 'utf-8') |
| 506 | + self.wifi_set_ap_passphrase(ssid, password, channel) |
| 507 | + else: |
| 508 | + self.wifi_set_ap_network(ssid, channel) |
| 509 | + for _ in range(10): # retries |
| 510 | + stat = self.status |
| 511 | + if stat == WL_AP_LISTENING: |
| 512 | + return stat |
| 513 | + time.sleep(1) |
| 514 | + if stat == WL_AP_FAILED: |
| 515 | + raise RuntimeError("Failed to create AP", ssid) |
| 516 | + raise RuntimeError("Unknown error 0x%02x" % stat) |
| 517 | + |
476 | 518 | def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name
|
477 | 519 | """Converts a bytearray IP address to a dotted-quad string for printing"""
|
478 | 520 | return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3])
|
|
0 commit comments