To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, 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, open the directory IoT_Party_Parrot/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
CIRCUITPY
# SPDX-FileCopyrightText: 2020 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
from os import getenv
import time
import board
import displayio
from adafruit_matrixportal.matrixportal import MatrixPortal
from adafruit_matrixportal.matrix import Matrix
import adafruit_imageload
print("Party Parrot Twitter Matrix")
# Get WiFi details, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
if None in [ssid, password]:
raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# import your bearer token
bear = getenv('bearer_token')
# query URL for tweets. looking for hashtag partyparrot sent to a specific username
DATA_SOURCE = 'https://api.twitter.com/2/tweets/search/recent?query=#partyparrot to:blitzcitydiy'
# json data path to get most recent tweet's ID number
DATA_LOCATION = ["meta", "newest_id"]
# create MatrixPortal object to grab data/connect to internet
matrixportal = MatrixPortal(
url=DATA_SOURCE,
json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL
)
# create matrix display
matrix = Matrix(width=32, height=32)
display = matrix.display
group = displayio.Group()
# load in party parrot bitmap
parrot_bit, parrot_pal = adafruit_imageload.load("/partyParrotsTweet.bmp",
bitmap=displayio.Bitmap,
palette=displayio.Palette)
parrot_grid = displayio.TileGrid(parrot_bit, pixel_shader=parrot_pal,
width=1, height=1,
tile_height=32, tile_width=32,
default_tile=10,
x=0, y=0)
group.append(parrot_grid)
display.root_group = group
# add bearer token as a header to request
matrixportal.set_headers({'Authorization': 'Bearer ' + bear})
last_value = 0 # checks last tweet's ID
check = 0 # time.monotonic() holder
parrot = False # state to track if an animation is currently running
party = 0 # time.monotonic() holder
p = 0 # index for tilegrid
party_count = 0 # count for animation cycles
while True:
# every 30 seconds...
if (check + 30) < time.monotonic():
# store most recent tweet's ID number in value
value = matrixportal.fetch()
print("Response is", value)
# reset time count
check = time.monotonic()
# compare last tweet ID and current tweet ID
if last_value != value:
print("new party!")
# if it's new, then it's a party!
last_value = value
parrot = True
else:
# if it's not new, then the wait continues
print("no new party... :(")
# when a new tweet comes in...
if parrot:
# every 0.1 seconds...
if (party + 0.1) < time.monotonic():
# the party parrot animation cycles
parrot_grid[0] = p
# p is the tilegrid index location
p += 1
party = time.monotonic()
# if an animation cycle ends
if p > 9:
# index is reset
p = 0
# animation cycle count is updated
party_count += 1
print("party parrot", party_count)
# after 16 animations cycles...
if party_count > 15:
# reset states
parrot = False
party_count = 0
p = 0
# clear the matrix so that it's blank
parrot_grid[0] = 10
print("the party is over")
Page last edited January 21, 2025
Text editor powered by tinymce.