forked from adafruit/Adafruit_CircuitPython_ESP32SPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32spi_cheerlights.py
65 lines (54 loc) · 1.83 KB
/
esp32spi_cheerlights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import time
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy
# Get wifi details and more from a secrets.py file
try:
from esp32spi_secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("ESP32 SPI webclient test")
DATA_SOURCE = "https://api.thingspeak.com/channels/1417/feeds.json?results=1"
DATA_LOCATION = ["feeds", 0, "field2"]
esp32_cs = DigitalInOut(board.D9)
esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
# neopixels
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)
pixels.fill(0)
# we'll save the value in question
last_value = value = None
while True:
try:
print("Fetching json from", DATA_SOURCE)
response = wifi.get(DATA_SOURCE)
print(response.json())
value=response.json()
for key in DATA_LOCATION:
value = value[key]
print(value)
response.close()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
continue
if not value:
continue
if last_value != value:
color = int(value[1:],16)
red = color >> 16 & 0xFF
green = color >> 8 & 0xFF
blue = color& 0xFF
gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack()
pixels.fill(gamma_corrected)
last_value = value
response = None
time.sleep(60)