In the code block below, click on the Download Project Bundle button, and save the .zip archive file to your computer.

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

Copy the contents of the Dashblock_API directory to your PyPortal CIRCUITPY drive.

# SPDX-FileCopyrightText: 2019 Isaac Wellish for Adafruit Industries
# SPDX-License-Identifier: MIT

Dashblock API Adafruit Learn Guide Count demo
Use Dashblock to create a custom API for,
then display the number of learn guides on the site

import os
import time
import board
from adafruit_pyportal import PyPortal

# Set up where we'll be fetching data from
DATA_SOURCE = "" + os.getenv("DASHBLOCK_KEY")
GUIDE_COUNT = ['entities', 0, 'guide count']
CAPTION = 'total tutorials:'

# determine the current working directory
# needed so we know where to find files
cwd = ("/"+__file__).rsplit('/', 1)[0]

# Initialize the pyportal object and let us know what data to fetch and where
# to display it
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path = (GUIDE_COUNT),
                    text_position=((40, 100)),
                    text_maxlen=(4), # max text length, only want first 4 chars for number of guides
                    caption_position=(40, 60),

# track the last value so we can play a sound when it updates
last_value = 0

while True:
        value = pyportal.fetch()
        print("Response is", value)
        int_value = int(value[:4]) # save only first 4 chars and cast to int
        if last_value < int_value:  # ooh it went up!
            print("New guide!")
            pyportal.play_file(cwd+"/coin.wav")  # make a noise!
        last_value = int_value
    except RuntimeError as e:
        print("Some error occured, retrying! -", e)
    except ValueError as e:
        print("Value error occured, retrying! -", e)

    time.sleep(600) #update every 10 mins

Downloading the libraries

Make sure to add the necessary libraries to the lib folder, info on how to do this can be found in the "PyPortal CircuitPython Setup" section

This project uses the following CircuitPython libraries:

  • adafruit_bitmap_font (directory)
  • adafruit_bus_device (directory)
  • adafruit_display_shapes (directory)
  • adafruit_display_text (directory)
  • adafruit_esp32spi (directory)
  • adafruit_imageload (directory)
  • adafruit_io (directory)
  • adafruit_pyportal.mpy (file)
  • adafruit_requests (file)
  • adafruit_sdcard.mpy (file)
  • adafruit_touchscreen.mpy (file)
  • neopixel.mpy (file)

Add Dashblock Credentials to settings.toml

The secrets file needs some edits before the project will work.

Your Dashblock account is tied to your API requests, thus you have a limited number of credits or requests for data. Each user has their own API key associated with their account. You can find your key by looking at the URL we extracted earlier from Dashblock.

It looks like this:

Go into your settings.toml file, and add the following line:

Make sure to change 'yourSuperLongDashblockKeyHere' with your key

Save the file and you are good to go!

CIRCUITPY_WIFI_SSID = "your_wifi_ssid"
CIRCUITPY_WIFI_PASSWORD = "your_wifi_password"
DASHBLOCK_KEY = "yourSuperLongDashblockKeyHere"

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


This guide was first published on Sep 09, 2019. It was last updated on Jun 21, 2024.

This page (Code PyPortal with CircuitPython) was last updated on Jun 19, 2024.

Text editor powered by tinymce.