The Adafruit ESP32-S2 TFT Feather comes with WiFi capabilities alongside the 1.14" 240x135pixel color TFT display. This example shows how to use the WiFi to query GitHub for the number of folks who have starred the CircuitPython project, and display the data with a lovely background on the TFT.
GitHub Stars WiFi Demo Secrets
This example requires you to provide your Wi-Fi credentials. To do this, you'll want to create a secrets.py file on your CIRCUITPY drive, with the following structure.
Update the following with your info, keeping the "
around them:
- Replace
"your-wifi-ssid"
with your Wi-Fi SSID - Replace
"your-wifi-password"
with your Wi-Fi password
Your secrets.py file should look like the code below, but with the above updated info.
# 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": "your-wifi-ssid", "password": "your-wifi-password", }
Set Up CIRCUITPY
Update your code.py to the following. 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 images folder, the entire fonts folder, the entire lib folder, and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY drive should resemble the following.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ CircuitPython GitHub Stars viewer """ import time import ssl import wifi import socketpool import displayio import board from adafruit_display_text import bitmap_label from adafruit_bitmap_font import bitmap_font import adafruit_requests # Get WiFi details secrets.py file try: from secrets import secrets except ImportError: print("WiFi secrets are kept in secrets.py, please add them there!") raise display = board.DISPLAY # URL to fetch from JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython" # Set up background image and text bitmap = displayio.OnDiskBitmap("/images/stars_background.bmp") tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader) group = displayio.Group() group.append(tile_grid) font = bitmap_font.load_font("/fonts/Arial-Bold-36.bdf") text_area = bitmap_label.Label(font, text="----", color=0xFFFFFF) text_area.x = 135 text_area.y = 90 group.append(text_area) display.root_group = group # Connect to WiFi print("Connecting to %s"%secrets["ssid"]) wifi.radio.connect(secrets["ssid"], secrets["password"]) print("Connected to %s!"%secrets["ssid"]) print("My IP address is", wifi.radio.ipv4_address) pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) while True: print("Fetching and parsing json from", JSON_STARS_URL) response = requests.get(JSON_STARS_URL) stars = response.json()["stargazers_count"] print("-" * 40) print("CircuitPython GitHub Stars", stars) print("-" * 40) text_area.text = str(stars) time.sleep(120)
Once everything is copied over, the background will show up on the display with ---- in the lower right.
Give it a moment to connect to WiFi and query GitHub. The ---- will be replaced with the number of stars!
Code Walkthrough
First you import all the necessary modules and libraries, including the secrets.py file which should have your WiFi credentials in it.
Next, you set up the display.
Then you provide the URL from which to fetch the data.
Next, you set up the background image, font, and text display.
After that, you connect to the WiFi.
Inside the loop, you fetch the data from the provided URL, specify the data you're looking for (stargazer_count
), print the data to the serial console, and update the text on the display.
Finally, you wait 120 seconds before querying again, and updating the text with the latest information.
That's all there is to connecting to WiFi and displaying JSON data on the TFT using CircuitPython!
Page last edited January 22, 2025
Text editor powered by tinymce.