|
| 1 | +# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import board |
| 8 | +from analogio import AnalogIn |
| 9 | +from adafruit_pyportal import PyPortal |
| 10 | +cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is) |
| 11 | +sys.path.append(cwd) |
| 12 | +import openweather_graphics # pylint: disable=wrong-import-position |
| 13 | + |
| 14 | +# Get wifi details and more from a secrets.py file |
| 15 | +try: |
| 16 | + from secrets import secrets |
| 17 | +except ImportError: |
| 18 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 19 | + raise |
| 20 | + |
| 21 | +# Use cityname, country code where countrycode is ISO3166 format. |
| 22 | +# E.g. "New York, US" or "London, GB" |
| 23 | +LOCATION = "New York, US" |
| 24 | + |
| 25 | +# Set up where we'll be fetching data from |
| 26 | +DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION |
| 27 | +DATA_SOURCE += "&appid="+secrets['openweather_token'] |
| 28 | +# You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22' |
| 29 | +DATA_LOCATION = [] |
| 30 | + |
| 31 | + |
| 32 | +# Initialize the pyportal object and let us know what data to fetch and where |
| 33 | +# to display it |
| 34 | +pyportal = PyPortal(url=DATA_SOURCE, |
| 35 | + json_path=DATA_LOCATION, |
| 36 | + status_neopixel=board.NEOPIXEL, |
| 37 | + default_bg=0x000000) |
| 38 | + |
| 39 | +display = board.DISPLAY |
| 40 | +# rotate display for portrait orientation |
| 41 | +display.rotation = 270 |
| 42 | + |
| 43 | +# instantiate the openweather_graphics class |
| 44 | +gfx = openweather_graphics.OpenWeather_Graphics(pyportal.splash, am_pm=True, celsius=False) |
| 45 | + |
| 46 | +# time keeping for refreshing screen icons and weather information |
| 47 | +localtile_refresh = None |
| 48 | +weather_refresh = None |
| 49 | + |
| 50 | +# setup light sensor as an analog input |
| 51 | +analogin = AnalogIn(board.LIGHT) |
| 52 | + |
| 53 | +# analog scaling helper |
| 54 | +def getVoltage(pin): |
| 55 | + return (pin.value * 3.3) / 65536 |
| 56 | + |
| 57 | +# timer for updating onscreen clock |
| 58 | +clock = time.monotonic() |
| 59 | +# timer for keeping backlight on |
| 60 | +light = time.monotonic() |
| 61 | +# light sensor threshold value |
| 62 | +threshold = 0.085 |
| 63 | + |
| 64 | +while True: |
| 65 | + # only query the online time once per hour (and on first run) |
| 66 | + if (not localtile_refresh) or (time.monotonic() - localtile_refresh) > 3600: |
| 67 | + try: |
| 68 | + print("Getting time from internet!") |
| 69 | + pyportal.get_local_time() |
| 70 | + localtile_refresh = time.monotonic() |
| 71 | + except RuntimeError as e: |
| 72 | + print("Some error occured, retrying! -", e) |
| 73 | + continue |
| 74 | + # only query the weather every 10 minutes (and on first run) |
| 75 | + if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600: |
| 76 | + try: |
| 77 | + value = pyportal.fetch() |
| 78 | + print("Response is", value) |
| 79 | + gfx.display_weather(value) |
| 80 | + weather_refresh = time.monotonic() |
| 81 | + except RuntimeError as e: |
| 82 | + print("Some error occured, retrying! -", e) |
| 83 | + continue |
| 84 | + # check light sensor |
| 85 | + if getVoltage(analogin) < threshold: |
| 86 | + # if its blocked then turn on backlight |
| 87 | + pyportal.set_backlight(1) |
| 88 | + # reset light timer |
| 89 | + light = time.monotonic() |
| 90 | + # after 10 seconds, turn off the backlight |
| 91 | + if (time.monotonic() - light) > 10: |
| 92 | + pyportal.set_backlight(0) |
| 93 | + # every 30 seconds update time on screen |
| 94 | + if (time.monotonic() - clock) > 30: |
| 95 | + gfx.update_time() |
| 96 | + clock = time.monotonic() |
0 commit comments