import board from digitalio import DigitalInOut import busio from adafruit_esp32spi import adafruit_esp32spi from adafruit_esp32spi import adafruit_esp32spi_socket import time import supervisor import sys def format_bssid(bssid_array): bssid = '' for i in range(len(bssid_array)-1, -1, -1): bssid += '{:02X}:'.format(bssid_array[i]) bssid = bssid[:-1] return bssid def connect_disconnect_ap(esp, ssids, host, port): fwstr = str(esp.firmware_version, 'utf-8') major, minor, sub = sys.implementation.version print("Circuitpython {}.{}.{}, ESP32 firmware {}".format(major, minor, sub, fwstr)) test_ip_str = '192.168.43.148' test_ip = esp.unpretty_ip(test_ip_str) status = {adafruit_esp32spi.WL_IDLE_STATUS: 'Idle', adafruit_esp32spi.WL_CONNECTED: 'Connected', adafruit_esp32spi.WL_DISCONNECTED: 'Disconnected'} for ssid, password in ssids: print("ESP32 status {}".format(status[esp.status])) print("connecting to {}".format(ssid)) esp.connect_AP(ssid, password, timeout_s = 30) print("ESP32 status {}".format(status[esp.status])) print("ESP32 bssid {}".format(format_bssid(esp.bssid))) print("ESP32 ssid {}".format(str(esp.ssid, 'utf-8'))) net_data = esp.network_data print("ESP32 ip_addr {}".format(esp.pretty_ip(net_data['ip_addr']))) print("ESP32 netmask {}".format(esp.pretty_ip(net_data['netmask']))) print("ESP32 gateway {}".format(esp.pretty_ip(net_data['gateway']))) adafruit_esp32spi_socket.set_interface(esp) my_sock = adafruit_esp32spi_socket.socket() print("ESP32 Busy {}".format(esp32_ready_pin.value)) print("connecting to {} port {}".format(test_ip_str, port)) try: my_sock.connect((test_ip, port)) if my_sock.connected(): print("Connected to {}".format(test_ip_str)) my_sock.close() else: print("Connection failed") except RuntimeError as e: sys.print_exception(e) test_ESP_Busy('timeout') print() print("Continuing ...") try: e_status = esp.status except RuntimeError as e: sys.print_exception(e) test_ESP_Busy('timeout') print() print("Continuing ...") print("ESP32 status {}".format(status[e_status])) if e_status == adafruit_esp32spi.WL_CONNECTED: print("Disconnecting") esp.disconnect() print("ESP32 status {}\n\n".format(status[esp.status])) def test_ESP_Busy(name): if esp32_ready_pin.value: print("ESP Busy after {:20s}\r".format(name), end='') start_busy_ns = time.monotonic_ns() current_busy_ns = start_busy_ns while True: come_up_for_air_ns = current_busy_ns + (10**9) while current_busy_ns < come_up_for_air_ns: current_busy_ns = time.monotonic_ns() if not esp32_ready_pin.value: break print("ESP Busy after {:35s} waiting {} seconds\r".format(name, (current_busy_ns - start_busy_ns) / 1e9), end='') if not esp32_ready_pin.value: break print("ESP Busy after {:35s} lasted {} seconds".format(name, (current_busy_ns - start_busy_ns) / 1e9)) else: print("ESP Ready after {:35s}".format(name)) esp32_cs_pin = DigitalInOut(board.ESP_CS) esp32_ready_pin = DigitalInOut(board.ESP_BUSY) esp32_reset_pin = DigitalInOut(board.ESP_RESET) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol( spi, esp32_cs_pin, esp32_ready_pin, esp32_reset_pin, ) host = 'raspberrypi.lan' port = 65432 ssids = [('linkme', '**********'), ('corrigan', '**********')] connect_disconnect_ap(esp, ssids, host, port) =============================================================================== code.py output: Circuitpython 6.1.0, ESP32 firmware 1.7.3 ESP32 status Idle connecting to linkme ESP32 status Connected ESP32 bssid 00:14:BF:A7:7D:62 ESP32 ssid linkme ESP32 ip_addr 192.168.23.156 ESP32 netmask 255.255.255.0 ESP32 gateway 192.168.23.1 ESP32 Busy False connecting to 192.168.43.148 port 65432 Connected to 192.168.43.148 ESP32 status Connected Disconnecting ESP32 status Disconnected ESP32 status Disconnected connecting to corrigan ESP32 status Connected ESP32 bssid 7E:D2:94:46:48:E5 ESP32 ssid corrigan ESP32 ip_addr 192.168.23.156 ESP32 netmask 255.255.255.0 ESP32 gateway 192.168.23.1 ESP32 Busy False connecting to 192.168.43.148 port 65432 Traceback (most recent call last): File "code.py", line 52, in connect_disconnect_ap File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 75, in connect File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 783, in socket_connect File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 685, in socket_open File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 345, in _send_command_get_response File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 296, in _wait_response_cmd File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 199, in _wait_for_ready RuntimeError: ESP32 not responding ESP Busy after timeout lasted 8.63928 seconds Continuing ... Traceback (most recent call last): File "code.py", line 65, in connect_disconnect_ap File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 355, in status File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 345, in _send_command_get_response File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 328, in _wait_response_cmd File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 312, in _wait_response_cmd File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 286, in _wait_spi_char RuntimeError: Timed out waiting for SPI char ESP Ready after timeout Continuing ... ESP32 status Connected Disconnecting Traceback (most recent call last): File "code.py", line 114, in File "code.py", line 75, in connect_disconnect_ap File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 530, in disconnect File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 345, in _send_command_get_response File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 328, in _wait_response_cmd File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 312, in _wait_response_cmd File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 286, in _wait_spi_char RuntimeError: Timed out waiting for SPI char Code done running.