code.py
 1  # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  This example will access the adafruit.io API, grab a number like active users
 7  and io plus subscribers... and display it on a screen
 8  If you can find something that spits out JSON data, we can display it!
 9  """
10  import time
11  import board
12  from adafruit_pyportal import PyPortal
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  # Set up where we'll be fetching data from
22  DATA_SOURCE = "https://io.adafruit.com/api/v2/stats?x-aio-key="+secrets['aio_key']
23  DATA_LOCATION1 = ["io_plus", "io_plus_subscriptions"]
24  DATA_LOCATION2 = ["users", "users_active_30_days"]
25  
26  cwd = ("/"+__file__).rsplit('/', 1)[0]
27  print(cwd)
28  pyportal = PyPortal(url=DATA_SOURCE,
29                      json_path=(DATA_LOCATION1, DATA_LOCATION2),
30                      status_neopixel=board.NEOPIXEL,
31                      default_bg=cwd+"/adafruitio_background.bmp",
32                      text_font=cwd+"/fonts/Collegiate-24.bdf",
33                      text_position=((165, 170), (165, 200)),
34                      text_color=(0x00FF00, 0x0000FF))
35  
36  while True:
37      try:
38          value = pyportal.fetch()
39          print("Response is", value)
40      except RuntimeError as e:
41          print("Some error occured, retrying! -", e)
42  
43      time.sleep(60)