Edited to my latest version, still got the weird output when connected to WiFi only. When connected via computer and Thonny it just works fine. See below, link to video’s attached. Hope someone can point out what I do wrong here
import WIFI_CONFIG
from network_manager import NetworkManager
import uasyncio
import urequests
import time
import math
from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
URL = "https://ovzoeker.nl/api/arrivals/2339619"
graphics = PicoGraphics(DISPLAY)
cu = CosmicUnicorn()
cu.clear()
def get_local_timestamp(utc_timestamp):
# Calculate the time zone difference (in seconds) between your device and the server
time_zone_offset = (time.mktime(time.localtime()) - time.mktime(time.gmtime())) * 1000
return utc_timestamp + time_zone_offset
# for Handling the wifi connection
def status_handler(mode, status, ip):
# reports wifi connection status
print(mode, status, ip)
print('Connecting to wifi...')
if status is not None:
if status:
print('Wifi connection successful!')
else:
print('Wifi connection failed!')
try:
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
except Exception as e:
print(f'Wifi connection failed! {e}')
def get_all_data(url):
try:
response = urequests.get(url)
if response.status_code == 200:
data = response.json()["arrivals"]
response.close()
return data
response.close()
return None
except OSError as e:
print("Error:", e)
return None
def get_countdown_time(countdown_seconds):
minutes = countdown_seconds // 60
return f"{minutes:02d} min"
def scroll_text(text, scale=1):
text_width = graphics.measure_text(text, scale=scale)
x_offset = 128
while True:
while x_offset > -text_width:
graphics.set_pen(graphics.create_pen(0, 0, 0))
graphics.clear()
graphics.set_pen(graphics.create_pen(255, 0, 0))
graphics.text(line, 0, 0, scale=1, spacing=1)
graphics.set_pen(graphics.create_pen(0, 255, 0))
graphics.text(text, x_offset, 10, scale=1, spacing=1)
graphics.set_pen(graphics.create_pen(0, 0, 255))
graphics.text(countdown_time, 0, 20, scale=1, spacing=1)
cu.update(graphics)
x_offset -= 1
time.sleep(0.03)
# Reset x_offset to the right side of the display for continuous scrolling
x_offset = 128
try:
while True:
data = get_all_data(URL)
if data is None:
print("Request failed")
time.sleep(5)
continue
# Find the bus with the least countdown time
min_countdown_seconds = None
next_bus = None
for item in data:
local_timestamp = get_local_timestamp(item['ts'])
countdown_seconds = max(0, int(local_timestamp - time.time()))
if countdown_seconds > 0 and (min_countdown_seconds is None or countdown_seconds < min_countdown_seconds):
min_countdown_seconds = countdown_seconds
next_bus = item
if next_bus is not None:
graphics.set_pen(graphics.create_pen(0, 0, 0)) # Set black background
graphics.clear()
line = f"{next_bus['route_short_name']}"
direction = f"{next_bus['trip_headsign']}"
countdown_time = get_countdown_time(min_countdown_seconds)
graphics.set_pen(graphics.create_pen(255, 0, 0))
graphics.text(line, 0, 0, scale=1, spacing=1)
graphics.set_pen(graphics.create_pen(0, 255, 0))
graphics.text(direction, 0, 10, scale=1, spacing=1)
graphics.set_pen(graphics.create_pen(0, 0, 255))
graphics.text(countdown_time, 0, 20, scale=1, spacing=1)
cu.update(graphics)
# Scroll the direction text continuously
scroll_text(direction)
time.sleep(60) # Fetch data every 60 seconds
except KeyboardInterrupt:
# Handle Ctrl+C gracefully
print("\nExiting...")
except Exception as e:
print("Error:", e)