1

I'm working on a project using an ESP32 with CircuitPython, specifically the Adafruit Matrix Portal M4. I'm trying to fetch data from the an API (https://api.wheresthefuckingtrain.com/by-id/A31), but I'm encountering a BrokenPipeError when I attempt to retrieve the data.

Retrieving data...Traceback (most recent call last):
  File "code.py", line 81, in <module>
  File "code.py", line 25, in get_arrival_times
  File "adafruit_portalbase/network.py", line 645, in fetch_data
  File "adafruit_portalbase/network.py", line 563, in fetch
  File "adafruit_requests.py", line 715, in get
  File "adafruit_requests.py", line 647, in request
  File "adafruit_connection_manager.py", line 347, in get_socket
  File "adafruit_connection_manager.py", line 249, in _get_connected_socket
  File "adafruit_connection_manager.py", line 61, in connect
  File "adafruit_esp32spi/adafruit_esp32spi_socketpool.py", line 114, in connect
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 917, in socket_connect
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 813, in socket_open
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 430, in _send_command_get_response
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 400, in _wait_response_cmd
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 382, in _check_data
BrokenPipeError: Expected 01 but got 00

Here is what my code looks like, following the source code from this creator:

import time
import microcontroller
from board import NEOPIXEL
import displayio
import adafruit_display_text.label
from adafruit_datetime import datetime
from adafruit_bitmap_font import bitmap_font
from adafruit_matrixportal.matrix import Matrix
from adafruit_matrixportal.network import Network

STOP_ID = 'A31'
DATA_SOURCE = 'https://api.wheresthefuckingtrain.com/by-id/%s' % (STOP_ID,)
DATA_LOCATION = ["data"]
UPDATE_DELAY = 15
SYNC_TIME_DELAY = 30
MINIMUM_MINUTES_DISPLAY = 9
BACKGROUND_IMAGE = 'assets/g-dashboard.bmp'
ERROR_RESET_THRESHOLD = 3

def get_arrival_in_minutes_from_now(now, date_str):
    train_date = datetime.fromisoformat(date_str).replace(tzinfo=None) # Remove tzinfo to be able to diff dates
    return round((train_date-now).total_seconds()/60.0)

def get_arrival_times():
    stop_trains =  network.fetch_data(DATA_SOURCE, json_path=(DATA_LOCATION,))
    stop_data = stop_trains[0]
    nortbound_trains = [x['time'] for x in stop_data['N']]
    southbound_trains = [x['time'] for x in stop_data['S']]

    now = datetime.now()
    print("Now: ", now)

    nortbound_arrivals = [get_arrival_in_minutes_from_now(now, x) for x in nortbound_trains]
    southound_arrivals = [get_arrival_in_minutes_from_now(now, x) for x in southbound_trains]

    n = [str(x) for x in nortbound_arrivals if x>= MINIMUM_MINUTES_DISPLAY]
    s = [str(x) for x in southound_arrivals if x>= MINIMUM_MINUTES_DISPLAY]

    n0 = n[0] if len(n) > 0 else '-'
    n1 = n[1] if len(n) > 1 else '-'
    s0 = s[0] if len(s) > 0 else '-'
    s1 = s[1] if len(s) > 1 else '-'

    return n0,n1,s0,s1

def update_text(n0, n1, s0, s1):
    text_lines[2].text = "%s,%s m" % (n0,n1)
    text_lines[4].text = "%s,%s m" % (s0,s1)
    display.root_group = group

# --- Display setup ---
matrix = Matrix()
display = matrix.display
network = Network(status_neopixel=NEOPIXEL, debug=False)

# --- Drawing setup ---
group = displayio.Group()
bitmap = displayio.OnDiskBitmap(open(BACKGROUND_IMAGE, 'rb'))
colors = [0x444444, 0xDD8000]  # [dim white, gold]

font = bitmap_font.load_font("assets/fonts/6x10.bdf")
text_lines = [
    displayio.TileGrid(bitmap, pixel_shader=getattr(bitmap, 'pixel_shader', displayio.ColorConverter())),
    adafruit_display_text.label.Label(font, color=colors[0], x=20, y=3, text="Queens"),
    adafruit_display_text.label.Label(font, color=colors[1], x=20, y=11, text="- mins"),
    adafruit_display_text.label.Label(font, color=colors[0], x=20, y=20, text="Church"),
    adafruit_display_text.label.Label(font, color=colors[1], x=20, y=28, text="- mins"),
]
for x in text_lines:
    group.append(x)
display.root_group = group

error_counter = 0
last_time_sync = None
while True:
    try:
        if last_time_sync is None or time.monotonic() > last_time_sync + SYNC_TIME_DELAY:
            # Sync clock to minimize time drift
            network.get_local_time()
            last_time_sync = time.monotonic()
        arrivals = get_arrival_times()
        update_text(*arrivals)
    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)
        error_counter = error_counter + 1
        if error_counter > ERROR_RESET_THRESHOLD:
            microcontroller.reset()

    time.sleep(UPDATE_DELAY)

I've seen elsewhere online that I might need to upgrade the Nina firmware. When doing so, I get stuck on the "Upload NINA Firmware" step, and the Adafruit ESPTool has the error:

Connecting...
Connected successfully.
Try hard reset.
[Object.debug:191] Finished read loop
Error: Couldn't sync to ESP. Try resetting.

I keep resetting to no avail. Can someone please help?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.