Installing the Project Code

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 MagTag_Covid_Vaccination/ 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.

Your CIRCUITPY drive should now look similar to the following image:

CIRCUITPY

Be sure you have the secrets.py file complete and loaded onto the CIRCUITPY drive also or the code will not run properly.

# SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries
#
# SPDX-License-Identifier: MIT

from adafruit_magtag.magtag import MagTag
from adafruit_progressbar.progressbar import ProgressBar

# Set up where we'll be fetching data from
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv"  # pylint: disable=line-too-long
# Find data for other countries/states here:
# https://github.com/owid/covid-19-data/tree/master/public/data/vaccinations

magtag = MagTag(url=DATA_SOURCE)
magtag.network.connect()

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=(
        (magtag.graphics.display.width // 2) - 1,
        8,
    ),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Title

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=(
        (magtag.graphics.display.width // 2) - 1,
        23,
    ),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Date

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=(
        (magtag.graphics.display.width // 2) - 1,
        40,
    ),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Vaccinated text

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=(
        (magtag.graphics.display.width // 2) - 1,
        85,
    ),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Fully vaccinated text

BAR_WIDTH = magtag.graphics.display.width - 80
BAR_HEIGHT = 25
BAR_X = magtag.graphics.display.width // 2 - BAR_WIDTH // 2

progress_bar = ProgressBar(
    BAR_X, 50, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)

progress_bar_1 = ProgressBar(
    BAR_X, 95, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)

magtag.graphics.splash.append(progress_bar)
magtag.graphics.splash.append(progress_bar_1)
magtag.graphics.set_background("/bmps/background.bmp")


def l_split(line):
    line_list = []
    print(line)
    while "," in line:
        if line[0] == '"':
            temp = line.split('"', 2)[1]
            line_list.append(temp)
            line = line.split('"', 2)[2][1:]
        else:
            temp, line = line.split(",", 1)
            line_list.append(temp)
    line_list.append(line)
    return line_list


try:
    table = magtag.fetch().split("\n")
    columns = l_split(table[0])
    latest = l_split(table[-2])
    print(columns)
    print(latest)
    value = dict(zip(columns, latest))
    print("Response is", value)
    print(value)

    vaccinated = int(value["people_vaccinated"]) / 331984513
    fully_vaccinated = int(value["people_fully_vaccinated"]) / 331984513

    magtag.set_text(f"{value['location']} Vaccination Rates", 0, False)
    magtag.set_text(value["date"], 1, False)
    magtag.set_text("Vaccinated: {:.2f}%".format(vaccinated * 100), 2, False)
    magtag.set_text(
        "Fully Vaccinated: {:.2f}%".format(fully_vaccinated * 100), 3, False
    )

    progress_bar.progress = vaccinated
    progress_bar_1.progress = fully_vaccinated

    magtag.refresh()

    SECONDS_TO_SLEEP = 24 * 60 * 60  # Sleep for one day

except (ValueError, RuntimeError, ConnectionError, OSError) as e:
    print("Some error occured, retrying in one hour! -", e)
    seconds_to_sleep = 60 * 60  # Sleep for one hour

print(f"Sleeping for {SECONDS_TO_SLEEP} seconds")
magtag.exit_and_deep_sleep(SECONDS_TO_SLEEP)

Code Run Through

First, the code imports the two required libraries.

from adafruit_magtag.magtag import MagTag
from adafruit_progressbar import ProgressBar

Next, the code defines where it'll be getting the data from, initializes the MagTag object and tells it to connect to the network defined in secrets.py.

# Set up where we'll be fetching data from
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/United%20States.csv"
# Find data for other countries/states here:
# https://github.com/owid/covid-19-data/tree/master/public/data/vaccinations

magtag = MagTag(url=DATA_SOURCE)
magtag.network.connect()

Then, the four text fields are defined. These are individually set by passing the set_text method a number that corresponds to the order the specific text field was created.

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=((magtag.graphics.display.width // 2) - 1, 8,),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Title

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=((magtag.graphics.display.width // 2) - 1, 23,),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Date

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=((magtag.graphics.display.width // 2) - 1, 40,),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Vaccinated text

magtag.add_text(
    text_font="/fonts/ncenR14.pcf",
    text_position=((magtag.graphics.display.width // 2) - 1, 85,),
    text_anchor_point=(0.5, 0.5),
    is_data=False,
)  # Fully vaccinated text

After that, the progress bars are set up. The top one will track the percent of people who have been partially vaccinated and the bottom one will track the percent of people who have been fully vaccinated.

BAR_WIDTH = magtag.graphics.display.width - 80
BAR_HEIGHT = 25
BAR_X = magtag.graphics.display.width // 2 - BAR_WIDTH // 2

progress_bar = ProgressBar(
    BAR_X, 50, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)

progress_bar_1 = ProgressBar(
    BAR_X, 95, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)

magtag.graphics.splash.append(progress_bar)
magtag.graphics.splash.append(progress_bar_1)
magtag.graphics.set_background("/bmps/background.bmp")

The code now enters the try part of the try/except block. It first gets the csv data from the URL defined above, then splits it by line, gets the latest line, and splits it into a list so it can be used easier. After that, the percent of people who have been partially and fully vaccinated is calculated. The four text fields are then all set to their respective values and the progress bars are updated. Assuming everything has gone well so far, the display is then refreshed.

Finally, the MagTag sleeps for a day, at which point this code will run again.

Please note that if you change the country you are getting the data for, you will also have to change the population (the denominators in vaccinated and fully_vaccinated) to match that.
try:
    value = magtag.fetch().split("\n")[-2].split(",")
    print("Response is", value)

    vaccinated = int(value[-2]) / 331984513
    fully_vaccinated = int(value[-1]) / 331984513

    magtag.set_text(f"{value[0]} Vaccination Rates", 0, False)
    magtag.set_text(value[1], 1, False)
    magtag.set_text("Vaccinated: {:.2f}%".format(vaccinated * 100), 2, False)
    magtag.set_text(
        "Fully Vaccinated: {:.2f}%".format(fully_vaccinated * 100), 3, False
    )

    progress_bar.progress = vaccinated
    progress_bar_1.progress = fully_vaccinated

    magtag.refresh()
    
    seconds_to_sleep = 24 * 60 * 60  # Sleep for one day
    print(f"Sleeping for {seconds_to_sleep} seconds")
    magtag.exit_and_deep_sleep(seconds_to_sleep)

However, if an issue has occurred, the code prints it out and tries again.

except (ValueError, RuntimeError) as e:
    print("Some error occured, retrying! -", e)

This guide was first published on Mar 19, 2021. It was last updated on Mar 19, 2021.

This page (Code the Vaccination Tracker) was last updated on Sep 15, 2023.

Text editor powered by tinymce.