Skip to content

Commit 2773887

Browse files
authored
Merge pull request adafruit#1263 from adafruit/party_parrot
Adding party parrot code
2 parents acbe731 + 03c8f0f commit 2773887

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

IoT_Party_Parrot/code.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import time
2+
import board
3+
import displayio
4+
from adafruit_matrixportal.matrixportal import MatrixPortal
5+
from adafruit_matrixportal.matrix import Matrix
6+
import adafruit_imageload
7+
8+
# Get wifi details and more from a secrets.py file
9+
try:
10+
from secrets import secrets
11+
except ImportError:
12+
print("WiFi secrets are kept in secrets.py, please add them there!")
13+
raise
14+
15+
print("Party Parrot Twitter Matrix")
16+
17+
# import your bearer token
18+
bear = secrets['bearer_token']
19+
20+
# query URL for tweets. looking for hashtag partyparrot sent to a specific username
21+
DATA_SOURCE = ('https://api.twitter.com/2/tweets/search/recent?query=#partyparrot to:blitzcitydiy')
22+
# json data path to get most recent tweet's ID number
23+
DATA_LOCATION = ["meta", "newest_id"]
24+
25+
# create MatrixPortal object to grab data/connect to internet
26+
matrixportal = MatrixPortal(
27+
url=DATA_SOURCE,
28+
json_path=DATA_LOCATION,
29+
status_neopixel=board.NEOPIXEL
30+
)
31+
32+
# create matrix display
33+
matrix = Matrix(width=32, height=32)
34+
display = matrix.display
35+
36+
group = displayio.Group(max_size=20)
37+
38+
# load in party parrot bitmap
39+
parrot_bit, parrot_pal = adafruit_imageload.load("/partyParrotsTweet.bmp",
40+
bitmap=displayio.Bitmap,
41+
palette=displayio.Palette)
42+
43+
parrot_grid = displayio.TileGrid(parrot_bit, pixel_shader=parrot_pal,
44+
width=1, height=1,
45+
tile_height=32, tile_width=32,
46+
default_tile=10,
47+
x=0, y=0)
48+
49+
group.append(parrot_grid)
50+
51+
display.show(group)
52+
53+
# add bearer token as a header to request
54+
matrixportal.set_headers({'Authorization': 'Bearer ' + bear})
55+
56+
last_value = 0 # checks last tweet's ID
57+
check = 0 # time.monotonic() holder
58+
parrot = False # state to track if an animation is currently running
59+
party = 0 # time.monotonic() holder
60+
p = 0 # index for tilegrid
61+
party_count = 0 # count for animation cycles
62+
63+
while True:
64+
# every 30 seconds...
65+
if (check + 30) < time.monotonic():
66+
# store most recent tweet's ID number in value
67+
value = matrixportal.fetch()
68+
print("Response is", value)
69+
# reset time count
70+
check = time.monotonic()
71+
# compare last tweet ID and current tweet ID
72+
if last_value != value:
73+
print("new party!")
74+
# if it's new, then it's a party!
75+
last_value = value
76+
parrot = True
77+
else:
78+
# if it's not new, then the wait continues
79+
print("no new party... :(")
80+
# when a new tweet comes in...
81+
if parrot:
82+
# every 0.1 seconds...
83+
if (party + 0.1) < time.monotonic():
84+
# the party parrot animation cycles
85+
parrot_grid[0] = p
86+
# p is the tilegrid index location
87+
p += 1
88+
party = time.monotonic()
89+
# if an animation cycle ends
90+
if p > 9:
91+
# index is reset
92+
p = 0
93+
# animation cycle count is updated
94+
party_count += 1
95+
print("party parrot", party_count)
96+
# after 16 animations cycles...
97+
if party_count > 15:
98+
# reset states
99+
parrot = False
100+
party_count = 0
101+
p = 0
102+
# clear the matrix so that it's blank
103+
parrot_grid[0] = 10
104+
print("the party is over")
11.1 KB
Binary file not shown.

IoT_Party_Parrot/secrets.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is where you keep secret settings, passwords, and tokens!
2+
# If you put them in the code you risk committing that info or sharing it
3+
4+
secrets = {
5+
'ssid' : 'insert your network name here',
6+
'password' : 'insert your network password here',
7+
'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
8+
'twitter_api_key' : 'insert your twitter api key here',
9+
'twitter_secret_key' : 'insert your twitter secret key here',
10+
'bearer_token' : 'insert your bearer token here'
11+
}

0 commit comments

Comments
 (0)