This project will pull the current local time from adafruit.io and then use it to calculate the percentage complete for the year.
A separate file on your MagTag, secrets.py, is used to store the information you need to access WiFi and Adafruit IO. You will need a free account Adafruit IO account, your WiFi access information, and your location (for localized timezone management).
Steps to set up your secrets.py file:
1. See the top part of this page on the MagTag for how to add the WiFi and timezone parts of the secrets.py file.
2. Set up an Adafruit IO account, if you do not have one. It's free and not complex.
3. Update the secrets.py file fields aio_username
and aio_key
with the values from your Adafruit IO account. Your secrets.py file should look similar to this:
# This file is where you keep secret settings, passwords, and tokens! # If you put them in the code you risk committing that info or sharing it secrets = { 'ssid' : 'your-ssid-here', 'password' : 'your-password-here', 'aio_username' : "your-aio-username-here", 'aio_key' : 'your-aio-key-here', 'location' : 'New York, US' }
Deep Sleep
This project uses the new deep sleep API to save power so it can run longer on a battery before needing a recharge. See this guide: https://learn.adafruit.com/deep-sleep-with-circuitpython to learn more!
Installing 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_Progress_Displays/year_progress_percent/ 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:

# SPDX-FileCopyrightText: 2020 Tim C, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense """ Show the progress through the year with text and a progress bar. Copy epilogue18.bdf into fonts/ on your CIRCUITPY drive. """ from adafruit_magtag.magtag import MagTag from adafruit_progressbar.progressbar import ProgressBar import rtc def days_in_year(date_obj): # check for leap year if (date_obj.tm_year % 100 != 0 or date_obj.tm_year % 400 == 0) and date_obj.tm_year % 4 == 0: return 366 return 365 magtag = MagTag() magtag.network.connect() magtag.add_text( text_font="/fonts/epilogue18.bdf", text_position=( (magtag.graphics.display.width // 2) - 1, 24, ), text_anchor_point=(0.5, 0.5), is_data=False, ) magtag.set_text("Year Progress:", auto_refresh=False) magtag.add_text( text_font="/fonts/epilogue18.bdf", text_position=( (magtag.graphics.display.width // 2) - 1, 55, ), text_anchor_point=(0.5, 0.5), is_data=False, ) # set progress bar width and height relative to board's display BAR_WIDTH = magtag.graphics.display.width - 80 BAR_HEIGHT = 30 BAR_X = magtag.graphics.display.width // 2 - BAR_WIDTH // 2 BAR_Y = 80 # Create a new progress_bar object at (x, y) progress_bar = ProgressBar( BAR_X, BAR_Y, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000 ) magtag.graphics.splash.append(progress_bar) try: magtag.network.get_local_time() now = rtc.RTC().datetime progress_bar.progress = now.tm_yday / days_in_year(now) magtag.set_text( "{:.2f}%".format(now.tm_yday / days_in_year(now) * 100.0), index=1 ) print(now) magtag.exit_and_deep_sleep(24 * 60 * 60) # one day except (ValueError, RuntimeError, ConnectionError, OSError) as e: print("Some error occurred, retrying after 1 minute! -", e) magtag.exit_and_deep_sleep(60) # one minute
Code Overview
This code uses the adafruit_magtag library to fetch the current time, and to show the text output on the display. The library creates its own displayio.Group
internally that is shown on the display. Add the progress bar to that Group like this magtag.graphics.splash.append(progress_bar)
.
You can use magtag.network.get_local_time()
to fetch the current time from Adafruit IO. Then you can access it with rtc.RTC().datetime
. Use its field tm_yday
, along with the total days in the year (accounting for leap days every 4 years) to divide out the year's progress. Set the progress on the progress bar before calling magtag.set_text()
which is nice because it will handle the screen refresh.
To put the MagTag to sleep we use the function magtag.exit_and_deep_sleep()
passing in the amount of seconds that it should sleep. This script sleeps for one day if everything works successfully, and one minute if anything failed so that it will try again shortly.
Page last edited January 21, 2025
Text editor powered by tinymce.