# solarxml_rpipico.py -rw-r--r-- 2.6 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
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)))