esp32 demo with st7789
schematic
https://github.com/mayphus/esp32_st7789
use esp32-s3's SPI (page 20 4c
) to connect to st7789 display
code
import board
import displayio
import busio
import wifi
import ssl
import socketpool
import adafruit_requests
from displayio import (
release_displays,
FourWire,
)
from adafruit_st7789 import ST7789
import terminalio
from adafruit_display_text import label
import time
# Release any existing displays
release_displays()
# Configure SPI pins for ESP32-S3
spi = busio.SPI(clock=board.GPIO12, MOSI=board.GPIO13)
tft_cs = board.GPIO10 # Chip Select
tft_dc = board.GPIO16 # Data/Command
tft_reset = board.GPIO15 # Reset
# Create the display bus
display_bus = FourWire(
spi,
command=tft_dc,
chip_select=tft_cs,
reset=tft_reset,
baudrate=24000000,
)
# Create the display
display = ST7789(
display_bus,
width=280,
height=240,
rowstart=20,
rotation=90,
)
# Create a display group
splash = displayio.Group()
display.root_group = splash
# WiFi credentials - replace with your details
WIFI_SSID = ".yY"
WIFI_PASSWORD = "edxcDfda3W,.@rzanxhSvna"
# Use Advice Slip API instead
QUOTE_URL = "https://api.adviceslip.com/advice"
# Connect to WiFi
print("Connecting to WiFi...")
wifi.radio.connect(WIFI_SSID, WIFI_PASSWORD)
print("Connected!")
# Create a socket pool and requests session
pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()
requests = adafruit_requests.Session(pool, ssl_context)
def wrap_text(text, width, font):
"""Wrap text to fit display width"""
words = text.split()
lines = []
current_line = []
current_width = 0
for word in words:
word_width = len(word) * 6 # Approximate width for terminalio font
if current_width + word_width <= width:
current_line.append(word)
current_width += word_width + 6 # Add space width
else:
lines.append(' '.join(current_line))
current_line = [word]
current_width = word_width + 6
if current_line:
lines.append(' '.join(current_line))
return '\n'.join(lines)
# Create text labels
quote_label = label.Label(
terminalio.FONT,
text="Loading...",
color=0xFFFFFF,
x=20,
y=50,
scale=1,
)
splash.append(quote_label)
# Main loop
while True:
try:
# Fetch advice data
response = requests.get(QUOTE_URL)
advice_data = response.json()
print(advice_data)
# Update display
advice = advice_data["slip"]["advice"]
# Wrap text to fit display width (240 pixels minus margins)
wrapped_text = wrap_text(advice, 200, terminalio.FONT)
quote_label.text = f'"{wrapped_text}"'
except Exception as e:
print("Error fetching advice:", e)
quote_label.text = "Error"
# Wait for 1 minute before updating again
time.sleep(60)