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'
}

Custom Font

This program uses as custom bdf font file. Download the full project zip in the code listing below and copy epilogue18.bdf into the /fonts directory in your CIRCUITPY drive. Create the /fonts directory if it doesn't exist yet. The code.py file should be in the main (root) directory of the CIRCUITPY drive.

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!

# 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) 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.

This guide was first published on Dec 02, 2020. It was last updated on Dec 02, 2020.

This page (Year Progress) was last updated on Oct 22, 2021.

Text editor powered by tinymce.