1+ import time
2+ import board
3+ import busio
4+ import digitalio
5+ from adafruit_fona .adafruit_fona import FONA
6+ import adafruit_fona .adafruit_fona_socket as cellular_socket
7+ import adafruit_requests as requests
8+
9+ import neopixel
10+ import adafruit_fancyled .adafruit_fancyled as fancy
11+
12+ # Get GPRS details and more from a secrets.py file
13+ try :
14+ from secrets import secrets
15+ except ImportError :
16+ print ("GPRS secrets are kept in secrets.py, please add them there!" )
17+ raise
18+
19+ # Create a serial connection for the FONA connection using 4800 baud.
20+ # These are the defaults you should use for the FONA Shield.
21+ # For other boards set RX = GPS module TX, and TX = GPS module RX pins.
22+ uart = busio .UART (board .TX , board .RX , baudrate = 4800 )
23+ rst = digitalio .DigitalInOut (board .D4 )
24+
25+ # Initialize FONA module (this may take a few seconds)
26+ print ("Initializing FONA" )
27+ fona = FONA (uart , rst )
28+
29+ # Enable GPS
30+ fona .gps = True
31+
32+ # Bring up cellular connection
33+ fona .configure_gprs ((secrets ["apn" ], secrets ["apn_username" ], secrets ["apn_password" ]))
34+
35+ # Bring up GPRS
36+ fona .gprs = True
37+ print ("FONA initialized" )
38+
39+ # Initialize a requests object with a socket and cellular interface
40+ requests .set_socket (cellular_socket , fona )
41+
42+ DATA_SOURCE = "http://api.thingspeak.com/channels/1417/feeds.json?results=1"
43+ DATA_LOCATION = ["feeds" , 0 , "field2" ]
44+
45+ # neopixels
46+ pixels = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.3 )
47+ pixels .fill (0 )
48+
49+ attempts = 3 # Number of attempts to retry each request
50+ failure_count = 0
51+ response = None
52+
53+ # we'll save the value in question
54+ last_value = value = None
55+
56+ while True :
57+ print ("Fetching json from" , DATA_SOURCE )
58+ response = requests .get (DATA_SOURCE )
59+ print (response .json ())
60+ value = response .json ()
61+ for key in DATA_LOCATION :
62+ value = value [key ]
63+ print (value )
64+ response .close ()
65+
66+ if not value :
67+ continue
68+ if last_value != value :
69+ color = int (value [1 :], 16 )
70+ red = color >> 16 & 0xFF
71+ green = color >> 8 & 0xFF
72+ blue = color & 0xFF
73+ gamma_corrected = fancy .gamma_adjust (fancy .CRGB (red , green , blue )).pack ()
74+
75+ pixels .fill (gamma_corrected )
76+ last_value = value
77+ response = None
78+ time .sleep (60 )
0 commit comments