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 coindesk API, grab a number like bitcoin value in 7 USD 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 # You can display in 'GBP', 'EUR' or 'USD' 15 CURRENCY = 'USD' 16 # Set up where we'll be fetching data from 17 DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json" 18 DATA_LOCATION = ['bpi', CURRENCY, 'rate_float'] 19 20 def text_transform(val): 21 if CURRENCY == 'USD': 22 return "$%d" % val 23 if CURRENCY == 'EUR': 24 return "€%d" % val 25 if CURRENCY == 'GBP': 26 return "£%d" % val 27 return "%d" % val 28 29 # the current working directory (where this file is) 30 cwd = ("/"+__file__).rsplit('/', 1)[0] 31 pyportal = PyPortal(url=DATA_SOURCE, json_path=DATA_LOCATION, 32 status_neopixel=board.NEOPIXEL, 33 default_bg=cwd+"/bitcoin_background.bmp", 34 text_font=cwd+"/fonts/Arial-Bold-24-Complete.bdf", 35 text_position=(195, 130), 36 text_color=0x0, 37 text_transform=text_transform) 38 pyportal.preload_font(b'$012345789') # preload numbers 39 pyportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol 40 41 while True: 42 try: 43 value = pyportal.fetch() 44 print("Response is", value) 45 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 46 print("Some error occured, retrying! -", e) 47 48 time.sleep(3*60) # wait 3 minutes