Code PyPortal with CircuitPython

Secrets File Setup

If you have not yet set up a secrets.py file in your CIRCUITPY drive and connected to the internet using it, follow this guide and come back when you've successfully connected to the internet

Adafruit IO username, and Adafruit IO key. Head to io.adafruit.com and simply click the View AIO Key link on the left hand side of the Adafruit IO page to get this information.

Then, add them to the secrets.py file:

Download: file
secrets = {
    'ssid' : '_your_wifi_ssid',
    'password : '_your_wifi_password',
    'aio_username' : '_your_adafruit_io_username',
    'aio_key' : '_your_big_huge_super_long_aio_key_'
    }

Add CircuitPython Code and Project Assets

In the embedded code element below, click on the Download: Project Zip link, and save the .zip archive file to your computer.

Then, uncompress the .zip file, it will unpack to a folder named PyPortal_Email_Display.

Copy the contents of the PyPortal_Email_Display directory to your PyPortal's CIRCUITPY drive. Make sure to save the font (Helvetica-Oblique-17.bdf) into the fonts folder on the CIRCUITPY drive.

Rename the email_display.py file to code.py so it will automatically run when the PyPortal restarts.

This is what the final contents of the CIRCUITPY drive will look like:

"""
PyPortal Adafruit IO Feed Display

Displays an Adafruit IO Feed on a PyPortal.
"""
import time
import board
from adafruit_pyportal import PyPortal

# Get Adafruit IO details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("Adafruit IO secrets are kept in secrets.py, please add them there!")
    raise

# Adafruit IO Account
IO_USER = secrets['aio_username']
IO_KEY = secrets['aio_key']
# Adafruit IO Feed
IO_FEED = 'zapemail'

DATA_SOURCE = "https://io.adafruit.com/api/v2/{0}/feeds/{1}?X-AIO-Key={2}".format(IO_USER,
                                                                                  IO_FEED, IO_KEY)
FEED_VALUE_LOCATION = ['last_value']

cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=FEED_VALUE_LOCATION,
                    status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/pyportal_email.bmp",
                    text_font=cwd+"/fonts/Helvetica-Oblique-17.bdf",
                    text_position=(30, 65),
                    text_color=0xFFFFFF,
                    text_wrap=35, # wrap feed after 35 chars
                    text_maxlen=160)

# speed up projects with lots of text by preloading the font!
pyportal.preload_font()

while True:
    try:
        print('Fetching Adafruit IO Feed Value..')
        value = pyportal.fetch()
        print("Response is", value)
    except RuntimeError as e:
        print("Some error occured, retrying! -", e)
    time.sleep(10)
If you run into any errors, such as "ImportError: no module named `adafruit_display_text.label`" be sure to update your libraries to the latest release bundle!

Code Usage

After the PyPortal loads up (it will display a startup image and sound), it will display an image called email_background.bmp as the screen's background. This is a 320 x 240 pixel RGB 16-bit raster graphic in .bmp format.

Then, it requests the value of the Adafruit IO feed (IO_FEED in the code) and displays the value with bitmapped fonts on top of the background.

When the custom inbox is emailed, Zapier will immediately send the data to an Adafruit IO feed.

Customization

Now that you have your PyPortal displaying incoming emails, you add some customization to the PyPortal to give your personal flair!

Change the background

You can customize the background to add (or remove) information, by making a 320x240 16-bit RGB color .bmp file.

Display more text

Want to show more text on the display? While our code limits the maximum length of the feed value being displayed to 160 characters, you can change it in the code by increasing the length of the variable text_maxlen to any amount of characters which can fit reasonably on the display. 

If you want to display more text, switch to a smaller font size.

Change the text color

You can also change the color of the display by changing the line text_color=0xFFFFFF in the code to your color of choice.

Visit https://www.color-hex.com to pick your color, and then adjust the text_color value.

For example, if you'd like to change the text from white to black, you'd adjust the text_color from text_color=0xFFFFFF to text_color=#000000

This guide was first published on Mar 15, 2019. It was last updated on Mar 15, 2019. This page (Code PyPortal with CircuitPython) was last updated on Jan 27, 2020.