Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active September 21, 2019 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anecdata/52d5ba962f172e1300432e5efa9fd402 to your computer and use it in GitHub Desktop.
Save anecdata/52d5ba962f172e1300432e5efa9fd402 to your computer and use it in GitHub Desktop.
import board
import time
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from secrets import secrets
# AP connect helper
def esp_connect():
esp_status = 255
try:
esp_is_connected = esp.is_connected
except RuntimeError as e:
print('ESP Is Connected Error: ', e)
try:
esp_status = esp.status
except RuntimeError as e:
print('ESP Status Error: ', e)
# these should always agree?
if esp_is_connected and (esp_status == 3):
pass # already connected
else:
try:
print('Connecting... ', end='')
""" 'Will retry up to 10 times and return on success
or raise an exception on failure' (1s b/t retries) """
esp.connect_AP(secrets['ssid'], secrets['password'])
esp_status = esp.status
print(esp_status)
except RuntimeError as e:
print('ESP Error: ', e)
return esp_status
print()
spi = board.SPI()
try: # PyPortal, etc.
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
except AttributeError: # Airlift FeatherWing compatible
esp32_cs = DigitalInOut(board.D13) # SAMD51/M4 Red LED
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
try:
espfw = ''
for _ in esp.firmware_version:
espfw += '{:c}'.format(_)
print('ESP Firmware: ', espfw)
esp_status = esp.status
print('ESP Status: ', esp_status)
except RuntimeError as e:
print('ESP Error:', e)
try:
esp_connect()
if esp.is_connected:
print(' BSSID RSSI SSID')
# NEW: bssid
print('{5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}'.format(*esp.bssid), end='')
print(' {: 4d} {:s}'.format(esp.rssi, str(esp.ssid, 'utf-8')), end='')
print(' ping=%dms (LAN)' % esp.ping(esp.network_data['gateway']))
except RuntimeError as e:
print('ESP Error:', e)
print()
while True:
print('Scanning... BSSID Ch RSSI SSID')
try:
for ap in esp.scan_networks():
# NEW: bssid, channel
print('{5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}'.format(*ap['bssid']), end='')
print(' {: 3d} {: 4d} {:s}'.format(ap['channel'], ap['rssi'], str(ap['ssid'], 'utf-8')))
except (RuntimeError, KeyError) as e:
print('ESP Error:', e)
print()
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment