From 04b795086a2a594dbf27c270f7090bce0e2e24c2 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Wed, 12 Jun 2024 14:27:13 -0700 Subject: [PATCH 1/3] Add ap_info --- adafruit_esp32spi/adafruit_esp32spi.py | 141 ++++++++++++++---- .../adafruit_esp32spi_wifimanager.py | 9 +- examples/esp32spi_ipconfig.py | 6 +- examples/esp32spi_simpletest.py | 8 +- examples/esp32spi_simpletest_rp2040.py | 6 +- examples/esp32spi_wpa2ent_simpletest.py | 12 +- examples/gpio/esp32spi_gpio.py | 6 +- 7 files changed, 130 insertions(+), 58 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index cbb37e9..5713c87 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -131,6 +131,97 @@ # pylint: disable=too-many-lines +class ESP_Network: + """A wifi network provided by a nearby access point.""" + + def __init__( # pylint: disable=too-many-arguments + self, + esp_spi_control=None, + raw_ssid=None, + raw_bssid=None, + raw_rssi=None, + raw_channel=None, + raw_country=None, + raw_authmode=None, + ): + self._esp_spi_control = esp_spi_control + self._raw_ssid = raw_ssid + self._raw_bssid = raw_bssid + self._raw_rssi = raw_rssi + self._raw_channel = raw_channel + self._raw_country = raw_country + self._raw_authmode = raw_authmode + + def _get_response(self, cmd): + respose = self._esp_spi_control._send_command_get_response( # pylint: disable=protected-access + cmd, [b"\xFF"] + ) + return respose[0] + + @property + def ssid(self): + """String id of the network""" + if self._raw_ssid: + response = self._raw_ssid + else: + response = self._get_response(_GET_CURR_SSID_CMD) + return response.decode("utf-8") + + @property + def bssid(self): + """BSSID of the network (usually the AP’s MAC address)""" + if self._raw_bssid: + response = self._raw_bssid + else: + response = self._get_response(_GET_CURR_BSSID_CMD) + return bytes(response) + + @property + def rssi(self): + """Signal strength of the network""" + if self._raw_bssid: + response = self._raw_rssi + else: + response = self._get_response(_GET_CURR_RSSI_CMD) + return struct.unpack(" 1.5.0 - fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2] + fw_semver_maj = self.firmware_version[2] assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above." resp = self._send_command_get_response(_SET_DIGITAL_READ_CMD, ((pin,),))[0] @@ -961,7 +1044,7 @@ def set_analog_read(self, pin, atten=ADC_ATTEN_DB_11): :param int atten: attenuation constant """ # Verify nina-fw => 1.5.0 - fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2] + fw_semver_maj = self.firmware_version[2] assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above." resp = self._send_command_get_response(_SET_ANALOG_READ_CMD, ((pin,), (atten,))) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 01b97e8..68cd058 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -96,10 +96,7 @@ def connect(self): print("Firmware vers.", self.esp.firmware_version) print("MAC addr:", [hex(i) for i in self.esp.MAC_address]) for access_pt in self.esp.scan_networks(): - print( - "\t%s\t\tRSSI: %d" - % (str(access_pt["ssid"], "utf-8"), access_pt["rssi"]) - ) + print("\t%s\t\tRSSI: %d" % (access_pt.ssid, access_pt.rssi)) if self._connection_type == ESPSPI_WiFiManager.NORMAL: self.connect_normal() elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE: @@ -328,7 +325,7 @@ def ip_address(self): self.connect() self.pixel_status((0, 0, 100)) self.pixel_status(0) - return self.esp.pretty_ip(self.esp.ip_address) + return self.esp.ipv4_address def pixel_status(self, value): """ @@ -349,4 +346,4 @@ def signal_strength(self): """ if not self.esp.is_connected: self.connect() - return self.esp.rssi + return self.esp.ap_info.rssi diff --git a/examples/esp32spi_ipconfig.py b/examples/esp32spi_ipconfig.py index f29e9a1..7e98df6 100644 --- a/examples/esp32spi_ipconfig.py +++ b/examples/esp32spi_ipconfig.py @@ -70,7 +70,7 @@ except OSError as e: print("could not connect to AP, retrying: ", e) continue -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) ip1 = esp.ip_address print("set ip dns") @@ -91,9 +91,7 @@ esp.pretty_ip(info["netmask"]), ) -IP_ADDR = esp.pretty_ip(esp.ip_address) -print("ip:", IP_ADDR) -print("My IP address is", esp.pretty_ip(esp.ip_address)) +print("My IP address is", esp.ipv4_address) print("udp in addr: ", UDP_IN_ADDR, UDP_IN_PORT) socketaddr_udp_in = pool.getaddrinfo(UDP_IN_ADDR, UDP_IN_PORT)[0][4] diff --git a/examples/esp32spi_simpletest.py b/examples/esp32spi_simpletest.py index 962c17e..c194648 100644 --- a/examples/esp32spi_simpletest.py +++ b/examples/esp32spi_simpletest.py @@ -63,11 +63,11 @@ if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: print("ESP32 found and in idle mode") -print("Firmware vers.", esp.firmware_version.decode("utf-8")) +print("Firmware vers.", esp.firmware_version) print("MAC addr:", ":".join("%02X" % byte for byte in esp.MAC_address)) for ap in esp.scan_networks(): - print("\t%-23s RSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"])) + print("\t%-23s RSSI: %d" % (ap.ssid, ap.rssi)) print("Connecting to AP...") while not esp.is_connected: @@ -76,8 +76,8 @@ except OSError as e: print("could not connect to AP, retrying: ", e) continue -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) -print("My IP address is", esp.pretty_ip(esp.ip_address)) +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) +print("My IP address is", esp.ipv4_address) print( "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")) ) diff --git a/examples/esp32spi_simpletest_rp2040.py b/examples/esp32spi_simpletest_rp2040.py index 4330b7d..eb5b8dd 100644 --- a/examples/esp32spi_simpletest_rp2040.py +++ b/examples/esp32spi_simpletest_rp2040.py @@ -46,7 +46,7 @@ print("MAC addr:", [hex(i) for i in esp.MAC_address]) for ap in esp.scan_networks(): - print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"])) + print("\t%s\t\tRSSI: %d" % (ap.ssid, ap.rssi)) print("Connecting to AP...") while not esp.is_connected: @@ -55,8 +55,8 @@ except OSError as e: print("could not connect to AP, retrying: ", e) continue -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) -print("My IP address is", esp.pretty_ip(esp.ip_address)) +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) +print("My IP address is", esp.ipv4_address) print( "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")) ) diff --git a/examples/esp32spi_wpa2ent_simpletest.py b/examples/esp32spi_wpa2ent_simpletest.py index 0df4d28..db5019f 100644 --- a/examples/esp32spi_wpa2ent_simpletest.py +++ b/examples/esp32spi_wpa2ent_simpletest.py @@ -59,17 +59,15 @@ def normalize(v): if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: print("ESP32 found and in idle mode") -# Get the ESP32 fw version number, remove trailing byte off the returned bytearray -# and then convert it to a string for prettier printing and later comparison -firmware_version = "".join([chr(b) for b in esp.firmware_version[:-1]]) -print("Firmware vers.", firmware_version) +# Get the ESP32 fw version number +print("Firmware vers.", esp.firmware_version) print("MAC addr:", [hex(i) for i in esp.MAC_address]) # WPA2 Enterprise support was added in fw ver 1.3.0. Check that the ESP32 # is running at least that version, otherwise, bail out assert ( - version_compare(firmware_version, "1.3.0") >= 0 + version_compare(esp.firmware_version, "1.3.0") >= 0 ), "Incorrect ESP32 firmware version; >= 1.3.0 required." # Set up the SSID you would like to connect to @@ -98,8 +96,8 @@ def normalize(v): time.sleep(2) print("") -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) -print("My IP address is", esp.pretty_ip(esp.ip_address)) +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) +print("My IP address is", esp.ipv4_address) print( "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")) ) diff --git a/examples/gpio/esp32spi_gpio.py b/examples/gpio/esp32spi_gpio.py index 6d7723b..b257eb8 100644 --- a/examples/gpio/esp32spi_gpio.py +++ b/examples/gpio/esp32spi_gpio.py @@ -67,11 +67,7 @@ def esp_init_pin_modes(din, dout): esp_reset_all() -espfirmware = "" -for _ in esp.firmware_version: - if _ != 0: - espfirmware += "{:c}".format(_) -print("ESP32 Firmware:", espfirmware) +print("ESP32 Firmware:", esp.firmware_version) print( "ESP32 MAC: {5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}".format( From c4dfdf57c43af35f2622c84670b6d45167682512 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Sun, 23 Jun 2024 11:36:39 -0800 Subject: [PATCH 2/3] Update adafruit_esp32spi/adafruit_esp32spi.py Co-authored-by: Dan Halbert --- adafruit_esp32spi/adafruit_esp32spi.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index 5713c87..ed5ecad 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -195,9 +195,7 @@ def channel(self): @property def country(self): """String id of the country code""" - if self._raw_country: - return self._raw_country - return None + return self._raw_country @property def authmode(self): From 2da98438cf81145166f89cd7fa3969e32ca186af Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Sun, 23 Jun 2024 16:16:37 -0700 Subject: [PATCH 3/3] Rename from ESP_Network to Network --- adafruit_esp32spi/adafruit_esp32spi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index ed5ecad..96b806d 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -131,7 +131,7 @@ # pylint: disable=too-many-lines -class ESP_Network: +class Network: """A wifi network provided by a nearby access point.""" def __init__( # pylint: disable=too-many-arguments @@ -491,7 +491,7 @@ def get_scan_networks(self): channel = self._send_command_get_response(_GET_IDX_CHAN_CMD, ((i,),))[0] authmode = self._send_command_get_response(_GET_IDX_ENCT_CMD, ((i,),))[0] APs.append( - ESP_Network( + Network( raw_ssid=name, raw_bssid=bssid, raw_rssi=rssi, @@ -608,7 +608,7 @@ def ap_info(self): """Network object containing BSSID, SSID, authmode, channel, country and RSSI when connected to an access point. None otherwise.""" if self.is_connected: - return ESP_Network(esp_spi_control=self) + return Network(esp_spi_control=self) return None @property