Skip to content

Commit b9befbf

Browse files
committed
add access point creation SPI commands and wifimanager helper
1 parent 92182ea commit b9befbf

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
# pylint: disable=bad-whitespace
5555
_SET_NET_CMD = const(0x10)
5656
_SET_PASSPHRASE_CMD = const(0x11)
57+
_SET_AP_NET_CMD = const(0x18)
58+
_SET_AP_PASSPHRASE_CMD = const(0x19)
5759
_SET_DEBUG_CMD = const(0x1A)
5860

5961
_GET_CONN_STATUS_CMD = const(0x20)
@@ -409,6 +411,18 @@ def wifi_set_entenable(self):
409411
if resp[0][0] != 1:
410412
raise RuntimeError("Failed to enable enterprise mode")
411413

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+
412426
@property
413427
def ssid(self):
414428
"""The name of the access point we're connected to"""
@@ -443,6 +457,15 @@ def is_connected(self):
443457
self.reset()
444458
return False
445459

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+
446469
def connect(self, secrets):
447470
"""Connect to an access point using a secrets dictionary
448471
that contains a 'ssid' and 'password' entry"""
@@ -473,6 +496,25 @@ def connect_AP(self, ssid, password): # pylint: disable=invalid-name
473496
raise RuntimeError("No such ssid", ssid)
474497
raise RuntimeError("Unknown error 0x%02X" % stat)
475498

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+
476518
def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name
477519
"""Converts a bytearray IP address to a dotted-quad string for printing"""
478520
return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3])

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131

3232
# pylint: disable=no-name-in-module
3333

34-
from adafruit_esp32spi import adafruit_esp32spi
3534
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
35+
from adafruit_esp32spi import adafruit_esp32spi
36+
3637

3738
class ESPSPI_WiFiManager:
3839
"""
@@ -93,6 +94,27 @@ def connect(self):
9394
self.reset()
9495
continue
9596

97+
def create_ap(self):
98+
"""
99+
Attempt to initialize in Access Point (AP) mode.
100+
Other WiFi devices will be able to connect to the created Access Point
101+
"""
102+
while not self._esp.ap_listening:
103+
try:
104+
if self.debug:
105+
print("Waiting for AP to be initialized...")
106+
self.pixel_status((100,0,0))
107+
self._esp.create_AP(bytes.ssid, 'utf-8'), bytes(self.password, 'utf-8')
108+
failure_count = 0
109+
self.pixel_status((0,100,0))
110+
except (ValueError, RuntimeError) as error:
111+
print("Failed to create access point\n", error)
112+
failure_count += 1
113+
if failure_count >= self.attempts:
114+
failure_count = 0
115+
self.reset()
116+
continue
117+
96118
def get(self, url, **kw):
97119
"""
98120
Pass the Get request to requests and update status LED

0 commit comments

Comments
 (0)