Skip to content

Commit 167227a

Browse files
authored
Merge pull request #3132 from adafruit/moon_phase
update moon phase clock
2 parents d81895f + f0d1744 commit 167227a

File tree

1 file changed

+27
-15
lines changed
  • QT_Py/NeoPixel_Moon_Phase_Clock

1 file changed

+27
-15
lines changed

QT_Py/NeoPixel_Moon_Phase_Clock/code.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
import adafruit_requests
1313
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
1414

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"
2025
# connect to wifi
2126
try:
2227
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
@@ -42,13 +47,13 @@
4247
WAXING_GIBBOUS = 3
4348
FULL_MOON = 4
4449
WANING_GIBBOUS = 5
45-
THIRD_QUARTER = 6
50+
LAST_QUARTER = 6
4651
WANING_CRESCENT = 7
4752
DARK_MOON = 8
4853
RED_MOON = 9
4954
# strings that match return from API
5055
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"]
5257

5358
# functions for each moon phase to light up based on neopixel orientation
5459
def set_new_moon():
@@ -87,7 +92,7 @@ def set_waning_gibbous():
8792
pixels[i] = ON
8893
pixels.show()
8994

90-
def set_third_quarter():
95+
def set_last_quarter():
9196
pixels.fill(OFF)
9297
for i in range(0, 24):
9398
pixels[i] = ON
@@ -117,7 +122,7 @@ def set_red_moon():
117122
WAXING_GIBBOUS: set_waxing_gibbous,
118123
FULL_MOON: set_full_moon,
119124
WANING_GIBBOUS: set_waning_gibbous,
120-
THIRD_QUARTER: set_third_quarter,
125+
LAST_QUARTER: set_last_quarter,
121126
WANING_CRESCENT: set_waning_crescent,
122127
DARK_MOON: set_dark_moon,
123128
RED_MOON: set_red_moon
@@ -152,19 +157,26 @@ def set_moon_phase(phase):
152157
while True:
153158
try:
154159
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"
159170
# get the JSON response
160171
response = requests.get(url)
161172
json_response = response.json()
162173
# isolate phase info
174+
current_phase = json_response["properties"]["data"]["curphase"]
163175
print("-" * 40)
164-
print(json_response[0]['Phase'])
176+
print(current_phase)
165177
print("-" * 40)
166178
# run function to update neopixels with current phase
167-
set_moon_phase(json_response[0]['Phase'])
179+
set_moon_phase(current_phase)
168180
response.close()
169181
time.sleep(1)
170182
first_run = False

0 commit comments

Comments
 (0)