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 Hackaday.io API, grab a number like hackaday skulls,
 7  and display it on a screen
 8  If you can find something that spits out JSON data, we can display it!
 9  Note that you need a hackaday API key to access the API!
10  """
11  import time
12  import board
13  from adafruit_pyportal import PyPortal
14  
15  # Get wifi details and more from a secrets.py file
16  try:
17      from secrets import secrets
18  except ImportError:
19      print("WiFi secrets are kept in secrets.py, please add them there!")
20      raise
21  
22  # Some data sources and JSON locations to try out
23  CAPTION="hackaday.io/project/163309-circuitpython-hackaday"
24  DATA_SOURCE = "https://api.hackaday.io/v1/projects/163309?api_key="+secrets['hackaday_token']
25  DATA_LOCATION = ["skulls"]
26  
27  # the current working directory (where this file is)
28  cwd = ("/"+__file__).rsplit('/', 1)[0]
29  pyportal = PyPortal(url=DATA_SOURCE, json_path=DATA_LOCATION,
30                      status_neopixel=board.NEOPIXEL,
31                      default_bg=cwd+"/had_background.bmp",
32                      text_font=cwd+"/fonts/Checkbook-50.bdf",
33                      text_position=(210, 110),
34                      text_color=0xFFFFFF,
35                      caption_text=CAPTION,
36                      caption_font=cwd+"/fonts/Arial.bdf",
37                      caption_position=(10, 220),
38                      caption_color=0xFFFFFF)
39  
40  # track the last value so we can play a sound when it updates
41  last_value = 0
42  
43  while True:
44      try:
45          value = pyportal.fetch()
46          print("Response is", value)
47          if last_value < value:  # ooh it went up!
48              print("New skull!")
49              pyportal.play_file(cwd+"/coin.wav")
50          last_value = value
51      except RuntimeError as e:
52          print("Some error occured, retrying! -", e)
53      time.sleep(60)