Skip to content

Add Cheerlights Example! #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions examples/adafruit_wiznet5k_cheerlights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import time
import board
import busio
from digitalio import DigitalInOut

from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_requests as requests

import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy

cs = DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs)

# Initialize a requests object with a socket and ethernet interface
requests.set_socket(socket, eth)

DATA_SOURCE = "http://api.thingspeak.com/channels/1417/feeds.json?results=1"
DATA_LOCATION = ["feeds", 0, "field2"]

# neopixels
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.3)
pixels.fill(0)

attempts = 3 # Number of attempts to retry each request
failure_count = 0
response = None

# we'll save the value in question
last_value = value = None

while True:
try:
print("Fetching json from", DATA_SOURCE)
response = requests.get(DATA_SOURCE)
print(response.json())
value = response.json()
for key in DATA_LOCATION:
value = value[key]
print(value)
response.close()
failure_count = 0
except AssertionError as error:
print("Request failed, retrying...\n", error)
failure_count += 1
if failure_count >= attempts:
raise AssertionError("Failed to resolve hostname, \
please check your router's DNS configuration.")
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)