/ MagTag_Cheerlights / code.py
code.py
1 # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 # 3 # SPDX-License-Identifier: Unlicense 4 import time 5 import board 6 from adafruit_magtag.magtag import MagTag 7 import neopixel 8 9 external_pixels = neopixel.NeoPixel(board.D10, 30, brightness=0.3) 10 11 # Set up where we'll be fetching data from 12 DATA_SOURCE = "http://api.thingspeak.com/channels/1417/field/2/last.json" 13 COLOR_LOCATION = ['field2'] 14 DATE_LOCATION = ['created_at'] 15 16 magtag = MagTag( 17 url=DATA_SOURCE, 18 json_path=(COLOR_LOCATION, DATE_LOCATION), 19 ) 20 magtag.network.connect() 21 22 # Color 23 magtag.add_text( 24 text_font="Arial-Bold-12.bdf", 25 text_position=(10, 15), 26 text_transform=lambda x: "New Color {}".format(x), 27 ) 28 # datestamp 29 magtag.add_text( 30 text_font="Arial-Bold-12.bdf", 31 text_position=(10, 35), 32 text_transform=lambda x: "Updated on: {}".format(x), 33 ) 34 35 timestamp = None 36 while True: 37 if not timestamp or ((time.monotonic() - timestamp) > 10): # once every 10 seconds... 38 try: 39 value = magtag.fetch() 40 print("Response is", value) 41 magtag.peripherals.neopixel_disable = False 42 color = int(value[0][1:], 16) 43 magtag.peripherals.neopixels.fill(color) 44 external_pixels.fill(color) 45 timestamp = time.monotonic() 46 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 47 print("Some error occured, retrying! -", e) 48 time.sleep(1)