esp32spi_cheerlights.py
1 # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 4 import time 5 import board 6 import busio 7 from digitalio import DigitalInOut 8 9 from adafruit_esp32spi import adafruit_esp32spi 10 from adafruit_esp32spi import adafruit_esp32spi_wifimanager 11 12 import neopixel 13 import adafruit_fancyled.adafruit_fancyled as fancy 14 15 # Get wifi details and more from a secrets.py file 16 try: 17 from secrets import secrets 18 except ImportError: 19 print("WiFi secrets are kept in secrets.py, please add them there!") 20 raise 21 22 print("ESP32 SPI webclient test") 23 24 DATA_SOURCE = "https://api.thingspeak.com/channels/1417/feeds.json?results=1" 25 DATA_LOCATION = ["feeds", 0, "field2"] 26 27 esp32_cs = DigitalInOut(board.D9) 28 esp32_ready = DigitalInOut(board.D10) 29 esp32_reset = DigitalInOut(board.D5) 30 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 31 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) 32 """Use below for Most Boards""" 33 status_light = neopixel.NeoPixel( 34 board.NEOPIXEL, 1, brightness=0.2 35 ) # Uncomment for Most Boards 36 """Uncomment below for ItsyBitsy M4""" 37 # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) 38 wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) 39 40 # neopixels 41 pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3) 42 pixels.fill(0) 43 44 # we'll save the value in question 45 last_value = value = None 46 47 while True: 48 try: 49 print("Fetching json from", DATA_SOURCE) 50 response = wifi.get(DATA_SOURCE) 51 print(response.json()) 52 value = response.json() 53 for key in DATA_LOCATION: 54 value = value[key] 55 print(value) 56 response.close() 57 except (ValueError, RuntimeError) as e: 58 print("Failed to get data, retrying\n", e) 59 wifi.reset() 60 continue 61 62 if not value: 63 continue 64 if last_value != value: 65 color = int(value[1:], 16) 66 red = color >> 16 & 0xFF 67 green = color >> 8 & 0xFF 68 blue = color & 0xFF 69 gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack() 70 71 pixels.fill(gamma_corrected) 72 last_value = value 73 response = None 74 time.sleep(60)