It's easy to use the Adafruit AirLift breakout with CircuitPython and the Adafruit CircuitPython ESP32SPI module. This module allows you to easily add WiFi to your project.
The ESP32's pins on the Metro M7 are as follows:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
CircuitPython Setup
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next you'll need to install the necessary libraries to use the hardware. Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and files:
- /adafruit_bus_device
- /adafruit_esp32spi
- adafruit_requests.mpy
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT from os import getenv import board import busio from digitalio import DigitalInOut import adafruit_connection_manager import adafruit_requests from adafruit_esp32spi import adafruit_esp32spi # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD secrets = { "ssid": getenv("CIRCUITPY_WIFI_SSID"), "password": getenv("CIRCUITPY_WIFI_PASSWORD"), } if secrets == {"ssid": None, "password": None}: try: # Fallback on secrets.py until depreciation is over and option is removed from secrets import secrets except ImportError: print("WiFi secrets are kept in settings.toml, please add them there!") raise print("ESP32 SPI webclient test") TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) # If you have an AirLift Shield: # esp32_cs = DigitalInOut(board.D10) # esp32_ready = DigitalInOut(board.D7) # esp32_reset = DigitalInOut(board.D5) # If you have an AirLift Featherwing or ItsyBitsy Airlift: # esp32_cs = DigitalInOut(board.D13) # esp32_ready = DigitalInOut(board.D11) # esp32_reset = DigitalInOut(board.D12) # If you have an externally connected ESP32: # NOTE: You may need to change the pins to reflect your wiring # esp32_cs = DigitalInOut(board.D9) # esp32_ready = DigitalInOut(board.D10) # esp32_reset = DigitalInOut(board.D5) # Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040 if "SCK1" in dir(board): spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1) else: spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: print("ESP32 found and in idle mode") 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" % (ap.ssid, ap.rssi)) print("Connecting to AP...") while not esp.is_connected: try: esp.connect_AP(secrets["ssid"], secrets["password"]) except OSError as e: print("could not connect to AP, retrying: ", e) continue 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")) ) print("Ping google.com: %d ms" % esp.ping("google.com")) # esp._debug = True print("Fetching text from", TEXT_URL) r = requests.get(TEXT_URL) print("-" * 40) print(r.text) print("-" * 40) r.close() print() print("Fetching json from", JSON_URL) r = requests.get(JSON_URL) print("-" * 40) print(r.json()) print("-" * 40) r.close() print("Done!")
Connect to the serial console to see the output. It should look something like the following:
Make sure you see the same output! If you don't, check your wiring. Note that we've changed the pinout in the code example above to reflect the CircuitPython Microcontroller Pinout at the top of this page.
Once you've succeeded, continue onto the next page!
Text editor powered by tinymce.