|
| 1 | +""" |
| 2 | +PyPortal UV Index display |
| 3 | +
|
| 4 | +Adafruit invests time and resources providing this open source code. |
| 5 | +Please support Adafruit and open source hardware by purchasing |
| 6 | +products from Adafruit! |
| 7 | +
|
| 8 | +Written by Dave Astels for Adafruit Industries |
| 9 | +Copyright (c) 2019 Adafruit Industries |
| 10 | +Licensed under the MIT license. |
| 11 | +
|
| 12 | +All text above must be included in any redistribution. |
| 13 | +""" |
| 14 | + |
| 15 | +import time |
| 16 | +import json |
| 17 | +import board |
| 18 | +import displayio |
| 19 | +from adafruit_pyportal import PyPortal |
| 20 | +from adafruit_display_shapes.rect import Rect |
| 21 | +from adafruit_display_text.Label import Label |
| 22 | +from adafruit_bitmap_font import bitmap_font |
| 23 | + |
| 24 | +try: |
| 25 | + from secrets import secrets |
| 26 | +except ImportError: |
| 27 | + print("""WiFi settings are kept in secrets.py, please add them there! |
| 28 | +the secrets dictionary must contain 'ssid' and 'password' at a minimum""") |
| 29 | + raise |
| 30 | + |
| 31 | +MAX_BAR_HEIGHT = 160 |
| 32 | +MARGIN = 10 |
| 33 | +SPACE_BETWEEN_BARS = 1 |
| 34 | + |
| 35 | +COLORS = [0x00FF00, 0x83C602, 0xa2CF02, |
| 36 | + 0xF7DE03, 0xF6B502, 0xF78802, |
| 37 | + 0xF65201, 0xEA2709, |
| 38 | + 0xDA0115, 0xFC019E, 0xB548FF, |
| 39 | + 0x988FFE, 0x7EA7FE, 0x66BFFD, 0x4BD9FF] |
| 40 | + |
| 41 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 42 | + |
| 43 | +CAPTION_FONT_FILE = cwd+'/fonts/Helvetica-Bold-16.bdf' |
| 44 | +BAR_FONT_FILE = cwd+'/fonts/Arial-Bold-12.bdf' |
| 45 | + |
| 46 | +#pylint:disable=line-too-long |
| 47 | +url = 'https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{0}/JSON'.format(secrets['zip']) |
| 48 | +#pylint:enable=line-too-long |
| 49 | + |
| 50 | +def extract_hour(date_time): |
| 51 | + """Extract the hour in a format to use for display: |
| 52 | + :param date_time: the timestamp from EPA UV readings |
| 53 | + """ |
| 54 | + split_date_time = date_time.split() |
| 55 | + hour = split_date_time[1] |
| 56 | + suffix = split_date_time[2] |
| 57 | + if hour[0] == '0': |
| 58 | + hour = hour[1] |
| 59 | + return '\n'.join([hour, suffix]) |
| 60 | + |
| 61 | +def extract_date(date_time): |
| 62 | + """Extract the date in a format to use for display: |
| 63 | + :param date_time: the timestamp from EPA UV readings |
| 64 | + """ |
| 65 | + return ' '.join(date_time.split('/')[0:2]) |
| 66 | + |
| 67 | +pyportal = PyPortal(url=url, |
| 68 | + status_neopixel=board.NEOPIXEL, |
| 69 | + default_bg=0xFFFFFF, |
| 70 | + caption_font=CAPTION_FONT_FILE) |
| 71 | + |
| 72 | +canvas = displayio.Group(max_size=36) |
| 73 | +pyportal.splash.append(canvas) |
| 74 | +bar_font = bitmap_font.load_font(BAR_FONT_FILE) |
| 75 | + |
| 76 | +while True: |
| 77 | + json_payload = '' |
| 78 | + try: |
| 79 | + json_payload = pyportal.fetch() |
| 80 | + raw_data = json.loads(json_payload) |
| 81 | + except (ValueError, RuntimeError) as ex: |
| 82 | + print('Error: ', ex) |
| 83 | + if isinstance(ex, ValueError): |
| 84 | + print('JSON:', json_payload) |
| 85 | + print('Retrying in 10 minutes') |
| 86 | + time.sleep(600) |
| 87 | + continue |
| 88 | + data = [] |
| 89 | + for d in raw_data: |
| 90 | + if d['UV_VALUE'] > 0: |
| 91 | + entry = {} |
| 92 | + entry['hour'] = extract_hour(d['DATE_TIME']) |
| 93 | + entry['value'] = int(d['UV_VALUE']) |
| 94 | + data.append(entry) |
| 95 | + the_day = raw_data[0]['DATE_TIME'] |
| 96 | + pyportal.set_caption('UV Index for {0}'.format(extract_date(the_day)), |
| 97 | + (80, 20), |
| 98 | + 0x000000) |
| 99 | + number_of_readings = len(data) |
| 100 | + whitespace = (number_of_readings - 1) * SPACE_BETWEEN_BARS + 2 * MARGIN |
| 101 | + bar_width = (320 - whitespace) // number_of_readings |
| 102 | + max_reading = max([d['value'] for d in data]) |
| 103 | + |
| 104 | + while len(canvas) > 0: |
| 105 | + canvas.pop() |
| 106 | + |
| 107 | + for i, reading in enumerate(data): |
| 108 | + bar_height = (MAX_BAR_HEIGHT * reading['value']) // max_reading |
| 109 | + x = int(MARGIN + i * (bar_width + SPACE_BETWEEN_BARS)) |
| 110 | + canvas.append(Rect(x, 200 - bar_height, |
| 111 | + bar_width, bar_height, |
| 112 | + fill=COLORS[reading['value']])) |
| 113 | + canvas.append(Label(bar_font, |
| 114 | + x=x+3, y=220, |
| 115 | + text=reading['hour'], |
| 116 | + color=0x000000, |
| 117 | + line_spacing=0.6)) |
| 118 | + canvas.append(Label(bar_font, |
| 119 | + x=x+(bar_width//2)-4, y=208-bar_height, |
| 120 | + text=str(reading['value']), |
| 121 | + color=0x000000)) |
| 122 | + |
| 123 | + time.sleep(3600) #refresh hourly |
0 commit comments