After completing the setup of your MatrixPortal S3 with CircuitPython, you can obtain the required code and libraries by downloading the Project Bundle.
Simply click the Download Project Bundle button located in the window below. The bundle will then be saved to your computer.
# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries # # SPDX-License-Identifier: MIT import os import time import ssl import wifi import board import terminalio import socketpool from adafruit_matrixportal.matrixportal import MatrixPortal import adafruit_requests SCROLL_DELAY = 0.03 time_interval = 5 text_color = 0xFC6900 # e.g., Retro Orange BASE_URL = "https://api.nytimes.com/svc/topstories/v2/" CATEGORY = "arts" # Change this to whatever category you want # The following values are allowed: # arts, automobiles, books/review, business, fashion, food, health, home, insider, magazine, movies, # nyregion, obituaries, opinion, politics, realestate, science, sports, sundayreview, technology, # theater, t-magazine, travel, upshot, us, world # --- Wi-Fi setup --- wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}") # --- Display setup --- matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True) matrixportal.add_text( text_font=terminalio.FONT, text_position=(0, (matrixportal.graphics.display.height // 2) - 1), scrolling=True, ) NYT_header_text_area = matrixportal.add_text( text_font=terminalio.FONT, text_position=(0, (matrixportal.graphics.display.height // 6) - 1), ) matrixportal.set_text("NYT:", NYT_header_text_area) # --- Networking setup --- context = ssl.create_default_context() with open("/api-nytimes-com-chain.pem", "rb") as certfile: context.load_verify_locations(cadata=certfile.read()) pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, context) NYT_API_KEY = os.getenv("NYT_API_KEY") DATA_SOURCE = BASE_URL + CATEGORY + ".json?api-key=" + NYT_API_KEY # --- Main Loop --- while True: print("Fetching json from", DATA_SOURCE) response = requests.get(DATA_SOURCE) titles = [result["title"] for result in response.json()["results"]] for title in titles: matrixportal.set_text(title) matrixportal.set_text_color(text_color) matrixportal.scroll_text(SCROLL_DELAY) time.sleep(time_interval)
Upload the Code to the MatrixPortal S3
Once you have downloaded the Project Bundle, connect your MatrixPortal S3 to your computer's USB port using a reliable USB data+power cable. Check your computer's File Explorer or Finder (depending on your operating system) for a CIRCUITPY folder.
Unzip the folder and transfer the lib folder and code.py file to the MatrixPortal S3 CIRCUITPY drive. After copying these files, your MatrixPortal S3 CIRCUITPY drive should resemble the following.
import os import time import ssl import wifi import board import terminalio import socketpool from adafruit_matrixportal.matrixportal import MatrixPortal import adafruit_requests
This is where the necessary libraries are imported. These libraries provide the functionality to connect to a WiFi network, make HTTPS requests, and display text on the MatrixPortal.
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
This portion focuses on collecting WiFi credentials and establishing a connection using the SSID and password provided through environment variables. If you've not yet set up your credentials, be sure to do so here: Setting up your Credentials.
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True) matrixportal.add_text( text_font=terminalio.FONT, text_position=(0, (matrixportal.graphics.display.height // 2) - 1), scrolling=True, ) NYT_header_text_area = matrixportal.add_text( text_font=terminalio.FONT, text_position=(0, (matrixportal.graphics.display.height // 6) - 1), ) matrixportal.set_text("NYT:", NYT_header_text_area)
Here, the script sets up the MatrixPortal display, including creating text areas for scrolling text and the static NYT header.
context = ssl.create_default_context() with open("/api-nytimes-com-chain.pem", "rb") as certfile: context.load_verify_locations(cadata=certfile.read()) pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, context) NYT_API_KEY = os.getenv("NYT_API_KEY") DATA_SOURCE = BASE_URL + CATEGORY + ".json?api-key=" + NYT_API_KEY This section sets up the network context with the necessary SSL certificates and prepares for making HTTP requests.
This section sets up the network context with the necessary SSL certificates and prepares for making HTTPS requests.
while True: print("Fetching json from", DATA_SOURCE) response = requests.get(DATA_SOURCE) titles = [result["title"] for result in response.json()["results"]] for title in titles: matrixportal.set_text(title) matrixportal.set_text_color(text_color) matrixportal.scroll_text(SCROLL_DELAY) time.sleep(time_interval)
In the main loop, the script continuously fetches data from the New York Times API, extracts the titles of articles from the received JSON, and displays these titles one by one on the MatrixPortal display, scrolling each title from right to left.
After displaying all the titles, the script pauses for time_interval
seconds before fetching and displaying the titles again.
Page last edited January 22, 2025
Text editor powered by tinymce.