Skip to content

Commit ac1e119

Browse files
committed
Changed to Connection Manager
refactored with Connection Manager, added pretty hierarchy serial view.
1 parent 40e0c17 commit ac1e119

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

examples/wifi/expanded/requests_wifi_api_rocketlaunch_live.py

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@
33
# Coded for Circuit Python 8.2.x
44
"""RocketLaunch.Live API Example"""
55

6-
import gc
76
import os
8-
import ssl
97
import time
10-
11-
import socketpool
128
import wifi
13-
9+
import adafruit_connection_manager
1410
import adafruit_requests
1511

16-
# Initialize WiFi Pool (There can be only 1 pool & top of script)
17-
pool = socketpool.SocketPool(wifi.radio)
18-
1912
# Time between API refreshes
2013
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
2114
sleep_time = 43200
@@ -45,72 +38,86 @@ def time_calc(input_time):
4538
return time_output
4639

4740

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)
4943
ROCKETLAUNCH_SOURCE = "https://fdo.rocketlaunch.live/json/launches/next/1"
5044

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)
6549

6650
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!")
6761
try:
6862
# Print Request to Serial
69-
print("Attempting to GET RocketLaunch.Live JSON!")
63+
print(" | Attempting to GET RocketLaunch.Live JSON!")
64+
time.sleep(2)
7065
debug_rocketlaunch_full_response = False
7166

72-
rocketlaunch_response = requests.get(url=ROCKETLAUNCH_SOURCE)
7367
try:
68+
rocketlaunch_response = requests.get(url=ROCKETLAUNCH_SOURCE)
7469
rocketlaunch_json = rocketlaunch_response.json()
7570
except ConnectionError as e:
76-
print("Connection Error:", e)
71+
print("Connection Error:", e)
7772
print("Retrying in 10 seconds")
73+
print(" | ✅ RocketLaunch.Live JSON!")
74+
7875
if debug_rocketlaunch_full_response:
7976
print("Full API GET URL: ", ROCKETLAUNCH_SOURCE)
8077
print(rocketlaunch_json)
8178

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}")
106113

107114
print("\nFinished!")
108115
print("Board Uptime: ", time.monotonic())
109116
print("Next Update in: ", time_calc(sleep_time))
110117
print("===============================")
111-
gc.collect()
118+
112119
except (ValueError, RuntimeError) as e:
113120
print("Failed to get data, retrying\n", e)
114121
time.sleep(60)
115-
continue
122+
break
116123
time.sleep(sleep_time)

0 commit comments

Comments
 (0)