Libraries

We'll need to make sure we have these libraries installed. (Check out this link on installing libraries if needed.)

  • adafruit_bitmap_font
  • adafruit_bus_device
  • adafruit_display_text
  • adafruit_esp32spi
  • adafruit_io
  • adafruit_matrixportal
  • adafruit_requests.mpy
  • neopixel.mpy

Connect to the Internet

Once you have CircuitPython setup and libraries installed we can get your board connected to the Internet. The process for connecting can be found here.

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.

Code

Click the Download: Zip File link below in the code window to get a zip file with all the files needed for the project. Copy purple_air_display.py from the zip file and place on the CIRCUITPY drive, then rename it to code.py.

# Purple Air AQI Display
# for Metro M4 Airlift with RGB Matrix Shield
# or Matrix Portal
# and 64 x 32 RGB LED Matrix

import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal

def aqi_transform(val):
    aqi = pm_to_aqi(val)  # derive Air Quality Index from Particulate Matter 2.5 value
    return "AQI: %d" % aqi

def message_transform(val):  # picks message based on thresholds
    index = aqi_to_list_index(pm_to_aqi(val))
    messages = (
        "Hazardous",
        "Very Unhealthy",
        "Unhealthy",
        "Unhealthy for Sensitive Groups",
        "Moderate",
        "Good",
    )

    if index is not None:
        return messages[index]
    return "Unknown"

SENSOR_ID = 3085 # Poughkeepsie  # 30183  LA outdoor  / 37823  oregon  / 21441   NYC
SENSOR_REFRESH_PERIOD = 30  # seconds
DATA_SOURCE = "https://www.purpleair.com/json?show=" + str(SENSOR_ID)
SCROLL_DELAY = 0.02
DATA_LOCATION = ["results", 0, "PM2_5Value"]  # navigate the JSON response

# --- Display setup ---
matrixportal = MatrixPortal(
    status_neopixel=board.NEOPIXEL,
    debug=True,
    url=DATA_SOURCE,
    json_path=(DATA_LOCATION, DATA_LOCATION),
)

# Create a static label to show AQI
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(8, 7),
    text_transform=aqi_transform,
)

# Create a scrolling label to show level message
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(0, 23),
    scrolling=True,
    text_transform=message_transform,
)
# pylint: disable=too-many-return-statements
def aqi_to_list_index(aqi):
    aqi_groups = (301, 201, 151, 101, 51, 0)
    for index, group in enumerate(aqi_groups):
        if aqi >= group:
            return index
    return None

def calculate_aqi(Cp, Ih, Il, BPh, BPl):  # wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
    return round(((Ih - Il)/(BPh - BPl)) * (Cp - BPl) + Il)

def pm_to_aqi(pm):
    pm = float(pm)
    if pm < 0:
        return pm
    if pm > 1000:
        return 1000
    if pm > 350.5:
        return calculate_aqi(pm, 500, 401, 500, 350.5)
    elif pm > 250.5:
        return calculate_aqi(pm, 400, 301, 350.4, 250.5)
    elif pm > 150.5:
        return calculate_aqi(pm, 300, 201, 250.4, 150.5)
    elif pm > 55.5:
        return calculate_aqi(pm, 200, 151, 150.4, 55.5)
    elif pm > 35.5:
        return calculate_aqi(pm, 150, 101, 55.4, 35.5)
    elif pm > 12.1:
        return calculate_aqi(pm, 100, 51, 35.4, 12.1)
    elif pm >= 0:
        return calculate_aqi(pm, 50, 0, 12, 0)
    else:
        return None

def get_color(aqi):
    index = aqi_to_list_index(aqi)
    colors = (
        (115, 20, 37),
        (140, 26, 75),
        (234, 51, 36),
        (239, 133, 51),
        (255, 255, 85),
        (104, 225, 67),
    )
    if index is not None:
        return colors[index]
    return (150, 150, 150)

sensor_refresh = None
while True:
    # only query the weather every 10 minutes (and on first run)
    if (not sensor_refresh) or (time.monotonic() - sensor_refresh) > SENSOR_REFRESH_PERIOD:
        try:
            value = matrixportal.fetch()
            print("Response is", value)
            matrixportal.set_text_color(get_color(pm_to_aqi(value[0])))
            sensor_refresh = time.monotonic()
        except RuntimeError as e:
            print("Some error occured, retrying! -", e)
            continue

    # Scroll it
    matrixportal.scroll_text(SCROLL_DELAY)

Purple Air

Purple Air is an air quality monitoring network based on IoT sensors. You can learn more about the sensors here. When people buy and install their own sensors and enable them to appear on the Purple Air map, we all benefit!

Head to the Purple Air map here and then try to find a sensor near your location. Even if you don't have your own sensor installed outside, you may be able to find a sensor nearby that you can choose to monitor on your Matrix display.

Here, I've zoomed in on a sensor in downtown Los Angeles. If I look at the URL for this sensor, I can see the sensor ID number:

https://www.purpleair.com/map?opt=1/mAQI/a10/cC0&select=30183

The number after select= is 30183 so I'll copy than number to use in my code.py file so that I can display that air quality sensor value.

In your code.py file, go to the line that has the variable SENSOR_ID and paste in the number of the sensor you'd like to display, then re-save the file to your CIRCUITPY drive.

For example:

SENSOR_ID = 30183

How it Works

Libraries

Most of the heavy lifting is done by the adafruit_matrixportal library we import. This will take care of getting online through the access point (the info is in your secrets.py file), as well as fetching and parsing the PM2.5 particle data from the Purple Air JSON file.

Functions

The aqi_transform() function converts PM (particulate matter) values to AQI (air quality index) values and returns the text string we'll display. It does this by using both the pm_to_aqi() and the calculate_aqi() fuctions.

The message_transform() function selects the text description associated with a give AQI value.

Variables

These are the variables used to pick a Purple Air sensor as well as a refresh rate, data source, scroll speed, and the data location withing the JSON file:

Download: file
SENSOR_ID = 3085 # Poughkeepsie
SENSOR_REFRESH_PERIOD = 30  # seconds
DATA_SOURCE = "https://www.purpleair.com/json?show=" + str(SENSOR_ID)
SCROLL_DELAY = 0.02
DATA_LOCATION = ["results", 0, "PM2_5Value"]  # navigate the JSON response

Matrix Setup

Next, the Matrix is set up for displaying text data using the terminalio font.

Main Loop

In the main loop of the code the latest value from the selected Purple Air sensor is fetched using value = matrixportal.fetch() and then the result is formatted and updated to the display.

The text description is then scrolled across the display and the process is repeated every thirty seconds.

This guide was first published on Sep 30, 2020. It was last updated on Sep 30, 2020.

This page (Code the Purple Air Display) was last updated on Nov 06, 2020.