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  NUM_THINGS=25  # how many things to select from (we randomize between em)
19  DATA_SOURCE = "https://api.thingiverse.com/users/adafruit/things?per_page="+str(NUM_THINGS)
20  DATA_SOURCE += "&access_token=" + secrets['thingiverse_token']
21  IMAGE_LOCATION = [0, "thumbnail"]
22  TITLE_LOCATION = [0, "name"]
23  URL_LOCATION = [0, "public_url"]
24  
25  # determine the current working directory needed so we know where to find files
26  cwd = ("/"+__file__).rsplit('/', 1)[0]
27  pyportal = adafruit_pyportal.PyPortal(url=DATA_SOURCE,
28                                        json_path=(TITLE_LOCATION, URL_LOCATION, IMAGE_LOCATION),
29                                        status_neopixel=board.NEOPIXEL,
30                                        default_bg=cwd+"/thingiverse_background.bmp",
31                                        text_font=cwd+"/fonts/Arial-12.bdf",
32                                        text_position=((5, 10), (5, 230)),
33                                        text_color=(0x00FF00, 0x00FF00),
34                                        text_transform=(None, None))
35  pyportal.preload_font()
36  
37  while True:
38      response = None
39      try:
40          response = pyportal.fetch()
41          print("Response is", response)
42          pyportal.set_background(None)
43          image_url = response[2].replace('_thumb_medium.', '_display_large.')
44          pyportal.wget(pyportal.image_converter_url(image_url,320, 240,color_depth=16),
45                        "/cache.bmp",
46                        chunk_size=512)
47          pyportal.set_background("/cache.bmp")
48  
49      except (IndexError, RuntimeError, ValueError) as e:
50          print("Some error occured, retrying! -", e)
51  
52      # next thingy should be random!
53      thingy = random.randint(0, NUM_THINGS - 1)
54      URL_LOCATION[0] = TITLE_LOCATION[0] = IMAGE_LOCATION[0] = thingy
55  
56      time.sleep(60 * 3)  # cycle every 3 minutes