Skip to content

Commit 04b7950

Browse files
committed
Add ap_info
1 parent 7bf8763 commit 04b7950

File tree

7 files changed

+130
-58
lines changed

7 files changed

+130
-58
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,97 @@
131131
# pylint: disable=too-many-lines
132132

133133

134+
class ESP_Network:
135+
"""A wifi network provided by a nearby access point."""
136+
137+
def __init__( # pylint: disable=too-many-arguments
138+
self,
139+
esp_spi_control=None,
140+
raw_ssid=None,
141+
raw_bssid=None,
142+
raw_rssi=None,
143+
raw_channel=None,
144+
raw_country=None,
145+
raw_authmode=None,
146+
):
147+
self._esp_spi_control = esp_spi_control
148+
self._raw_ssid = raw_ssid
149+
self._raw_bssid = raw_bssid
150+
self._raw_rssi = raw_rssi
151+
self._raw_channel = raw_channel
152+
self._raw_country = raw_country
153+
self._raw_authmode = raw_authmode
154+
155+
def _get_response(self, cmd):
156+
respose = self._esp_spi_control._send_command_get_response( # pylint: disable=protected-access
157+
cmd, [b"\xFF"]
158+
)
159+
return respose[0]
160+
161+
@property
162+
def ssid(self):
163+
"""String id of the network"""
164+
if self._raw_ssid:
165+
response = self._raw_ssid
166+
else:
167+
response = self._get_response(_GET_CURR_SSID_CMD)
168+
return response.decode("utf-8")
169+
170+
@property
171+
def bssid(self):
172+
"""BSSID of the network (usually the AP’s MAC address)"""
173+
if self._raw_bssid:
174+
response = self._raw_bssid
175+
else:
176+
response = self._get_response(_GET_CURR_BSSID_CMD)
177+
return bytes(response)
178+
179+
@property
180+
def rssi(self):
181+
"""Signal strength of the network"""
182+
if self._raw_bssid:
183+
response = self._raw_rssi
184+
else:
185+
response = self._get_response(_GET_CURR_RSSI_CMD)
186+
return struct.unpack("<i", response)[0]
187+
188+
@property
189+
def channel(self):
190+
"""Channel number the network is operating on"""
191+
if self._raw_channel:
192+
return self._raw_channel[0]
193+
return None
194+
195+
@property
196+
def country(self):
197+
"""String id of the country code"""
198+
if self._raw_country:
199+
return self._raw_country
200+
return None
201+
202+
@property
203+
def authmode(self):
204+
"""String id of the authmode
205+
206+
derived from Nina code:
207+
https://github.com/adafruit/nina-fw/blob/master/arduino/libraries/WiFi/src/WiFi.cpp#L385
208+
"""
209+
if self._raw_authmode:
210+
response = self._raw_authmode[0]
211+
else:
212+
response = self._get_response(_GET_CURR_ENCT_CMD)[0]
213+
214+
if response == 7:
215+
return "OPEN"
216+
if response == 5:
217+
return "WEP"
218+
if response == 2:
219+
return "PSK"
220+
if response == 4:
221+
return "WPA2"
222+
return "UNKNOWN"
223+
224+
134225
class ESP_SPIcontrol: # pylint: disable=too-many-public-methods, too-many-instance-attributes
135226
"""A class that will talk to an ESP32 module programmed with special firmware
136227
that lets it act as a fast an efficient WiFi co-processor"""
@@ -359,7 +450,7 @@ def firmware_version(self):
359450
if self._debug:
360451
print("Firmware version")
361452
resp = self._send_command_get_response(_GET_FW_VERSION_CMD)
362-
return resp[0]
453+
return resp[0].decode("utf-8").replace("\x00", "")
363454

364455
@property
365456
def MAC_address(self): # pylint: disable=invalid-name
@@ -397,16 +488,19 @@ def get_scan_networks(self):
397488
# print("SSID names:", names)
398489
APs = [] # pylint: disable=invalid-name
399490
for i, name in enumerate(names):
400-
a_p = {"ssid": name}
401-
rssi = self._send_command_get_response(_GET_IDX_RSSI_CMD, ((i,),))[0]
402-
a_p["rssi"] = struct.unpack("<i", rssi)[0]
403-
encr = self._send_command_get_response(_GET_IDX_ENCT_CMD, ((i,),))[0]
404-
a_p["encryption"] = encr[0]
405491
bssid = self._send_command_get_response(_GET_IDX_BSSID_CMD, ((i,),))[0]
406-
a_p["bssid"] = bssid
407-
chan = self._send_command_get_response(_GET_IDX_CHAN_CMD, ((i,),))[0]
408-
a_p["channel"] = chan[0]
409-
APs.append(a_p)
492+
rssi = self._send_command_get_response(_GET_IDX_RSSI_CMD, ((i,),))[0]
493+
channel = self._send_command_get_response(_GET_IDX_CHAN_CMD, ((i,),))[0]
494+
authmode = self._send_command_get_response(_GET_IDX_ENCT_CMD, ((i,),))[0]
495+
APs.append(
496+
ESP_Network(
497+
raw_ssid=name,
498+
raw_bssid=bssid,
499+
raw_rssi=rssi,
500+
raw_channel=channel,
501+
raw_authmode=authmode,
502+
)
503+
)
410504
return APs
411505

412506
def scan_networks(self):
@@ -512,23 +606,12 @@ def _wifi_set_ap_passphrase(self, ssid, passphrase, channel):
512606
raise OSError("Failed to setup AP password")
513607

514608
@property
515-
def ssid(self):
516-
"""The name of the access point we're connected to"""
517-
resp = self._send_command_get_response(_GET_CURR_SSID_CMD, [b"\xFF"])
518-
return resp[0]
519-
520-
@property
521-
def bssid(self):
522-
"""The MAC-formatted service set ID of the access point we're connected to"""
523-
resp = self._send_command_get_response(_GET_CURR_BSSID_CMD, [b"\xFF"])
524-
return resp[0]
525-
526-
@property
527-
def rssi(self):
528-
"""The receiving signal strength indicator for the access point we're
529-
connected to"""
530-
resp = self._send_command_get_response(_GET_CURR_RSSI_CMD, [b"\xFF"])
531-
return struct.unpack("<i", resp[0])[0]
609+
def ap_info(self):
610+
"""Network object containing BSSID, SSID, authmode, channel, country and RSSI when
611+
connected to an access point. None otherwise."""
612+
if self.is_connected:
613+
return ESP_Network(esp_spi_control=self)
614+
return None
532615

533616
@property
534617
def network_data(self):
@@ -942,7 +1025,7 @@ def set_digital_read(self, pin):
9421025
:param int pin: ESP32 GPIO pin to read from.
9431026
"""
9441027
# Verify nina-fw => 1.5.0
945-
fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2]
1028+
fw_semver_maj = self.firmware_version[2]
9461029
assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above."
9471030

9481031
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):
9611044
:param int atten: attenuation constant
9621045
"""
9631046
# Verify nina-fw => 1.5.0
964-
fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2]
1047+
fw_semver_maj = self.firmware_version[2]
9651048
assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above."
9661049

9671050
resp = self._send_command_get_response(_SET_ANALOG_READ_CMD, ((pin,), (atten,)))

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ def connect(self):
9696
print("Firmware vers.", self.esp.firmware_version)
9797
print("MAC addr:", [hex(i) for i in self.esp.MAC_address])
9898
for access_pt in self.esp.scan_networks():
99-
print(
100-
"\t%s\t\tRSSI: %d"
101-
% (str(access_pt["ssid"], "utf-8"), access_pt["rssi"])
102-
)
99+
print("\t%s\t\tRSSI: %d" % (access_pt.ssid, access_pt.rssi))
103100
if self._connection_type == ESPSPI_WiFiManager.NORMAL:
104101
self.connect_normal()
105102
elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE:
@@ -328,7 +325,7 @@ def ip_address(self):
328325
self.connect()
329326
self.pixel_status((0, 0, 100))
330327
self.pixel_status(0)
331-
return self.esp.pretty_ip(self.esp.ip_address)
328+
return self.esp.ipv4_address
332329

333330
def pixel_status(self, value):
334331
"""
@@ -349,4 +346,4 @@ def signal_strength(self):
349346
"""
350347
if not self.esp.is_connected:
351348
self.connect()
352-
return self.esp.rssi
349+
return self.esp.ap_info.rssi

examples/esp32spi_ipconfig.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
except OSError as e:
7171
print("could not connect to AP, retrying: ", e)
7272
continue
73-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
73+
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
7474
ip1 = esp.ip_address
7575

7676
print("set ip dns")
@@ -91,9 +91,7 @@
9191
esp.pretty_ip(info["netmask"]),
9292
)
9393

94-
IP_ADDR = esp.pretty_ip(esp.ip_address)
95-
print("ip:", IP_ADDR)
96-
print("My IP address is", esp.pretty_ip(esp.ip_address))
94+
print("My IP address is", esp.ipv4_address)
9795
print("udp in addr: ", UDP_IN_ADDR, UDP_IN_PORT)
9896

9997
socketaddr_udp_in = pool.getaddrinfo(UDP_IN_ADDR, UDP_IN_PORT)[0][4]

examples/esp32spi_simpletest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@
6363

6464
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
6565
print("ESP32 found and in idle mode")
66-
print("Firmware vers.", esp.firmware_version.decode("utf-8"))
66+
print("Firmware vers.", esp.firmware_version)
6767
print("MAC addr:", ":".join("%02X" % byte for byte in esp.MAC_address))
6868

6969
for ap in esp.scan_networks():
70-
print("\t%-23s RSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
70+
print("\t%-23s RSSI: %d" % (ap.ssid, ap.rssi))
7171

7272
print("Connecting to AP...")
7373
while not esp.is_connected:
@@ -76,8 +76,8 @@
7676
except OSError as e:
7777
print("could not connect to AP, retrying: ", e)
7878
continue
79-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
80-
print("My IP address is", esp.pretty_ip(esp.ip_address))
79+
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
80+
print("My IP address is", esp.ipv4_address)
8181
print(
8282
"IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
8383
)

examples/esp32spi_simpletest_rp2040.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
print("MAC addr:", [hex(i) for i in esp.MAC_address])
4747

4848
for ap in esp.scan_networks():
49-
print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
49+
print("\t%s\t\tRSSI: %d" % (ap.ssid, ap.rssi))
5050

5151
print("Connecting to AP...")
5252
while not esp.is_connected:
@@ -55,8 +55,8 @@
5555
except OSError as e:
5656
print("could not connect to AP, retrying: ", e)
5757
continue
58-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
59-
print("My IP address is", esp.pretty_ip(esp.ip_address))
58+
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
59+
print("My IP address is", esp.ipv4_address)
6060
print(
6161
"IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
6262
)

examples/esp32spi_wpa2ent_simpletest.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ def normalize(v):
5959
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
6060
print("ESP32 found and in idle mode")
6161

62-
# Get the ESP32 fw version number, remove trailing byte off the returned bytearray
63-
# and then convert it to a string for prettier printing and later comparison
64-
firmware_version = "".join([chr(b) for b in esp.firmware_version[:-1]])
65-
print("Firmware vers.", firmware_version)
62+
# Get the ESP32 fw version number
63+
print("Firmware vers.", esp.firmware_version)
6664

6765
print("MAC addr:", [hex(i) for i in esp.MAC_address])
6866

6967
# WPA2 Enterprise support was added in fw ver 1.3.0. Check that the ESP32
7068
# is running at least that version, otherwise, bail out
7169
assert (
72-
version_compare(firmware_version, "1.3.0") >= 0
70+
version_compare(esp.firmware_version, "1.3.0") >= 0
7371
), "Incorrect ESP32 firmware version; >= 1.3.0 required."
7472

7573
# Set up the SSID you would like to connect to
@@ -98,8 +96,8 @@ def normalize(v):
9896
time.sleep(2)
9997

10098
print("")
101-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
102-
print("My IP address is", esp.pretty_ip(esp.ip_address))
99+
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
100+
print("My IP address is", esp.ipv4_address)
103101
print(
104102
"IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
105103
)

examples/gpio/esp32spi_gpio.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ def esp_init_pin_modes(din, dout):
6767

6868
esp_reset_all()
6969

70-
espfirmware = ""
71-
for _ in esp.firmware_version:
72-
if _ != 0:
73-
espfirmware += "{:c}".format(_)
74-
print("ESP32 Firmware:", espfirmware)
70+
print("ESP32 Firmware:", esp.firmware_version)
7571

7672
print(
7773
"ESP32 MAC: {5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}".format(

0 commit comments

Comments
 (0)