from machine import Pin, I2C
import ssd1306
import network
import urequests as requests
import time
import ujson
# entity to fetch wait time for
entity_id = "352feb94-e52e-45eb-9c92-e4b44c6b1a9d" # Pirates
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
button = Pin(33, Pin.IN, Pin.PULL_UP)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
def text_width(text, char_width=8):
return len(text) * char_width
def write_text_to_screen(ride_name, wait_time):
max_width = oled.width
estimated_width = text_width(ride_name)
x_position = 0 # Start off-screen to the right
move_right = False
# Determine the minimum x_position for the left boundary
min_x_position = max_width - estimated_width
while True:
oled.fill(0) # Clear the screen
# Display the ride name
oled.text(ride_name, x_position, 0)
# Display the wait time on the next line
oled.text(wait_time, 0, 10)
oled.show()
# Update the x position for the next frame
if estimated_width <= max_width:
break # No need to scroll if the text fits
if move_right:
x_position += 1
if x_position >= 0:
time.sleep(0.5)
move_right = False
else:
x_position -= 1
if x_position <= min_x_position:
time.sleep(0.5)
move_right = True
time.sleep(0.01) # Adjust for desired scrolling speed
def fetch_wait_time():
print("Fetching wait times...")
res = requests.get(url = f'https://api.themeparks.wiki/v1/entity/{entity_id}/live')
if res.status_code != 200:
res.raise_for_status()
json_data = ujson.loads(res.text)
ride_name = json_data.get('name')
wait_time = json_data.get('liveData', [{}])[0].get('queue', {}).get('STANDBY', {}).get('waitTime')
if wait_time is None:
print("Wait time is none, using ride status")
wait_time = json_data.get('liveData', [{}])[0].get('status', None)
if wait_time != None:
print(f"{ride_name}: {str(wait_time)}")
return ride_name, wait_time
print(f"{ride_name}: {str(wait_time)}")
return ride_name, wait_time
def update_wait_time():
ride_name, wait_time = fetch_wait_time()
if wait_time != None:
write_text_to_screen(ride_name, f"Wait: {wait_time}")
else:
write_text_to_screen("Unknown", "")
# wait until WiFi is available...
write_text_to_screen("WiFi...", "")
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
write_text_to_screen("Updating...", "")
last_update_time = 0
update_interval = 300 # Update every 300 seconds (5 minutes)
# initial fetch of wait time
update_wait_time()
while True:
current_time = time.time()
# Update wait time every 5 minutes
if current_time - last_update_time >= update_interval:
update_wait_time()
last_update_time = current_time
# Check if the button is pressed for immediate update
if button.value() == 0:
update_wait_time()
while button.value() == 0: # Wait for button release
time.sleep(0.1)
# Short delay to prevent high CPU usage
time.sleep(0.1)