1
+ import time
2
+ import board
3
+ import busio
4
+ from digitalio import DigitalInOut
5
+
6
+ from adafruit_wiznet5k .adafruit_wiznet5k import WIZNET5K
7
+ import adafruit_wiznet5k .adafruit_wiznet5k_socket as socket
8
+ import adafruit_requests as requests
9
+
10
+ import neopixel
11
+ import adafruit_fancyled .adafruit_fancyled as fancy
12
+
13
+ cs = DigitalInOut (board .D10 )
14
+ spi_bus = busio .SPI (board .SCK , MOSI = board .MOSI , MISO = board .MISO )
15
+
16
+ # Initialize ethernet interface with DHCP
17
+ eth = WIZNET5K (spi_bus , cs )
18
+
19
+ # Initialize a requests object with a socket and ethernet interface
20
+ requests .set_socket (socket , eth )
21
+
22
+ DATA_SOURCE = "http://api.thingspeak.com/channels/1417/feeds.json?results=1"
23
+ DATA_LOCATION = ["feeds" , 0 , "field2" ]
24
+
25
+ # neopixels
26
+ pixels = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.3 )
27
+ pixels .fill (0 )
28
+
29
+ attempts = 3 # Number of attempts to retry each request
30
+ failure_count = 0
31
+ response = None
32
+
33
+ # we'll save the value in question
34
+ last_value = value = None
35
+
36
+ while True :
37
+ try :
38
+ print ("Fetching json from" , DATA_SOURCE )
39
+ response = requests .get (DATA_SOURCE )
40
+ print (response .json ())
41
+ value = response .json ()
42
+ for key in DATA_LOCATION :
43
+ value = value [key ]
44
+ print (value )
45
+ response .close ()
46
+ failure_count = 0
47
+ except AssertionError as error :
48
+ print ("Request failed, retrying...\n " , error )
49
+ failure_count += 1
50
+ if failure_count >= attempts :
51
+ raise AssertionError ("Failed to resolve hostname, \
52
+ please check your router's DNS configuration." )
53
+ continue
54
+ if not value :
55
+ continue
56
+ if last_value != value :
57
+ color = int (value [1 :], 16 )
58
+ red = color >> 16 & 0xFF
59
+ green = color >> 8 & 0xFF
60
+ blue = color & 0xFF
61
+ gamma_corrected = fancy .gamma_adjust (fancy .CRGB (red , green , blue )).pack ()
62
+
63
+ pixels .fill (gamma_corrected )
64
+ last_value = value
65
+ response = None
66
+ time .sleep (60 )
0 commit comments