Skip to content

Instantly share code, notes, and snippets.

@jfurcean
Last active March 15, 2021 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfurcean/bfcafc200a433f2d31b736d41d9202e8 to your computer and use it in GitHub Desktop.
Save jfurcean/bfcafc200a433f2d31b736d41d9202e8 to your computer and use it in GitHub Desktop.
Display COVID-19 Vaccine Data on a Adafruit MagTag
import time
import alarm
import supervisor
import alarm
from adafruit_magtag.magtag import MagTag
# Change this to the hour you want to check the data at, for us its 8pm
# local time (eastern), which is 20:00 hrs
DAILY_UPDATE_HOUR = 20
# Set up where we'll be fetching data from
DATA_SOURCE = "https://covid.cdc.gov/covid-data-tracker/COVIDData/getAjaxData?id=vaccination_data"
# 0 is USA
LOCATION_NUM = 0
DATE_LOCATION = ["vaccination_data", LOCATION_NUM, 'Date']
NAME_LOCATION = ["vaccination_data", LOCATION_NUM, 'LongName']
CENSUS_LOACTION = ["vaccination_data", LOCATION_NUM, 'Census2019']
ADMINSTERED = ["vaccination_data", LOCATION_NUM, 'Doses_Administered']
SERIES_COMPLETE = ["vaccination_data", LOCATION_NUM, 'Series_Complete_Yes']
COMPLETE_PERCENT = ["vaccination_data", LOCATION_NUM, 'Series_Complete_Pop_Pct']
magtag = MagTag(
url=DATA_SOURCE,
json_path=(DATE_LOCATION, NAME_LOCATION,
CENSUS_LOACTION,ADMINSTERED, SERIES_COMPLETE,
COMPLETE_PERCENT),
)
# Date stamp of info
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 15),
text_transform=lambda x: "Date: {}".format(x[0:10]),
)
# Location Country/State
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 35),
text_transform=lambda x: "Location: {}".format(x),
)
# Population
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 55),
text_transform=lambda x: "Population: {:,}".format(x),
)
# Doses Administered
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 75),
text_transform=lambda x: "Administered: {:,}".format(x),
)
# Number Complete
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 95),
text_transform=lambda x: "Full Vaccinated: {:,}".format(x),
)
# Percent Fully Vaccinated
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 115),
text_transform=lambda x: "Percent: {:.2f}%".format(x),
)
# updated time
magtag.add_text(
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(245, 30),
line_spacing=0.75,
is_data=False
)
magtag.graphics.qrcode(b"https://covidtracking.com/data",
qr_size=2, x=240, y=70)
magtag.peripherals.neopixels.brightness = 0.1
magtag.peripherals.neopixel_disable = False # turn on lights
magtag.peripherals.neopixels.fill(0x0F0000) # red!
magtag.get_local_time()
try:
now = time.localtime()
print("Now: ", now)
# display the current time since its the last-update
updated_at = "%d/%d\n%d:%02d" % now[1:5]
magtag.set_text(updated_at, 6, False)
# get data from the CDC
value = magtag.fetch()
print("Response is", value)
# OK we're done!
magtag.peripherals.neopixels.fill(0x000F00) # greten
except (ValueError, RuntimeError) as e:
print("Some error occured, trying again later -", e)
time.sleep(2) # let screen finish updating
# we only wanna wake up once a day, around the event update time:
event_time = time.struct_time((now[0], now[1], now[2],
DAILY_UPDATE_HOUR, 0, 0,
-1, -1, now[8]))
# how long is that from now?
remaining = time.mktime(event_time) - time.mktime(now)
if remaining < 0: # ah its aready happened today...
remaining += 24 * 60 * 60 # wrap around to the next day
remaining_hrs = remaining // 3660
remaining_min = (remaining % 3600) // 60
print("Gonna zzz for %d hours, %d minutes" % (remaining_hrs, remaining_min))
# Turn it all off and go to bed till the next update time
magtag.exit_and_deep_sleep(remaining)
@paulywill
Copy link

For the QR position, I had to place it up and left to fit the entire thing on my MagTag screen.

qr_size=2, x=225, y=55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment