-
-
Save TristanAmond/165fb71462f8fd6e1b5a152856f81f9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from adafruit_display_text import label | |
from adafruit_bitmap_font import bitmap_font | |
import board | |
import gc | |
import time | |
import busio | |
from digitalio import DigitalInOut, Pull | |
import neopixel | |
import json | |
import adafruit_apds9960.apds9960 | |
from adafruit_apds9960 import colorutility | |
from adafruit_matrixportal.matrix import Matrix | |
import adafruit_requests as requests | |
from adafruit_esp32spi import adafruit_esp32spi | |
from adafruit_esp32spi import adafruit_esp32spi_wifimanager | |
import display_manager | |
# --- CONSTANTS SETUP ---- | |
try: | |
from secrets import secrets | |
except ImportError: | |
print("Wifi + constants are kept in secrets.py, please add them there!") | |
raise | |
# local Metro station | |
station_code = secrets["station_code"] | |
# width of total displays in pixels | |
# NOTE this width is set for 2 64x32 RGB LED Matrix panels | |
# (https://www.adafruit.com/product/2278) | |
width = 128 | |
# --- INITIALIZE DISPLAY ----------------------------------------------- | |
# MATRIX DISPLAY MANAGER | |
matrix = Matrix(width=128, height=32, bit_depth=2, tile_rows=1) | |
display_manager = display_manager.display_manager(matrix.display) | |
print("display manager loaded") | |
# --- WIFI SETUP ------------- | |
# Initialize ESP32 Pins: | |
esp32_cs = DigitalInOut(board.ESP_CS) | |
esp32_ready = DigitalInOut(board.ESP_BUSY) | |
esp32_reset = DigitalInOut(board.ESP_RESET) | |
# Initialize wifi components | |
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) | |
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) | |
# Initialize neopixel status light | |
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) | |
# Initialize wifi object | |
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light, attempts=5) | |
print("WiFi loaded") | |
gc.collect() | |
# --- FUNCTIONS --- | |
# queries WMATA API to return a dict with all unique train destinations, sorted by min | |
# input is StationCode from WMATA API | |
def get_trains(StationCode): | |
try: | |
# query WMATA API with input StationCode | |
URL = 'https://api.wmata.com/StationPrediction.svc/json/GetPrediction/' | |
payload = {'api_key': secrets['wmata api key']} | |
response = wifi.get(URL + StationCode, headers=payload) | |
json_data = response.json() | |
except Exception as e: | |
print("Failed to get data, retrying\n", e) | |
wifi.reset() | |
try: | |
for item in json_data["Trains"]: | |
print(item) | |
except Exception as e: | |
print(e) | |
get_trains(station_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment