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 have to install adafruit_progressbar.
After you've copied everything over, this is what the CIRCUITPY drive should look like. |
from adafruit_magtag.magtag import MagTag from adafruit_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" # 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") 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) except (ValueError, RuntimeError) as e: print("Some error occured, retrying! -", e)
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.
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)