/
neopixel_on_off.py
53 lines (46 loc) · 1.55 KB
/
neopixel_on_off.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
from machine import Pin
from neopixel import NeoPixel
from time import sleep
import network
import urequests as requests
import ujson
# Wifi credentials
wifi_ssid = "YOUR_WIFI_SSID"
wifi_password = "YOUR_WIFI_PASSWORD"
# Adafruit IO authentication
aio_key = "YOUR_ADAFRUIT_IO_KEY"
username = "YOUR_ADAFRUIT_USERNAME"
headers = {'X-AIO-Key': aio_key, 'Content-Type': 'application/json'}
# Don't forget the NeoPixels!
np = NeoPixel(Pin(15), 12)
feed_names = ['red', 'green', 'blue']
rgb_values = {'red': 0, 'green': 0, 'blue': 0}
# Connect to Wifi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(wifi_ssid, wifi_password)
while not sta_if.isconnected():
print(".", end = "")
def create_URL(feedname):
url = "https://io.adafruit.com/api/v2/" + username + "/feeds/" + feedname + "/data/last"
return url
try:
while True:
on_or_off = ujson.loads(requests.get(create_URL('on'), headers=headers).text)['value']
if on_or_off == 'ON':
for color in feed_names:
response = requests.get(create_URL(color), headers=headers)
parsed = ujson.loads(response.text)
value = int(parsed['value'])
rgb_values[color] = value
for i in range(12):
np[i] = (rgb_values['red'], rgb_values['green'], rgb_values['blue'])
np.write()
else:
for i in range(12):
np[i] = (0, 0, 0)
np.write()
except KeyboardInterrupt:
for i in range(12):
np[i] = (0, 0, 0)
np.write()