PyPortal Astronauts in Space
2026-06-02 | By Adafruit Industries
License: See Original Project Addressable LEDs Ambient Board Specific Displays Environmental LCD / TFT Light Temperature WS2812/SK6812 (NeoPixel) AdaBox Arduino
Courtesy of Adafruit
Guide by John Park and Anne Barela
Overview
Display the current number and names of the astronauts in space on your PyPortal display!
Using CircuitPython, the PyPortal wirelessly grabs the data needed from the internet API and then turns the JSON data into a total "in-space" count and the names of the astronauts.
Parts
Install CircuitPython
CircuitPython is a derivative of MicroPython designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the CIRCUITPY "flash" drive to iterate.
The following instructions will show you how to install CircuitPython. If you've already installed CircuitPython but are looking to update it or reinstall it, the same steps work for that as well!
Set up CircuitPython Quick Start!
Follow this quick step-by-step for super-fast Python power :)
Download the latest version of CircuitPython for the PyPortal via CircuitPython.org
Download the latest version of CircuitPython for the PyPortal Pynt via CircuitPython.org
Click the link above to download the latest version of CircuitPython for the PyPortal.
Download and save it to your desktop (or wherever is handy).
Plug your PyPortal into your computer using a known-good USB cable.
A lot of people end up using charge-only USB cables and it is very frustrating! So, make sure you have a USB cable you know is good for data sync.
Double-click the Reset button on the top in the middle (magenta arrow) on your board, and you will see the NeoPixel RGB LED (green arrow) turn green. If it turns red, check the USB cable, try another USB port, etc. Note: The little red LED next to the USB connector will pulse red. That's ok!
If double-clicking doesn't work the first time, try again. Sometimes it can take a few tries to get the rhythm right!
You will see a new disk drive appear called PORTALBOOT.
Drag the adafruit-circuitpython-pyportal-<whatever>.uf2 file to PORTALBOOT.
The LED will flash. Then, the PORTALBOOT drive will disappear, and a new disk drive called CIRCUITPY will appear.
If you haven't added any code to your board, the only file that will be present is boot_out.txt. This is absolutely normal! It's time for you to add your code.py and get started!
That's it, you're done! :)
PyPortal Default Files
Click below to download a zip of the files that shipped on the PyPortal or PyPortal Pynt.
PyPortal CircuitPython Setup
To use all the amazing features of your PyPortal with CircuitPython, you must first install a number of libraries. This page covers that process.
Adafruit CircuitPython Bundle
Download the Adafruit CircuitPython Library Bundle. You can find the latest release here:
Latest Adafruit CircuitPython Library Bundle
Download the adafruit-circuitpython-bundle-*.x-mpy-*.zip bundle zip file where *.x MATCHES THE VERSION OF CIRCUITPYTHON YOU INSTALLED, and unzip a folder of the same name. Inside you'll find a lib folder. You have two options:
You can add the lib folder to your CIRCUITPY drive. This will ensure you have all the drivers. But it will take a bunch of space on the 8 MB disk
Add each library as you need it, this will reduce the space usage, but you'll need to put in a little more effort.
At a minimum we recommend the following libraries, in fact we more than recommend. They're basically required. So, grab them and install them into CIRCUITPY/lib now!
adafruit_esp32spi - This is the library that gives you internet access via the ESP32 using (you guessed it!) SPI transport. You need this for anything Internet.
adafruit_requests - This library allows us to perform HTTP requests and get responses back from servers. GET/POST/PUT/PATCH - They're all in here!
adafruit_connection_manager - used by adafruit_requests
adafruit_pyportal - This is our friendly wrapper library that does a lot of our projects, displays graphics and text, fetches data from the internet. Nearly all of our projects depend on it!
adafruit_portalbase - This library is the base library that adafruit_pyportal library is built on top of.
adafruit_touchscreen - A library for reading touches from the resistive touchscreen. Handles all the analog noodling, rotation and calibration for you.
adafruit_io - This library helps connect the PyPortal to our free datalogging and viewing service.
adafruit_imageload - An image display helper, required for any graphics!
adafruit_display_text - not surprisingly, it displays text on the screen
adafruit_bitmap_font - We have fancy font support, and it’s easy to make new fonts. This library reads and parses font files.
adafruit_slideshow - for making image slideshows - handy for quick display of graphics and sound
neopixel - for controlling the onboard NeoPixel
adafruit_adt7410 - library to read the temperature from the on-board Analog Devices ADT7410 precision temperature sensor (not necessary for Titano or Pynt)
adafruit_bus_device - low level support for I2C/SPI
adafruit_fakerequests - This library allows you to create fake HTTP requests by using local files.
Internet Connect!
Connect to WiFi
OK, now that you have your settings.toml file set up - you can connect to the Internet.
To do this, 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 examples/ 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:
Update to CircuitPython 9.2.x or later to use this example.
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
from os import getenv
import adafruit_connection_manager
import adafruit_requests
import board
import busio
from digitalio import DigitalInOut
# Use this import for adafruit_esp32spi version 11.0.0 and up.
# Note that frozen libraries may not be up to date.
# import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi
# Get wifi details and more from a settings.toml file
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
print("ESP32 SPI webclient test")
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_URL = "http://wifitest.adafruit.com/testwifi/sample.json"
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an AirLift Shield:
# esp32_cs = DigitalInOut(board.D10)
# esp32_ready = DigitalInOut(board.D7)
# esp32_reset = DigitalInOut(board.D5)
# If you have an AirLift Featherwing or ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)
# If you have an externally connected ESP32:
# NOTE: You may need to change the pins to reflect your wiring
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
if "SCK1" in dir(board):
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
else:
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
pool = adafruit_connection_manager.get_radio_socketpool(esp)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
requests = adafruit_requests.Session(pool, ssl_context)
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", ":".join(f"{byte:02X}" for byte in esp.MAC_address))
for ap in esp.scan_networks():
print(f"\t{ap.ssid:<23} RSSI: {ap.rssi}")
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(ssid, password)
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
print("My IP address is", esp.ipv4_address)
print(f"IP lookup adafruit.com: {esp.pretty_ip(esp.get_host_by_name('adafruit.com'))}")
print(f"Ping google.com: {esp.ping('google.com')} ms")
# esp._debug = True
print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print("-" * 40)
print(r.text)
print("-" * 40)
r.close()
print()
print("Fetching json from", JSON_URL)
r = requests.get(JSON_URL)
print("-" * 40)
print(r.json())
print("-" * 40)
r.close()
print("Done!")
And save it to your board, with the name code.py.
Don't forget you'll also need to create the settings.toml file as seen above, with your WiFi ssid and password.
In a serial console, you should see something like the following. For more information about connecting with a serial console, view the guide Connecting to the Serial Console.
>>> import wifitest
ESP32 SPI webclient test
ESP32 found and in idle mode
Firmware vers. 1.7.5
MAC addr: 24:C9:DC:BD:0F:3F
HomeNetwork RSSI: -46
HomeNetwork RSSI: -76
Fios-12345 RSSI: -92
FiOS-AB123 RSSI: -92
NETGEAR53 RSSI: -93
Connecting to AP...
Connected to HomeNetwork RSSI: -45
My IP address is 192.168.1.245
IP lookup adafruit.com: 104.20.39.240
Ping google.com: 30 ms
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------
Fetching json from http://wifitest.adafruit.com/testwifi/sample.json
----------------------------------------
{'fun': True, 'company': 'Adafruit', 'founded': 2005, 'primes': [2, 3, 5], 'pi': 3.14, 'mixed': [False, None, 3, True, 2.7, 'cheese']}
----------------------------------------
Done!Going over the example above, here's a breakdown of what the program is doing:
Initialize the ESP32 over SPI using the SPI port and 3 control pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
#...
else:
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)Get the socket pool and the SSL context and then tell the adafruit_requests library about them.
pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context)
Verify an ESP32 is found, checks the firmware and MAC address
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", ":".join("%02X" % byte for byte in esp.MAC_address))Perform a scan of all access points it can see and print out the name and signal strength.
for ap in esp.scan_networks():
print("\t%-23s RSSI: %d" % (ap.ssid, ap.rssi))Connect to the AP we've defined here, then print out the local IP address. Then attempt to do a domain name lookup and ping google.com to check network connectivity. (Note sometimes the ping fails or takes a while; this isn't a big deal.)
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(ssid, password)
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
print("My IP address is", esp.ipv4_address)
print(
"IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
)Now we're getting to the really interesting part of the example program. We've written a library for web fetching web data, named adafruit_requests. It is a lot like the regular Python library named requests. This library allows you to send HTTP and HTTPS requests easily and provides helpful methods for parsing the response from the server.
Here is the part of the example program is fetching text data from a URL.
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" # Further up in the program
# ...
print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print('-' * 40)
print(r.text)
print('-' * 40)
r.close()Finally, here the program is fetching some JSON data. The adafruit_requests library will parse the JSON into a Python dictionary whose structure is the same as the structure of the JSON.
JSON_URL = "http://wifitest.adafruit.com/testwifi/sample.json" # Further up in the program
# ...
print("Fetching json from", JSON_URL)
r = requests.get(JSON_URL)
print('-' * 40)
print(r.json())
print('-' * 40)
r.close()Advanced Requests Usage
Want to send custom HTTP headers, parse the response as raw bytes, or handle a response's http status code in your CircuitPython code?
We've written an example to show advanced usage of the requests module below.
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 examples/ 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.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import os
import adafruit_connection_manager
import board
import busio
from adafruit_esp32spi import adafruit_esp32spi
from digitalio import DigitalInOut
import adafruit_requests
# Get WiFi details, ensure these are setup in settings.toml
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
# If you have an AirLift Featherwing or ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
radio = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print("Connecting to AP...")
while not radio.is_connected:
try:
radio.connect_AP(ssid, password)
except RuntimeError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(radio.ap_info.ssid, "utf-8"), "\tRSSI:", radio.ap_info.rssi)
# Initialize a requests session
pool = adafruit_connection_manager.get_radio_socketpool(radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
requests = adafruit_requests.Session(pool, ssl_context)
JSON_GET_URL = "https://httpbin.org/get"
# Define a custom header as a dict.
headers = {"user-agent": "blinka/1.0.0"}
print(f"Fetching JSON data from {JSON_GET_URL}...")
with requests.get(JSON_GET_URL, headers=headers) as response:
print("-" * 60)
json_data = response.json()
headers = json_data["headers"]
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
print("-" * 60)
# Read Response's HTTP status code
print("Response HTTP Status Code: ", response.status_code)
print("-" * 60)
Your CIRCUITPY drive should now look similar to the following image:
WiFi Manager
The way the examples above connect to WiFi works but it's a little finicky. Since WiFi is not necessarily so reliable, you may have disconnects and need to reconnect. For more advanced uses, we recommend using the WiFiManager class. It will wrap the connection/status/requests loop for you - reconnecting if WiFi drops, resetting the ESP32 if it gets into a bad state, etc.
Here's a more advanced example that shows using the WiFiManager and also how to fetch the current time from a web source.
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
from os import getenv
import board
import busio
import neopixel
import rtc
from digitalio import DigitalInOut
# Use these imports for adafruit_esp32spi version 11.0.0 and up.
# Note that frozen libraries may not be up to date.
# import adafruit_esp32spi
# from adafruit_esp32spi.wifimanager import WiFiManager
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager
# Get wifi details and more from a settings.toml file
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
print("ESP32 local time")
TIME_API = "https://time.now/developer/api/ip"
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
if "SCK1" in dir(board):
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
else:
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
"""Uncomment below for ItsyBitsy M4"""
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# GREEN_LED = PWMOut.PWMOut(esp, 27)
# BLUE_LED = PWMOut.PWMOut(esp, 25)
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = WiFiManager(esp, ssid, password, status_pixel=status_pixel)
the_rtc = rtc.RTC()
response = None
while True:
try:
print("Fetching json from", TIME_API)
response = wifi.get(TIME_API)
break
except OSError as e:
print("Failed to get data, retrying\n", e)
continue
json = response.json()
current_time = json["datetime"]
the_date, the_time = current_time.split("T")
year, month, mday = (int(x) for x in the_date.split("-"))
the_time = the_time.split(".")[0]
hours, minutes, seconds = (int(x) for x in the_time.split(":"))
# We can also fill in these extra nice things
year_day = json["day_of_year"]
week_day = json["day_of_week"]
is_dst = json["dst"]
now = time.struct_time((year, month, mday, hours, minutes, seconds, week_day, year_day, is_dst))
print(now)
the_rtc.datetime = now
while True:
print(time.localtime())
time.sleep(1)
Further Information
For more information on the basics of doing networking in CircuitPython, see this guide:
By Anne Barela
Code the Astronaut Display
Astronaut Count and Names
This project uses CircuitPython code on tan Adafruit PyPortal to show the current number of astronauts in space according to the thespacedevs.com API. A previous API used in this guide (open-notify.org) is no longer updated.
The PyPortal will do the following:
Display a custom background .bmp image
Check for the current number, and names of people in space
Display the current number of astronauts and list their names and affiliation.
The code is set to update the display every hour at the moment. So it will not be accurate just after a launch or splashdown. This is to lessen the amount of data that is pulled from this free access server.
Install CircuitPython Code and 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_Astronauts.
Copy the contents of the PyPortal_Astronauts directory to your PyPortal's CIRCUITPY drive.
The background image was altered from the prior version to make the names stand out and to add the Artemis II mission capsule in the upper right.
This is what the final contents of the CIRCUITPY drive will look like:
Note the font file is different from the ones used in the previous version of the project and the background bitmap is also different (and new code, of course).
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
# SPDX-FileCopyrightText: 2026 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
This example will access the Space Devs astronaut API, display the number of
humans currently in space and their names and agency on a PyPortal screen.
"""
import gc
import json
import time
import board
import supervisor
from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.label import Label
from adafruit_portalbase.network import HttpError
# Set up where we'll be fetching data from
DATA_SOURCE = (
"https://ll.thespacedevs.com/2.2.0/astronaut/"
"?mode=list&in_space=true&limit=16&type=human"
)
# Determine the current working directory
cwd = ("/"+__file__).rsplit('/', 1)[0]
# Initialize the pyportal object - no json_path since we parse manually
pyportal = PyPortal(url=DATA_SOURCE,
status_neopixel=board.NEOPIXEL,
default_bg=cwd+"/astronauts_background.bmp")
gc.collect()
# Font for names and count label
names_font = bitmap_font.load_font(cwd+"/fonts/Helvetica-Bold-10.bdf")
names_font.load_glyphs(
b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ()'
)
# Display positions and colors
names_position = (6, 140)
names_color = 0xFF00FF
count_color = 0xFFFFFF
count_position = (180, 108)
# Layout constants
screen_width = 320
col_gap = 6
char_width = 7 # slightly wider estimate for Helvetica Bold proportional font
font_height = 12
max_rows = 8
# Agency name to abbreviation lookup
AGENCY_ABBREV = {
"National Aeronautics and Space Administration": "USA",
"Russian Federal Space Agency (ROSCOSMOS)": "RUS",
"European Space Agency": "ESA",
"China National Space Administration": "CHN",
"Japan Aerospace Exploration Agency": "JAX",
"Canadian Space Agency": "CSA",
"SpaceX": "SpX",
"Boeing": "Boe",
}
gc.collect()
def make_count_label(num_people):
lbl = Label(names_font, text="{} People in Space".format(num_people))
lbl.color = count_color
lbl.x = count_position[0]
lbl.y = count_position[1]
return lbl
def fetch_astronauts():
"""Fetch and parse astronaut data, filtering out non-humans."""
gc.collect()
raw = pyportal.fetch()
if isinstance(raw, str):
data = json.loads(raw)
else:
data = raw
humans = [
a for a in data["results"]
if a.get("type", {}).get("name") != "Non-Human"
]
num_people = len(humans)
del data
del raw
gc.collect()
return num_people, humans
def build_name_list(astronaut_list):
"""Build display strings from astronaut records."""
result = []
for a in astronaut_list:
agency_full = a.get("agency", "")
abbrev = AGENCY_ABBREV.get(agency_full, agency_full[:4])
result.append("%s (%s)" % (a["name"], abbrev))
return result
def calculate_columns(names):
"""Calculate column widths and positions based on name lengths.
Col1 names are truncated to fit; col2 start is fixed from col1 width.
"""
col1_x = names_position[0]
col2_names = names[max_rows:]
col2_max_px = (
max(len(n) * char_width for n in col2_names) if col2_names else 0
)
total_available = screen_width - col1_x - col_gap
if col2_names:
# Allocate col2 what it needs, give the rest to col1
col2_width = min(col2_max_px, total_available // 2)
col2_width = max(col2_width, 0)
col1_width = total_available - col2_width - col_gap
col2_x = col1_x + col1_width + col_gap
else:
col1_width = total_available
col2_x = screen_width # unused
# col1 max_chars is the hard truncation limit
max_chars_col1 = col1_width // char_width
max_chars_col2 = col2_width // char_width if col2_names else 0
return col2_x, max_chars_col1, max_chars_col2
def display_astronauts(num_people, names):
"""Create and append all labels, return list for later cleanup."""
label_list = []
# Count label
label_list.append(make_count_label(num_people))
pyportal.root_group.append(label_list[-1])
col2_x, max_chars_col1, max_chars_col2 = calculate_columns(names)
y_start = names_position[1]
for i, name in enumerate(names):
in_col2 = i >= max_rows
max_chars = max_chars_col2 if in_col2 else max_chars_col1
if len(name) > max_chars:
name = name[:max_chars - 1] + "~"
new_lbl = Label(names_font, text=name)
new_lbl.color = names_color
new_lbl.x = col2_x if in_col2 else names_position[0]
new_lbl.y = (
y_start + (i - max_rows) * font_height
if in_col2
else y_start + i * font_height
)
pyportal.root_group.append(new_lbl)
label_list.append(new_lbl)
return label_list
def clear_labels(label_list):
"""Remove all labels from the display group."""
for _lbl in label_list:
pyportal.root_group.pop()
# Main loop
while True:
try:
people_count, fetched_list = fetch_astronauts()
name_list = build_name_list(fetched_list)
del fetched_list
gc.collect()
print("People in space:", people_count)
for n in name_list:
print(" ", n)
except HttpError as e:
print("Rate limited, backing off! -", e)
time.sleep(300) # wait 5 minutes before retrying
supervisor.reload()
except (RuntimeError, KeyError, ValueError) as e:
print("Fetch error, retrying! -", e)
time.sleep(30)
supervisor.reload()
active_labels = display_astronauts(people_count, name_list)
time.sleep(3600) # display for 1 hour - data changes infrequently
clear_labels(active_labels)
gc.collect()
supervisor.reload()
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!
How it Works
The PyPortal Astronauts Display does the following things to keep you in the know about non-Earthbound humans:
Background
First, it displays a bitmap graphic named astronauts_background.bmp as the screen's background. This is a 320 x 240 pixel RGB 16-bit raster graphic in .bmp format.
Font
To display information on the screen, the PyPortal code will use a bitmapped font overlayed on top of the background. The font used here is are bitmap fonts made from the Helvetica typeface. You can learn more about converting type in this guide.
In order to speed up the display of text, the pyportal.preload_font() command is used to place the needed glyphs into memory.
JSON
How does the PyPortal know how many people are in space, as well as their names and locations? Is it talking to them when we're not looking?
That would be awesome, but actually it's not the case. Instead, the PyPortal is fetching a JSON file from the thespacedevs.com API.
Here is a partial return showing just one astronaut - there is so much information! The count of astronauts is at the top (subtract 1 for the non-human "Starman" that SpaceX put up). Name and agency are used for each real person ("human").
Unlike the original project, the JSON data is too complex and large to parse with the PyPortal module, so it's done separately.
Text Positioning
The number of astronauts in space has grown significantly and now requires two columns to display them all. A fair amount of code is used to put the names on the display in two columns. If the name and agency is too large, a ~ character is shown to indicate that.
Background Image
If you would like to create your own background, awesome! You'll want to save the file with these specifications:
320 x 240 pixels
16-bit RGB color (8-bits per channel)
Save file as .bmp format
You can then copy the .bmp file to the root level of the CIRCUITPY drive. Make sure you refer to this new filename in the pyportal constructor line:
default_bg=cwd+"/astronaut_background.bmp"
Change that line to use the new filename name, such as:
default_bg=cwd+"/my_new_background.bmp"
Previous Astronaut Display
Note that the data source for the original guide code open-notify.org is no longer up to date. And there are some other minor issues like the number of astronaut names that can fit on the display. This code is left here for study as it goes into more detail. But be advised it'll not pull accurate information due to open-notify.org not being updated.
Astronaut Count and Names
Using CircuitPython code on the PyPortal, we'll have the display show the current number of astronauts in space according to the open-notify.org API.
The PyPortal will do the following:
Display a custom background .bmp image
Check for the current number, and names of people in space
Display the current number of astronauts
Wait for you to touch the screen and then display the names of the astronauts for 30 seconds
Install CircuitPython Code and 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_Astronauts.
Copy the contents of the PyPortal_Astronauts directory to your PyPortal's CIRCUITPY drive.
This is what the final contents of the CIRCUITPY drive will look like:
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
This example will access the open-notify people in space API, the number of
astronauts and their names... and display it on a screen!
if you can find something that spits out JSON data, we can display it
"""
import time
import board
from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.label import Label
# Set up where we'll be fetching data from
DATA_SOURCE = "http://api.open-notify.org/astros.json"
DATA_LOCATION = [["number"], ["people"]]
# 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=DATA_LOCATION,
status_neopixel=board.NEOPIXEL,
default_bg=cwd+"/astronauts_background.bmp",
text_font=cwd+"/fonts/Helvetica-Bold-100.bdf",
text_position=((225, 50), None),
text_color=(0xFFFFFF, None))
names_font = bitmap_font.load_font(cwd+"/fonts/Helvetica-Bold-16.bdf")
# pre-load glyphs for fast printing
names_font.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ()')
names_position = (10, 135)
names_color = 0xFF00FF
while True:
try:
value = pyportal.fetch()
print("Response is", value)
except RuntimeError as e:
print("Some error occured, retrying! -", e)
stamp = time.monotonic()
while (time.monotonic() - stamp) < 5 *60: # wait 5 minutes before getting again
if pyportal.touchscreen.touch_point:
names = ""
for astro in value[1]:
names += "%s (%s)\n" % (astro['name'], astro['craft'])
names = names[:-1] # remove final '\n'
names_textarea = Label(names_font, text=names)
names_textarea.x = names_position[0]
names_textarea.y = names_position[1]
names_textarea.color = names_color
pyportal.root_group.append(names_textarea)
time.sleep(30) # wait 30 seconds to read it
pyportal.root_group.pop()
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!
How it Works
The PyPortal Astronauts Display does the following things to keep you in the know about non-Earthbound humans:
Background
First, it displays a bitmap graphic named astronauts_background.bmp as the screen's background. This is a 320 x 240 pixel RGB 16-bit raster graphic in .bmp format.
Font
To display information on the screen, the PyPortal code will use a bitmapped font overlayed on top of the background. The font used here is are bitmap fonts made from the Helvetica typeface. You can learn more about converting type in this guide.
In order to speed up the display of text, the pyportal.preload_font() command is used to place the needed glyphs into memory.
JSON
How does the PyPortal know how many people are in space, as well as their names and locations? Is it talking to them when we're not looking?
That would be awesome, but actually it's not the case. Instead, the PyPortal is fetching a JSON file from the open-notify.org API. The address here has what we need: http://api.open-notify.org/astros.json
This file contains all sorts of information, delivered in an easy-to-parse format. If you visit that URL by copying the address and pasting it into the Load Url button of the online code "beautifier" https://codebeautify.org/jsonviewer you'll see the raw JSON file next to a nicely formatted version of it (choose "View" from the dropdown menu in the right-hand box to change the display format).
Here it is in a raw-er form, but still using indentation and carriage returns to make it readable:
{
"message": "success",
"number": 6,
"people": [
{
"craft": "ISS",
"name": "Oleg Kononenko"
},
{
"craft": "ISS",
"name": "David Saint-Jacques"
},
{
"craft": "ISS",
"name": "Anne McClain"
},
{
"craft": "ISS",
"name": "Alexey Ovchinin"
},
{
"craft": "ISS",
"name": "Nick Hague"
},
{
"craft": "ISS",
"name": "Christina Koch"
}
]
}Keys
Look at the code and you'll see two keys that we'll query, number, and people. The number key has a value of 6 in this case, so we'll call that a key:value pair written this way "number" : 6.
The people key doesn't have a single value, but is instead has a sub-tree with multiple other sets of key:value pairs such as:
"craft" : "ISS" and "name" : "Christina Koch"
Our CircuitPython code is able to grab and parse this data using the following variables:
DATA_SOURCE = "http://api.open-notify.org/astros.json" DATA_LOCATION = [["number"], ["people"]]
Traversing JSON
The DATA_LOCATION contains two variables that we use to traverse the JSON file. In the image here, note how there is a tree hierarchy indicated by the indentation level. The number key is at the top level of the file's hierarchy, so we can call its name directly.
The people key is also at the top level of the hierarchy but contains a sub-tree of multiple craft and name key:value pairs.
You can see this more clearly by switching to the "Form" view of the code beautifier.
PyPortal Constructor
When we set up the pyportal constructor, we are providing it with these things:
url to query
json_path to traverse and find the key:value pair we need
default_bg path and name to display the background bitmap
text_font path and name to the font used for displaying the follower count value
text_position on the screen's x/y coordinate system
text_color
Fetch
With the PyPortal set up, we can then use pyportal.fetch() to do the query and parsing of the data and then display it on screen on top of the background image.
This repeats every five minutes to stay current!
Touchscreen
You'll also notice in the code that there's a section at the bottom inside the main while True: loop that tests for the touchscreen to see if it's been pressed or not by using pyportal.touchscreen.touch_point.
When the value is true, due to being pressed, it will then display the names of the astronauts using the pyportal.root_group.append() function. After thirty seconds the names are cleared using the pyportal.root_group.pop() function.
Customization
You can customize this project to make it your own and point to different website API's as the source of your JSON data, as well as adjust the graphics and text.
Text Position
Depending on the design of your background bitmap and the length of the text you're displaying, you may want to reposition the text and caption. You can do this with the text_position and caption_position options.
The PyPortal's display is 320 pixels wide and 240 pixels high. In order to refer to those positions on the screen, we use an x/y coordinate system, where x is horizontal and y is vertical.
The origin of this coordinate system is the upper left corner. This means that a pixel placed at the upper left corner would be (0,0) and the lower right corner would be (320, 240).
Text Color
Another way to customize your display is to adjust the color of the text. The line text_color=0xFFFFFF in the constructor shows how. You will need to use the hexadecimal value for any color you want to display.
You can use something like https://htmlcolorcodes.com/ to pick your color and then copy the hex value, in this example it would be 0x0ED9EE.
Background Image
If you would like to create your own background, awesome! You'll want to save the file with these specifications:
320 x 240 pixels
16-bit RGB color (8-bits per channel)
Save file as .bmp format
You can then copy the .bmp file to the root level of the CIRCUITPY drive. Make sure you refer to this new filename in the pyportal constructor line:
default_bg=cwd+"/astronaut_background.bmp"
Change that line to use the new filename name, such as:
default_bg=cwd+"/my_new_background.bmp"