code.py
 1  # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import random
 7  import board
 8  import adafruit_pyportal
 9  
10  # Get wifi details and more from a settings.py file
11  try:
12      from secrets import secrets
13  except ImportError:
14      print("WiFi secrets are kept in secrets.py, please add them there!")
15      raise
16  
17  # Set up where we'll be fetching data from
18  DATA_SOURCE = "https://api.hackster.io/v2/projects?"
19  DATA_SOURCE += "client_id="+secrets['hackster_clientid']
20  DATA_SOURCE += "&client_secret="+secrets['hackster_secret']
21  IMAGE_LOCATION = ['records', 0, "cover_image_url"]
22  TITLE_LOCATION = ['records',0, "name"]
23  HID_LOCATION = ['records', 0, "hid"]
24  NUM_PROJECTS = 24
25  
26  # determine the current working directory needed so we know where to find files
27  cwd = ("/"+__file__).rsplit('/', 1)[0]
28  pyportal = adafruit_pyportal.PyPortal(url=DATA_SOURCE,
29                                        json_path=(TITLE_LOCATION, HID_LOCATION),
30                                        image_json_path=IMAGE_LOCATION,
31                                        image_position=(0, 0),
32                                        image_resize=(320, 240),
33                                        status_neopixel=board.NEOPIXEL,
34                                        default_bg=cwd+"/hackster_background.bmp",
35                                        text_font=cwd+"/fonts/Arial-Bold-12.bdf",
36                                        text_position=((5, 5), (5, 200)),
37                                        text_color=(0xFF0000, 0xFF0000),
38                                        text_wrap=(40, 40))
39  pyportal.preload_font()
40  
41  while True:
42      response = None
43      try:
44          response = pyportal.fetch()
45          print("Response is", response)
46          pyportal.set_text("http://hackster.com/project/"+response[1], 1)
47      except (IndexError, RuntimeError, ValueError) as e:
48          print("Some error occured, retrying! -", e)
49  
50      # next thingy should be random!
51      thingy = random.randint(0, NUM_PROJECTS-1)
52      HID_LOCATION[1] = TITLE_LOCATION[1] = IMAGE_LOCATION[1] = thingy
53  
54      time.sleep(60 * 3)  # cycle every 3 minutes