Once you've finished setting up your Feather ESP32-S2 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download to your computer as a zipped folder.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import os import ssl import wifi import socketpool import microcontroller import board import digitalio import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError aio_username = os.getenv("ADAFRUIT_AIO_USERNAME") aio_key = os.getenv("ADAFRUIT_AIO_KEY") # buttons button = digitalio.DigitalInOut(board.D5) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP button_state = False lights_on = False print() print("Connecting to WiFi...") # connect to your SSID wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) print("Connected to WiFi!") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) io = IO_HTTP(aio_username, aio_key, requests) try: feed_lights = io.get_feed("lights") except AdafruitIO_RequestError: feed_lights = io.create_new_feed("lights") while True: try: if not button.value and button_state: button_state = False if button.value and not button_state: print("pressed") lights_on = not lights_on io.send_data(feed_lights["key"], int(lights_on)) button_state = True except Exception as error: # pylint: disable=broad-except print(error) time.sleep(5) microcontroller.reset()
Upload the Code and Libraries to the Feather ESP32-S2
After downloading the Project Bundle, plug your Feather ESP32-S2 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Feather ESP32-S2's CIRCUITPY drive.
- lib folder
- code.py
Your Feather ESP32-S2 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
Add Your settings.toml File
As of CircuitPython 8.0.0, there is support for Environment Variables. Environment variables are stored in a settings.toml file. Similar to secrets.py, the settings.toml file separates your sensitive information from your main code.py file. Add your settings.toml file as described in the Create Your settings.toml File page earlier in this guide. You'll need to include your ADAFRUIT_AIO_USERNAME
, ADAFRUIT_AIO_KEY
, CIRCUITPY_WIFI_SSID
and CIRCUITPY_WIFI_PASSWORD
.
CIRCUITPY_WIFI_SSID = "your-ssid-here" CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here" ADAFRUIT_AIO_USERNAME = "your-username-here" ADAFRUIT_AIO_KEY = "your-key-here"
How the CircuitPython Code Works
The code begins by setting up the button as an input on pin D5. The state lights_on
is set to False
.
button = digitalio.DigitalInOut(board.D5) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP button_state = False lights_on = False
WiFi and IO
Next, the board connects to WiFi using your SSID and password information from your settings.toml file. After a connection is established, the board connects to Adafruit IO using HTTP and fetches the feed lights
. If the feed does not exist, it creates a new feed called lights
.
aio_username = os.getenv("ADAFRUIT_AIO_USERNAME") aio_key = os.getenv("ADAFRUIT_AIO_KEY") print() print("Connecting to WiFi...") # connect to your SSID wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) print("Connected to WiFi!") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) io = IO_HTTP(aio_username, aio_key, requests) try: feed_lights = io.get_feed("lights") except AdafruitIO_RequestError: feed_lights = io.create_new_feed("lights")
The Loop
In the loop, if the button is pressed it toggles the state of lights_on
. The value of lights_on
is sent to the lights
feed as an integer (0
for False
, 1
for True
).
The rest of the magic takes place with the itsaSNAP app and iOS Shortcuts. Make sure you follow along with the steps on the iOS Shortcut Setup page to fully complete the project.
while True: try: if not button.value and button_state: button_state = False if button.value and not button_state: print("pressed") lights_on = not lights_on io.send_data(feed_lights["key"], int(lights_on)) button_state = True except Exception as error: # pylint: disable=broad-except print(error) time.sleep(5) microcontroller.reset()
Page last edited January 22, 2025
Text editor powered by tinymce.