|
3 | 3 | # Coded for Circuit Python 8.2.x
|
4 | 4 | """RocketLaunch.Live API Example"""
|
5 | 5 |
|
6 |
| -import gc |
7 | 6 | import os
|
8 |
| -import ssl |
9 | 7 | import time
|
10 |
| - |
11 |
| -import socketpool |
12 | 8 | import wifi
|
13 |
| - |
| 9 | +import adafruit_connection_manager |
14 | 10 | import adafruit_requests
|
15 | 11 |
|
16 |
| -# Initialize WiFi Pool (There can be only 1 pool & top of script) |
17 |
| -pool = socketpool.SocketPool(wifi.radio) |
18 |
| - |
19 | 12 | # Time between API refreshes
|
20 | 13 | # 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
|
21 | 14 | sleep_time = 43200
|
@@ -45,72 +38,86 @@ def time_calc(input_time):
|
45 | 38 | return time_output
|
46 | 39 |
|
47 | 40 |
|
48 |
| -# Free Public API, no token or header required |
| 41 | +# Publicly available data no header required |
| 42 | +# The number at the end is the amount of launches (max 5 free api) |
49 | 43 | ROCKETLAUNCH_SOURCE = "https://fdo.rocketlaunch.live/json/launches/next/1"
|
50 | 44 |
|
51 |
| -# Connect to Wi-Fi |
52 |
| -print("\n===============================") |
53 |
| -print("Connecting to WiFi...") |
54 |
| -requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
55 |
| -while not wifi.radio.ipv4_address: |
56 |
| - try: |
57 |
| - wifi.radio.connect(ssid, password) |
58 |
| - except ConnectionError as e: |
59 |
| - print("❌ Connection Error:", e) |
60 |
| - print("Retrying in 10 seconds") |
61 |
| - time.sleep(10) |
62 |
| - gc.collect() |
63 |
| -print("✅ WiFi!") |
64 |
| -print("===============================") |
| 45 | +# Initalize Wifi, Socket Pool, Request Session |
| 46 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 47 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 48 | +requests = adafruit_requests.Session(pool, ssl_context) |
65 | 49 |
|
66 | 50 | while True:
|
| 51 | + # Connect to Wi-Fi |
| 52 | + print("\n===============================") |
| 53 | + print("Connecting to WiFi...") |
| 54 | + while not wifi.radio.ipv4_address: |
| 55 | + try: |
| 56 | + wifi.radio.connect(ssid, password) |
| 57 | + except ConnectionError as e: |
| 58 | + print("❌ Connection Error:", e) |
| 59 | + print("Retrying in 10 seconds") |
| 60 | + print("✅ Wifi!") |
67 | 61 | try:
|
68 | 62 | # Print Request to Serial
|
69 |
| - print("Attempting to GET RocketLaunch.Live JSON!") |
| 63 | + print(" | Attempting to GET RocketLaunch.Live JSON!") |
| 64 | + time.sleep(2) |
70 | 65 | debug_rocketlaunch_full_response = False
|
71 | 66 |
|
72 |
| - rocketlaunch_response = requests.get(url=ROCKETLAUNCH_SOURCE) |
73 | 67 | try:
|
| 68 | + rocketlaunch_response = requests.get(url=ROCKETLAUNCH_SOURCE) |
74 | 69 | rocketlaunch_json = rocketlaunch_response.json()
|
75 | 70 | except ConnectionError as e:
|
76 |
| - print("❌ Connection Error:", e) |
| 71 | + print("Connection Error:", e) |
77 | 72 | print("Retrying in 10 seconds")
|
| 73 | + print(" | ✅ RocketLaunch.Live JSON!") |
| 74 | + |
78 | 75 | if debug_rocketlaunch_full_response:
|
79 | 76 | print("Full API GET URL: ", ROCKETLAUNCH_SOURCE)
|
80 | 77 | print(rocketlaunch_json)
|
81 | 78 |
|
82 |
| - print("✅ RocketLaunch.Live JSON!") |
83 |
| - rocketlaunch_flightname = str(rocketlaunch_json["result"][0]["name"]) |
84 |
| - print(f" | Flight Name: {rocketlaunch_flightname}") |
85 |
| - rocketlaunch_provider = str(rocketlaunch_json["result"][0]["provider"]["name"]) |
86 |
| - print(f" | Provider: {rocketlaunch_provider}") |
87 |
| - rocketlaunch_vehiclename = str( |
88 |
| - rocketlaunch_json["result"][0]["vehicle"]["name"] |
89 |
| - ) |
90 |
| - print(f" | Vehicle Name: {rocketlaunch_vehiclename}") |
91 |
| - |
92 |
| - rocketlaunch_winopen = str(rocketlaunch_json["result"][0]["win_open"]) |
93 |
| - rocketlaunch_winclose = str(rocketlaunch_json["result"][0]["win_close"]) |
94 |
| - print(f" | Window: {rocketlaunch_winopen} to {rocketlaunch_winclose}") |
95 |
| - |
96 |
| - rocketlaunch_sitename = str( |
97 |
| - rocketlaunch_json["result"][0]["pad"]["location"]["name"] |
98 |
| - ) |
99 |
| - print(f" | Launch Site: {rocketlaunch_sitename}") |
100 |
| - |
101 |
| - rocketlaunch_mission = str( |
102 |
| - rocketlaunch_json["result"][0]["mission_description"] |
103 |
| - ) |
104 |
| - if rocketlaunch_mission != "None": |
105 |
| - print(f" | Mission: {rocketlaunch_mission}") |
| 79 | + # JSON Endpoints |
| 80 | + RLFN = str(rocketlaunch_json["result"][0]["name"]) |
| 81 | + RLWO = str(rocketlaunch_json["result"][0]["win_open"]) |
| 82 | + TMINUS = str(rocketlaunch_json["result"][0]["t0"]) |
| 83 | + RLWC = str(rocketlaunch_json["result"][0]["win_close"]) |
| 84 | + RLP = str(rocketlaunch_json["result"][0]["provider"]["name"]) |
| 85 | + RLVN = str(rocketlaunch_json["result"][0]["vehicle"]["name"]) |
| 86 | + RLPN = str(rocketlaunch_json["result"][0]["pad"]["name"]) |
| 87 | + RLLS = str(rocketlaunch_json["result"][0]["pad"]["location"]["name"]) |
| 88 | + RLLD = str(rocketlaunch_json["result"][0]["launch_description"]) |
| 89 | + RLM = str(rocketlaunch_json["result"][0]["mission_description"]) |
| 90 | + RLDATE = str(rocketlaunch_json["result"][0]["date_str"]) |
| 91 | + |
| 92 | + # Print to serial & display label if endpoint not "None" |
| 93 | + if RLDATE != "None": |
| 94 | + print(f" | | Date: {RLDATE}") |
| 95 | + if RLFN != "None": |
| 96 | + print(f" | | Flight: {RLFN}") |
| 97 | + if RLP != "None": |
| 98 | + print(f" | | Provider: {RLP}") |
| 99 | + if RLVN != "None": |
| 100 | + print(f" | | Vehicle: {RLVN}") |
| 101 | + if RLWO != "None": |
| 102 | + print(f" | | Window: {RLWO} to {RLWC}") |
| 103 | + elif TMINUS != "None": |
| 104 | + print(f" | | Window: {TMINUS} to {RLWC}") |
| 105 | + if RLLS != "None": |
| 106 | + print(f" | | Site: {RLLS}") |
| 107 | + if RLPN != "None": |
| 108 | + print(f" | | Pad: {RLPN}") |
| 109 | + if RLLD != "None": |
| 110 | + print(f" | | Description: {RLLD}") |
| 111 | + if RLM != "None": |
| 112 | + print(f" | | Mission: {RLM}") |
106 | 113 |
|
107 | 114 | print("\nFinished!")
|
108 | 115 | print("Board Uptime: ", time.monotonic())
|
109 | 116 | print("Next Update in: ", time_calc(sleep_time))
|
110 | 117 | print("===============================")
|
111 |
| - gc.collect() |
| 118 | + |
112 | 119 | except (ValueError, RuntimeError) as e:
|
113 | 120 | print("Failed to get data, retrying\n", e)
|
114 | 121 | time.sleep(60)
|
115 |
| - continue |
| 122 | + break |
116 | 123 | time.sleep(sleep_time)
|
0 commit comments