Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve native wifi API compatibility #199

Merged
merged 6 commits into from
May 3, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions adafruit_esp32spi/adafruit_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ def MAC_address(self): # pylint: disable=invalid-name
@property
def MAC_address_actual(self): # pylint: disable=invalid-name
"""A bytearray containing the actual MAC address of the ESP32"""
if self._debug:
print("MAC address")
resp = self._send_command_get_response(_GET_MACADDR_CMD, [b"\xFF"])
new_resp = bytearray(resp[0])
new_resp = reversed(new_resp)
return new_resp
return bytearray(reversed(self.MAC_address))

@property
def mac_address(self):
"""A bytes containing the actual MAC address of the ESP32"""
return bytes(self.MAC_address_actual)

def start_scan_networks(self):
"""Begin a scan of visible access points. Follow up with a call
Expand Down Expand Up @@ -545,14 +545,19 @@ def ip_address(self):
return self.network_data["ip_addr"]

@property
def is_connected(self):
def connected(self):
"""Whether the ESP32 is connected to an access point"""
try:
return self.status == WL_CONNECTED
except OSError:
self.reset()
return False

@property
def is_connected(self):
"""Whether the ESP32 is connected to an access point"""
return self.connected

@property
def ap_listening(self):
"""Returns if the ESP32 is in access point mode and is listening for connections"""
Expand All @@ -568,10 +573,11 @@ def disconnect(self):
if resp[0][0] != 1:
raise OSError("Failed to disconnect")

def connect(self, secrets):
"""Connect to an access point using a secrets dictionary
that contains a 'ssid' and 'password' entry"""
self.connect_AP(secrets["ssid"], secrets["password"])
def connect(self, ssid, password=None, timeout=10):
"""Connect to an access point with given name and password."""
if isinstance(ssid, dict): # secrets
ssid, password = ssid["ssid"], ssid["password"]
anecdata marked this conversation as resolved.
Show resolved Hide resolved
self.connect_AP(ssid, password, timeout_s=timeout)

def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name
"""Connect to an access point with given name and password.
Expand Down Expand Up @@ -647,6 +653,11 @@ def create_AP(
raise ConnectionError("Failed to create AP", ssid)
raise OSError("Unknown error 0x%02x" % stat)

@property
def ipv4_address(self):
"""IP address of the station when connected to an access point."""
return self.pretty_ip(self.ip_address)

def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name
"""Converts a bytearray IP address to a dotted-quad string for printing"""
return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3])
Expand Down
Loading