-
Notifications
You must be signed in to change notification settings - Fork 2
/
code.py
61 lines (53 loc) · 2.09 KB
/
code.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
"""
This example queries the DarkSky API to find out the current
weather for your location... and display it on a screen!
"""
import sys
import time
import board
from adafruit_pyportal import PyPortal
import darksky # pylint: disable=wrong-import-position
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
sys.path.append(cwd)
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set up where we'll be fetching data from
DATA_SOURCE = "https://api.darksky.net/forecast/"+secrets['DARKSKY_KEY']
DATA_SOURCE += "/"+secrets['GPS_COORDS']
DATA_SOURCE += "?units=auto&exclude=hourly,minutely,flags,daily,alerts"
DATA_LOCATION = []
# Initialize the pyportal object and let us know what data to fetch and where
# to display it
pyportal = PyPortal(url=DATA_SOURCE,
json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL,
default_bg=0x000000)
gfx = darksky.darksky(pyportal.splash, am_pm=True, celsius=False)
localtile_refresh = None
weather_refresh = None
while True:
# only query the online time once per hour (and on first run)
if (not localtile_refresh) or (time.monotonic() - localtile_refresh) > 3600:
try:
print("Getting time from internet!")
pyportal.get_local_time()
localtile_refresh = time.monotonic()
except RuntimeError as e:
print("Some error occured, retrying! -", e)
continue
# only query the weather every 10 minutes (and on first run)
if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
try:
value = pyportal.fetch()
print("Response is", value)
gfx.display_weather(value)
weather_refresh = time.monotonic()
except RuntimeError as e:
print("Some error occured, retrying! -", e)
continue
gfx.update_time()
time.sleep(60) # wait 60 seconds before updating anything again