Skip to content

Instantly share code, notes, and snippets.

@bassdread
Last active December 31, 2015 19:09
Show Gist options
  • Save bassdread/8032083 to your computer and use it in GitHub Desktop.
Save bassdread/8032083 to your computer and use it in GitHub Desktop.
Code for make blink sticks talk to cheer lights. More info at http://www.cheerlights.com/subscribe-to-cheerlights-feed and blinkstick.com
#!/usr/bin/python
"""
Download this script and run it as
```
python thenameofthefile.py
```
or
```
chmod 755 thenameofthefile.py
```
and run it
```
./thenameofthefile.py
```
It will run forever checking and changing the colour every 30 seconds.
"""
import requests, json, time
# https://github.com/arvydas/blinkstick-python/wiki
from blinkstick import blinkstick
# url to fetch from
cheer_lights_url = "http://api.thingspeak.com/channels/1417/field/1/last.json"
def fetch_current_colour():
try:
# grab the colour
r = requests.get(cheer_lights_url)
# anything else than 200 from the HTTP request means it failed
if (r.status_code != 200):
print "Unable to fetch current colour, quitting"
exit(1)
# reply from cheer lights looks like
# {
# created_at: "2013-12-22T15:37:40Z",
# entry_id: 10323,
# field1: "purple"
# }
raw_json = json.loads(r.content)
# return the colour from the loaded json
return raw_json.get('field1')
except Exception as e:
print "Unable to fetch current colour, quitting because %s" % (e)
def light_up():
# get the colour
colour = fetch_current_colour()
print "setting to %s" % (colour)
# cycle over all the blinksticks setting the colour
for bstick in blinkstick.find_all():
bstick.set_color(name=colour)
if __name__ == '__main__':
# loop forever
while (True):
# set the colour
light_up()
# wait for a bit
time.sleep(30)
@Ciemon
Copy link

Ciemon commented Dec 24, 2013

At line 25 there is a call to import requests. Requests "is an elegant and simple HTTP library for Python" and can be installed with:

$ pip install requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment