|
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Coded for Circuit Python 8.2.x |
| 4 | +"""RocketLaunch.Live API Example""" |
| 5 | + |
| 6 | +import gc |
| 7 | +import os |
| 8 | +import ssl |
| 9 | +import time |
| 10 | +import socketpool |
| 11 | +import wifi |
| 12 | +import adafruit_requests |
| 13 | + |
| 14 | +# Initialize WiFi Pool (There can be only 1 pool & top of script) |
| 15 | +pool = socketpool.SocketPool(wifi.radio) |
| 16 | + |
| 17 | +# Time between API refreshes |
| 18 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 19 | +sleep_time = 43200 |
| 20 | + |
| 21 | +# Get WiFi details, ensure these are setup in settings.toml |
| 22 | +ssid = os.getenv("WIFI_SSID") |
| 23 | +password = os.getenv("WIFI_PASSWORD") |
| 24 | + |
| 25 | + |
| 26 | +# Converts seconds in minutes/hours/days |
| 27 | +def time_calc(input_time): |
| 28 | + if input_time < 60: |
| 29 | + sleep_int = input_time |
| 30 | + time_output = f"{sleep_int:.0f} seconds" |
| 31 | + elif 60 <= input_time < 3600: |
| 32 | + sleep_int = input_time / 60 |
| 33 | + time_output = f"{sleep_int:.0f} minutes" |
| 34 | + elif 3600 <= input_time < 86400: |
| 35 | + sleep_int = input_time / 60 / 60 |
| 36 | + time_output = f"{sleep_int:.0f} hours" |
| 37 | + elif 86400 <= input_time < 432000: |
| 38 | + sleep_int = input_time / 60 / 60 / 24 |
| 39 | + time_output = f"{sleep_int:.1f} days" |
| 40 | + else: # if > 5 days convert float to int & display whole days |
| 41 | + sleep_int = input_time / 60 / 60 / 24 |
| 42 | + time_output = f"{sleep_int:.0f} days" |
| 43 | + return time_output |
| 44 | + |
| 45 | + |
| 46 | +# Free Public API, no token or header required |
| 47 | +ROCKETLAUNCH_SOURCE = "https://fdo.rocketlaunch.live/json/launches/next/1" |
| 48 | + |
| 49 | +# Connect to Wi-Fi |
| 50 | +print("\n===============================") |
| 51 | +print("Connecting to WiFi...") |
| 52 | +requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 53 | +while not wifi.radio.ipv4_address: |
| 54 | + try: |
| 55 | + wifi.radio.connect(ssid, password) |
| 56 | + except ConnectionError as e: |
| 57 | + print("❌ Connection Error:", e) |
| 58 | + print("Retrying in 10 seconds") |
| 59 | + time.sleep(10) |
| 60 | + gc.collect() |
| 61 | +print("✅ WiFi!") |
| 62 | +print("===============================") |
| 63 | + |
| 64 | +while True: |
| 65 | + try: |
| 66 | + # Print Request to Serial |
| 67 | + print("Attempting to GET RocketLaunch.Live JSON!") |
| 68 | + debug_rocketlaunch_full_response = False |
| 69 | + |
| 70 | + rocketlaunch_response = requests.get(url=ROCKETLAUNCH_SOURCE) |
| 71 | + try: |
| 72 | + rocketlaunch_json = rocketlaunch_response.json() |
| 73 | + except ConnectionError as e: |
| 74 | + print("❌ Connection Error:", e) |
| 75 | + print("Retrying in 10 seconds") |
| 76 | + if debug_rocketlaunch_full_response: |
| 77 | + print("Full API GET URL: ", ROCKETLAUNCH_SOURCE) |
| 78 | + print(rocketlaunch_json) |
| 79 | + |
| 80 | + print("✅ RocketLaunch.Live JSON!") |
| 81 | + rocketlaunch_flightname = str(rocketlaunch_json["result"][0]["name"]) |
| 82 | + print(f" | Flight Name: {rocketlaunch_flightname}") |
| 83 | + rocketlaunch_provider = str(rocketlaunch_json["result"][0]["provider"]["name"]) |
| 84 | + print(f" | Provider: {rocketlaunch_provider}") |
| 85 | + rocketlaunch_vehiclename = str( |
| 86 | + rocketlaunch_json["result"][0]["vehicle"]["name"] |
| 87 | + ) |
| 88 | + print(f" | Vehicle Name: {rocketlaunch_vehiclename}") |
| 89 | + |
| 90 | + rocketlaunch_winopen = str(rocketlaunch_json["result"][0]["win_open"]) |
| 91 | + rocketlaunch_winclose = str(rocketlaunch_json["result"][0]["win_close"]) |
| 92 | + print(f" | Window: {rocketlaunch_winopen} to {rocketlaunch_winclose}") |
| 93 | + |
| 94 | + rocketlaunch_sitename = str( |
| 95 | + rocketlaunch_json["result"][0]["pad"]["location"]["name"] |
| 96 | + ) |
| 97 | + print(f" | Launch Site: {rocketlaunch_sitename}") |
| 98 | + |
| 99 | + rocketlaunch_mission = str( |
| 100 | + rocketlaunch_json["result"][0]["mission_description"] |
| 101 | + ) |
| 102 | + if rocketlaunch_mission != "None": |
| 103 | + print(f" | Mission: {rocketlaunch_mission}") |
| 104 | + |
| 105 | + print("\nFinished!") |
| 106 | + print("Board Uptime: ", time.monotonic()) |
| 107 | + print("Next Update in: ", time_calc(sleep_time)) |
| 108 | + print("===============================") |
| 109 | + gc.collect() |
| 110 | + except (ValueError, RuntimeError) as e: |
| 111 | + print("Failed to get data, retrying\n", e) |
| 112 | + time.sleep(60) |
| 113 | + continue |
| 114 | + time.sleep(sleep_time) |
0 commit comments