|
12 | 12 | import adafruit_requests
|
13 | 13 | from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
|
14 | 14 |
|
15 |
| -# FarmSense API for moon phase |
16 |
| -# https://www.farmsense.net/api/astro-widgets/ |
17 |
| -url = "https://api.farmsense.net/v1/moonphases/?d=" |
18 |
| -# Adafruit IO time server for UNIX time, no API key needed |
19 |
| -time_url = "https://io.adafruit.com/api/v2/time/seconds" |
| 15 | +# US Navy Astronomical Applications API for moon phase |
| 16 | +# https://aa.usno.navy.mil/api |
| 17 | +# Your location coordinates (adjust these to your location) |
| 18 | +LATITUDE = 40.71 |
| 19 | +LONGITUDE = -74.0060 |
| 20 | +TIMEZONE = -5 # EST/EDT, adjust for your timezone |
| 21 | +DST = True # Set to False if not in daylight saving time |
| 22 | + |
| 23 | +# Adafruit IO time server for current date, no API key needed |
| 24 | +date_url = "https://io.adafruit.com/api/v2/time/ISO-8601" |
20 | 25 | # connect to wifi
|
21 | 26 | try:
|
22 | 27 | wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
|
|
42 | 47 | WAXING_GIBBOUS = 3
|
43 | 48 | FULL_MOON = 4
|
44 | 49 | WANING_GIBBOUS = 5
|
45 |
| -THIRD_QUARTER = 6 |
| 50 | +LAST_QUARTER = 6 |
46 | 51 | WANING_CRESCENT = 7
|
47 | 52 | DARK_MOON = 8
|
48 | 53 | RED_MOON = 9
|
49 | 54 | # strings that match return from API
|
50 | 55 | phase_names = ["New Moon", "Waxing Crescent", "First Quarter", "Waxing Gibbous",
|
51 |
| - "Full Moon", "Waning Gibbous", "Third Quarter", "Waning Crescent","Dark Moon","Red Moon"] |
| 56 | + "Full Moon", "Waning Gibbous", "Last Quarter", "Waning Crescent","Dark Moon","Red Moon"] |
52 | 57 |
|
53 | 58 | # functions for each moon phase to light up based on neopixel orientation
|
54 | 59 | def set_new_moon():
|
@@ -87,7 +92,7 @@ def set_waning_gibbous():
|
87 | 92 | pixels[i] = ON
|
88 | 93 | pixels.show()
|
89 | 94 |
|
90 |
| -def set_third_quarter(): |
| 95 | +def set_last_quarter(): |
91 | 96 | pixels.fill(OFF)
|
92 | 97 | for i in range(0, 24):
|
93 | 98 | pixels[i] = ON
|
@@ -117,7 +122,7 @@ def set_red_moon():
|
117 | 122 | WAXING_GIBBOUS: set_waxing_gibbous,
|
118 | 123 | FULL_MOON: set_full_moon,
|
119 | 124 | WANING_GIBBOUS: set_waning_gibbous,
|
120 |
| - THIRD_QUARTER: set_third_quarter, |
| 125 | + LAST_QUARTER: set_last_quarter, |
121 | 126 | WANING_CRESCENT: set_waning_crescent,
|
122 | 127 | DARK_MOON: set_dark_moon,
|
123 | 128 | RED_MOON: set_red_moon
|
@@ -152,19 +157,26 @@ def set_moon_phase(phase):
|
152 | 157 | while True:
|
153 | 158 | try:
|
154 | 159 | if first_run or ticks_diff(ticks_ms(), timer_clock) >= timer:
|
155 |
| - # get unix time |
156 |
| - unix_time = requests.get(time_url) |
157 |
| - # update farmsense request with UNIX time |
158 |
| - url = f"https://api.farmsense.net/v1/moonphases/?d={unix_time.text}" |
| 160 | + # get current date |
| 161 | + date_response = requests.get(date_url) |
| 162 | + iso_date = date_response.text.strip('"') |
| 163 | + current_date = iso_date.split('T')[0] |
| 164 | + date_response.close() |
| 165 | + # build Navy API URL with parameters |
| 166 | + # pylint: disable=line-too-long |
| 167 | + url = f"https://aa.usno.navy.mil/api/rstt/oneday?date={current_date}&coords={LATITUDE},{LONGITUDE}&tz={TIMEZONE}" |
| 168 | + if DST: |
| 169 | + url += "&dst=true" |
159 | 170 | # get the JSON response
|
160 | 171 | response = requests.get(url)
|
161 | 172 | json_response = response.json()
|
162 | 173 | # isolate phase info
|
| 174 | + current_phase = json_response["properties"]["data"]["curphase"] |
163 | 175 | print("-" * 40)
|
164 |
| - print(json_response[0]['Phase']) |
| 176 | + print(current_phase) |
165 | 177 | print("-" * 40)
|
166 | 178 | # run function to update neopixels with current phase
|
167 |
| - set_moon_phase(json_response[0]['Phase']) |
| 179 | + set_moon_phase(current_phase) |
168 | 180 | response.close()
|
169 | 181 | time.sleep(1)
|
170 | 182 | first_run = False
|
|
0 commit comments