March 24, 2019

The PyPortal Lands

The PyPortal Lands

So Adafruit's latest is the PyPortal, an Atmel-SAMD51-powered, Python-running board with a touchable LCD display, an ESP32 for WiFi and BT, and a form factor which makes it easy to mount. I got mine with the Adabox subscription and it came with an acrylic enclosure and a tube you could fill with pennies (UK pennies work) to weight down the enclosure which has a curve in the back where it can sit.

It runs CircuitPython, Adafruit's fork of MicroPython built for ease of development and deployment across Adafruits various boards. And with the PyPortal, that includes a PyPortal class, used in lots of their examples, which makes the business of creating an internet connected dashboard a breeze... or at least a reasonable gust. Here's some code I hacked up from another example to turn it into an Petition monitor for the Revoke Article 50 petition...

UPDATE 25/3/19: There's now a Github repository codepope/RevokeArticle50 with the essential assets for this app in it.

"""
This example goes and gets the number of signers to the Petition to revoke article 50 in the UK
"""

import time
import board
from adafruit_pyportal import PyPortal

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

# pylint: disable=line-too-long
DATA_SOURCE = "https://petition.parliament.uk/petitions/241584/count.json"
DATA_LOCATION1 = [ "signature_count" ]

# the current working directory (where this file is)
cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=DATA_LOCATION1,
                    status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/revoke50.bmp",
                    text_font=cwd+"/fonts/AmaticSC-Bold-80.bdf",
                    text_position=(50, 80),
                    text_color=0xFFFFFF,
                    caption_text="#revokearticle50",
                    caption_font=cwd+"/fonts/AmaticSC-Bold-40.bdf",
                    caption_position=(50, 180),
                    caption_color=0xFFFFFF)


while True:
    try:
        sigs = pyportal.fetch()
        sigs = int(sigs)
        print("Signatures:", sigs)
    except RuntimeError as e:
        print("Some error occured, retrying! -", e)

    time.sleep(60)

That PyPortal class is taking URLs for JSON data, status LED, backdrops (GIMP recommended to get that clean BMP feel), fonts (FontForge for making BDFs but oh such hard work and well crashy on the Mac), captions and positions. You then get to orchestrate it retrieving the data and thats pretty much it.