code.py
 1  # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  PyPortal Adafruit IO Feed Display
 7  
 8  Displays an Adafruit IO Feed on a PyPortal.
 9  """
10  import time
11  import board
12  from adafruit_pyportal import PyPortal
13  
14  # Get Adafruit IO details and more from a secrets.py file
15  try:
16      from secrets import secrets
17  except ImportError:
18      print("Adafruit IO secrets are kept in secrets.py, please add them there!")
19      raise
20  
21  # Adafruit IO Account
22  IO_USER = secrets['aio_username']
23  IO_KEY = secrets['aio_key']
24  # Adafruit IO Feed
25  IO_FEED = 'zapemail'
26  
27  DATA_SOURCE = "https://io.adafruit.com/api/v2/{0}/feeds/{1}?X-AIO-Key={2}".format(IO_USER,
28                                                                                    IO_FEED, IO_KEY)
29  FEED_VALUE_LOCATION = ['last_value']
30  
31  cwd = ("/"+__file__).rsplit('/', 1)[0]
32  pyportal = PyPortal(url=DATA_SOURCE,
33                      json_path=FEED_VALUE_LOCATION,
34                      status_neopixel=board.NEOPIXEL,
35                      default_bg=cwd+"/pyportal_email.bmp",
36                      text_font=cwd+"/fonts/Helvetica-Oblique-17.bdf",
37                      text_position=(30, 65),
38                      text_color=0xFFFFFF,
39                      text_wrap=35, # wrap feed after 35 chars
40                      text_maxlen=160)
41  
42  # speed up projects with lots of text by preloading the font!
43  pyportal.preload_font()
44  
45  while True:
46      try:
47          print('Fetching Adafruit IO Feed Value..')
48          value = pyportal.fetch()
49          print("Response is", value)
50      except RuntimeError as e:
51          print("Some error occured, retrying! -", e)
52      time.sleep(10)