Text Editor
Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.
Alternatively, you can use any text editor that saves simple text files.
Follow this guide for info on getting your AIO credentials for the settings.toml file.
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_Showerthoughts/ 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 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT # MagTag Shower Thoughts # Be sure to put WiFi access point info in settings.toml file to connect from os import getenv import time import random from adafruit_magtag.magtag import MagTag # Get WiFi details, ensure these are setup in settings.toml ssid = getenv("CIRCUITPY_WIFI_SSID") password = getenv("CIRCUITPY_WIFI_PASSWORD") if None in [ssid, password]: raise RuntimeError( "WiFi settings are kept in settings.toml, " "please add them there. The settings file must contain " "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " "at a minimum." ) # Set up where we'll be fetching data from DATA_SOURCE = "https://www.reddit.com/r/showerthoughts/hot.json?limit=10" quote_num = random.randint(0, 9) # we get 10 possibilities, pick one of them QUOTE_LOCATION = ["data", "children", quote_num, "data", "title"] AUTHOR_LOCATION = ["data", "children", quote_num, "data", "author"] # in seconds, we can refresh about 100 times on a battery TIME_BETWEEN_REFRESHES = 1 * 60 * 60 # one hour delay magtag = MagTag( url=DATA_SOURCE, json_path=(QUOTE_LOCATION, AUTHOR_LOCATION), ) magtag.graphics.set_background("/bmps/magtag_shower_bg.bmp") # quote in bold text, with text wrapping magtag.add_text( text_font="/fonts/Arial-Bold-12.pcf", text_wrap=28, text_maxlen=120, text_position=( (magtag.graphics.display.width // 2), (magtag.graphics.display.height // 2) - 10, ), line_spacing=0.75, text_anchor_point=(0.5, 0.5), # center the text on x & y ) # author in italic text, no wrapping magtag.add_text( text_font="/fonts/Arial-Italic-12.bdf", text_position=(magtag.graphics.display.width // 2, 118), text_anchor_point=(0.5, 0.5), # left justify this line ) try: magtag.network.connect() value = magtag.fetch() print("Response is", value) except (ValueError, RuntimeError, ConnectionError, OSError) as e: magtag.set_text(e) print("Some error occured, retrying! -", e) # wait 2 seconds for display to complete time.sleep(2) magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES)
import time import random from adafruit_magtag.magtag import MagTag
The time
library allows us to do some pausing between steps.
We'll use the random
library to randomize posts.
The adafruit_magtag
library makes it very simple to set up the MagTag's display, get online via WiFi, and to request and parse .json files.
Showerthought Variables
We'll set up some variables for the sources of our data. This represent the Reddit URL as well as the .json traversal needed to grab the quote and author name.
DATA_SOURCE = "https://www.reddit.com/r/showerthoughts/hot.json?limit=10" quote_num = random.randint(0, 9) # we get 10 possibilities, pick one of them QUOTE_LOCATION = ["data", "children", quote_num, "data", "title"] AUTHOR_LOCATION = ["data", "children", quote_num, "data", "author"]
Sleepy Time
The MagTag uses a deep sleep mode to conserve battery power between updates. We'll set the quotes to refresh every hour, which means we can get days of use on a single charge of a LiPo battery!
TIME_BETWEEN_REFRESHES = 1 * 60 * 60 # one hour delay
Setup
There are a number of setup steps we'll take next. First, we create the MagTag()
object named magtag
(all lower-case is easier to type anyway!).
Then we'll set the background graphic to the on-disk bitmap file.
magtag = MagTag( url=DATA_SOURCE, json_path=(QUOTE_LOCATION, AUTHOR_LOCATION), ) magtag.graphics.set_background("/bmps/magtag_shower_bg.bmp")
Text
Next up, we'll use the magtag library's text commands to create the two text objects, one for the quote and one for the author.
The magtag.add_text()
command has arguments for the font, wrap width, maximum length of text glyphs, position, line spacing, and anchor point for the text.
magtag.add_text( text_font="/fonts/Arial-Bold-12.bdf", text_wrap=28, text_maxlen=120, text_position=( (magtag.graphics.display.width // 2), (magtag.graphics.display.height // 2) - 10, ), line_spacing=0.75, text_anchor_point=(0.5, 0.5), # center the text on x & y ) # author in italic text, no wrapping magtag.add_text( text_font="/fonts/Arial-Italic-12.bdf", text_position=(magtag.graphics.display.width // 2, 118), text_anchor_point=(0.5, 0.5), # left justify this line )
Connect
Next we'll get online, using the authentication details in the settings.toml file to connect to the network.
Then, we'll use the magtag.fetch()
command which will go to the specified DATA_SOURCE
url, grab the .json file, and parse it for the QUOTE_LOCATION
text and AUTHOR_LOCATION
text. (We include some error checking just in case. That's Internet!)
Once retrieved and parsed, the text is displayed on the MagTag.
Next, the MagTag takes an hour long nap, then wakes up to do it all over again. This then repeats until the end of time, keeping you happy with fresh quotes!
try: magtag.network.connect() value = magtag.fetch() print("Response is", value) except (ValueError, RuntimeError) as e: magtag.set_text(e) print("Some error occured, retrying! -", e) # wait 2 seconds for display to complete time.sleep(2) magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES)
Page last edited June 13, 2025
Text editor powered by tinymce.