-
Notifications
You must be signed in to change notification settings - Fork 1
/
MoonPhase.py
130 lines (107 loc) · 2.76 KB
/
MoonPhase.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Write your code here :-)
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
# Moonphase example utilizing Farmsense API by bk
# brandonklevence@gmail.com
from adafruit_magtag.magtag import MagTag
import adafruit_requests
import secrets
import wifi
import ipaddress
import ssl
import socketpool
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
TIME_URL = "https://io.adafruit.com/api/v2/time/seconds"
response = requests.get(TIME_URL)
unix = str(response.text)
print(unix)
# Set up where we'll be fetching data from
DATA_SOURCE = "https://api.farmsense.net/v1/moonphases/?d="
DATA_SOURCE += unix
DATA_Phase = [0, "Phase"]
DATA_Illumination = [0, "Illumination"]
DATA_Age = [0, "Age"]
def text_transform(value):
return value
magtag = MagTag(url=DATA_SOURCE,
json_path=(DATA_Phase, DATA_Illumination, DATA_Age))
magtag.network.connect()
magtag.add_text(
text_position=(
(magtag.graphics.display.width // 2) - 1,
(magtag.graphics.display.height // 2) + 22,
),
text_scale=1.5,
text_transform=text_transform,
text_anchor_point=(0.5, 0.5),
)
magtag.add_text(
text_position=(
(magtag.graphics.display.width // 2) - 1,
(magtag.graphics.display.height // 2) + 43,
),
text_scale=1.5,
text_transform=text_transform,
text_anchor_point=(0.5, 0.5),
# is_data= False,
)
magtag.add_text(
text_font="/fonts/MoonPhases-75.bdf",
text_position=((magtag.graphics.display.width // 2) - 1, 40),
text_anchor_point=(0.5, 0.5),
)
try:
value = magtag.fetch()
moonChar = [
"0",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"1",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
]
z = value[2] / 29.56
y = int(z * 28)
print(moonChar[y])
magtag.set_text(value[0], index=0)
p = str("Illuminated: " + str(int(value[1]*100)) +"%")
magtag.set_text(p, index=1)
magtag.set_text(moonChar[y], index=2)
print(value)
print("Illumination:", value[1], "%")
except (ValueError, RuntimeError) as e:
print("Some error occured, retrying! -", e)
magtag.exit_and_deep_sleep(60)