Once you have CircuitPython setup and libraries installed we can get your board connected to the Internet. Note that access to enterprise level secured WiFi networks is not currently supported, only WiFi networks that require SSID and password.
To get connected, you will need to start by creating a secrets file.
We expect people to share tons of projects as they build CircuitPython WiFi widgets. What we want to avoid is people accidentally sharing their passwords or secret tokens and API keys. So, we designed all our examples to use a secrets.py
file, that is in your CIRCUITPY drive, to hold secret/private/custom data. That way you can share your main project without worrying about accidentally sharing private stuff.
Your secrets.py file should look like this:
# This file is where you keep secret settings, passwords, and tokens! # If you put them in the code you risk committing that info or sharing it secrets = { 'ssid' : 'home ssid', 'password' : 'my password', 'id_scope' : '0101018D', 'device_id' : 'pyportal', 'key' : 'MyK3y==' }
Inside is a python dictionary named secrets with a line for each entry. Each entry has an entry name (say 'ssid'
) and then a colon to separate it from the entry key 'home ssid'
and finally a comma ,
At a minimum you'll need the ssid
and password
for your local WiFi setup. As you make projects you may need more tokens and keys, just add them one line at a time. See for example other tokens such as ones needed to access Azure IoT Central. Other non-secret data can also go here, just cause its called secrets doesn't mean you can't have general customization data in there!
Of course, don't share your secrets.py - keep that out of GitHub, Discord or other project-sharing sites. For example, if you are using git, you should add it to your .gitignore file.
OK now you have your secrets file set up, then you can look to connecting to the internet. Lets use the ESP32SPI and the Requests libraries - you'll need to visit the CircuitPython bundle and install:
- adafruit_bus_device
- adafruit_esp32spi
- adafruit_requests
- neopixel
Into your lib folder. Once that's done, load up the following example using Visual Studio Code, Mu or your favorite editor:
import board import busio from digitalio import DigitalInOut import neopixel from adafruit_esp32spi import adafruit_esp32spi from adafruit_esp32spi import adafruit_esp32spi_wifimanager from secrets import secrets print("ESP32 SPI webclient test") # Get the pins for the PyPortal esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) # Initialize the ESP over SPI spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) # Configure the status light status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Connect the WiFi manager wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi.connect() # Log the connection details print("Connected to", str(esp.ssid, "utf-8")) print("My IP address is", esp.pretty_ip(esp.ip_address)) # Look up some sites to validate the connection print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))) print("Ping Bing.com: %d ms" % esp.ping("bing.com"))
And save it to your board, with the name code.py.
You should see something like the following on the screen on your PyPortal:
ESP32 SPI webclient test Connected to MySSID My IP address is 192.168.0.1 IP lookup adafruit.com: 104.20.39.240 Ping Bing.com: 10 ms
Text editor powered by tinymce.