|
| 1 | +""" |
| 2 | +Dashblock API Adafruit Learn Guide Count demo |
| 3 | +Use Dashblock to create a custom API for learn.adafruit.com, |
| 4 | +then display the number of learn guides on the site |
| 5 | +""" |
| 6 | + |
| 7 | +import time |
| 8 | +import board |
| 9 | +from adafruit_pyportal import PyPortal |
| 10 | + |
| 11 | +# Get wifi details and more from a secrets.py file |
| 12 | +try: |
| 13 | + from secrets import secrets |
| 14 | +except ImportError: |
| 15 | + print("WiFi settings are kept in settings.py, please add them there!") |
| 16 | + raise |
| 17 | + |
| 18 | +# Set up where we'll be fetching data from |
| 19 | +DATA_SOURCE = "https://api.dashblock.io/model/v1?api_key=" + secrets['dashblock_key'] |
| 20 | +GUIDE_COUNT = ['entities', 0, 'guide count'] |
| 21 | +CAPTION = 'total tutorials:' |
| 22 | + |
| 23 | +# determine the current working directory |
| 24 | +# needed so we know where to find files |
| 25 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 26 | + |
| 27 | +# Initialize the pyportal object and let us know what data to fetch and where |
| 28 | +# to display it |
| 29 | +pyportal = PyPortal(url=DATA_SOURCE, |
| 30 | + json_path = (GUIDE_COUNT), |
| 31 | + status_neopixel=board.NEOPIXEL, |
| 32 | + default_bg=cwd+"/adabot_cover.bmp", |
| 33 | + text_font=cwd+"/fonts/Collegiate-50.bdf", |
| 34 | + text_position=((40, 100)), |
| 35 | + text_color=(0x8080FF), |
| 36 | + text_maxlen=(4), # max text length, only want first 4 chars for number of guides |
| 37 | + caption_text=CAPTION, |
| 38 | + caption_font=cwd+"/fonts/Collegiate-24.bdf", |
| 39 | + caption_position=(40, 60), |
| 40 | + caption_color=0xFFFFFF) |
| 41 | + |
| 42 | +# track the last value so we can play a sound when it updates |
| 43 | +last_value = 0 |
| 44 | + |
| 45 | +while True: |
| 46 | + try: |
| 47 | + value = pyportal.fetch() |
| 48 | + print("Response is", value) |
| 49 | + int_value = int(value[:4]) # save only first 4 chars and cast to int |
| 50 | + if last_value < int_value: # ooh it went up! |
| 51 | + print("New guide!") |
| 52 | + pyportal.play_file(cwd+"/coin.wav") # make a noise! |
| 53 | + last_value = int_value |
| 54 | + except RuntimeError as e: |
| 55 | + print("Some error occured, retrying! -", e) |
| 56 | + except ValueError as e: |
| 57 | + print("Value error occured, retrying! -", e) |
| 58 | + continue |
| 59 | + |
| 60 | + time.sleep(600) #update every 10 mins |
0 commit comments