Installing the Project Code

Download a zip of the project by clicking 'Download: Project Zip' in the preview of code.py below.

After unzipping the file, copy its contents to the CIRCUITPY drive which appears when the MagTag is connected to your computer via a USB cable and turned on via a small on/off switch onboard.

In addition to the libraries you installed in the 'MagTag-Specific CircuitPython Libraries' section, you'll also have to install adafruit_progressbar.

After you've copied everything over, this is what the CIRCUITPY drive should look like.

Be sure you have the libraries and secrets.py file complete and loaded onto the CIRCUITPY drive also or the code will not run properly.
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) 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 2021-03-19 15:57:17 -0400.

This page (Code the Vaccination Tracker) was last updated on Oct 22, 2021.

Text editor powered by tinymce.