Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active September 24, 2019 05:06
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/d3bcfa538a0375e6c1b30c80e2f4fbad to your computer and use it in GitHub Desktop.
Save anecdata/d3bcfa538a0375e6c1b30c80e2f4fbad to your computer and use it in GitHub Desktop.
ESP32SPI find and connect to an open wi-fi AP
import board
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
def esp_reset():
try:
esp.reset()
print('ESP Reset')
except RuntimeError as e:
print('ESP Reset Error:', e)
def esp_connect(ssid, pw=False):
esp_status = 255
try:
esp_status = esp.status
except RuntimeError as e:
print('ESP Status Error:', e)
esp_reset()
try:
esp_is_connected = esp.is_connected
except RuntimeError as e:
print('ESP Is Connected Error:', e)
esp_reset()
# these should never disagree?
if esp_status == 3 and esp_is_connected:
pass # already connected
else:
try:
print('ESP Connecting...', end=' ')
esp.connect_AP(ssid, pw)
esp_status = esp.status
esp_is_connected = esp.is_connected
print(esp_status, esp_is_connected)
except RuntimeError as e:
print('ESP Connection Error:', e)
esp_reset()
return esp_status, esp_is_connected
print('ESP32...')
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 & Bitsy Add-On
esp32_cs = DigitalInOut(board.D13) # 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)
espfw = ''
for _ in esp.firmware_version:
espfw += '{:c}'.format(_)
print('ESP Firmware: ', espfw)
print('ESP MAC addr: {5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}'.format(*esp.MAC_address))
print('ESP Status: ', esp.status, esp.is_connected)
print("Scanning WiFi...")
while True:
try:
found = False
best_open_ap = None
for ap in esp.scan_networks():
print(' ', ap)
# scan results are ordered from best to worst RSSI
if not found and ap['encryption'] == 7:
found = True
best_open_ap = ap['ssid']
break
except RuntimeError as e:
print('ESP Scanning Error:', e)
esp_reset()
if best_open_ap:
while True:
try:
if esp_connect(best_open_ap) == (3, True):
print('RSSI: ', esp.rssi)
print('SSID: ', str(esp.ssid, 'utf-8'))
print('IP: ', esp.pretty_ip(esp.ip_address))
print('Netmask: %d.%d.%d.%d' % (esp.network_data['netmask'][0], esp.network_data['netmask'][1], esp.network_data['netmask'][2], esp.network_data['netmask'][3]))
print('Gateway: %d.%d.%d.%d' % (esp.network_data['gateway'][0], esp.network_data['gateway'][1], esp.network_data['gateway'][2], esp.network_data['gateway'][3]))
print('LAN ping: %dms' % esp.ping(esp.network_data['gateway']))
break
except RuntimeError as e:
print('ESP Error:', e)
esp_reset()
else:
print('No open wi-fi networks found.')
print('Done.')
@anecdata
Copy link
Author

Adafruit CircuitPython 5.0.0-alpha.4 on 2019-09-15; Adafruit PyPortal with samd51j20
adafruit-circuitpython-bundle-5.x-mpy-20190917

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment