"""
This script for RPi Pico W MicroPython does:
1. Connect to WiFi using hardcoded credentials,
2. Downloads the solar data XML
3. Parses it and prints parsed data to the console.
While it doesn't do much on its own, it should be a nice
base for something more useful.
Greets, SP6MR
"""
import network
import time
ssid = "WIFI_SSID"
password = "WIFI_PASS"
url = "https://www.hamqsl.com/solarxml.php"
def connect(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print("Connecting to WiFi...", end="")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print(" Connected!")
print("Network config:", wlan.ifconfig())
return wlan.ifconfig()
connect(ssid, password)
try:
import xmltok
except ImportError:
import mip
mip.install("xmltok")
import xmltok
try:
import requests
except ImportError:
import mip
mip.install("requests")
import requests
class GetBytes():
def __init__(self, f):
self.f = f
def read(self, n: int) -> str:
return str(self.f.read(n), "utf-8")
def get_solar() -> Optional[Map[str, str]]:
retval = {}
response = requests.get(url)
if response.status_code != 200:
print(f"HTTP Request returned {response.status_code}")
return None
path = []
name_1 = None
name_2 = None
for i in xmltok.tokenize(GetBytes(response.raw)):
if i[0] == "START_TAG":
path.append(i[1][1])
elif i[0] == "END_TAG":
popped_tag = path.pop()
if popped_tag != i[1][1]:
print("XML isn't well formed! Expected {popped_tag} to close, but got {i[1]}")
elif i[0] == "ATTR":
if path == ["solar", "solardata", "calculatedconditions", "band"]:
if i[1][1] == "name":
name_1 = i[2]
if i[1][1] == "time":
name_2 = i[2]
elif path == ["solar", "solardata", "calculatedvhfconditions", "phenomenon"]:
if i[1][1] == "name":
name_1 = i[2]
if i[1][1] == "location":
name_2 = i[2]
elif i[0] == "TEXT":
if path == ["solar", "solardata", "calculatedconditions", "band"] or \
path == ["solar", "solardata", "calculatedvhfconditions", "phenomenon"]:
retval[f"{name_1} {name_2}"] = i[1]
current_band_name = None
current_band_time = None
response.close()
return retval
result = get_solar()
print("\n".join(map(lambda x: f"{x} -> {result[x]}", result)))