
When we first launched the Light on Kickstarter, we received mail from some folks who had what looked to be an interesting project – Cheerlights. Basically it uses Twitter and some other server software to provide a universal ‘color’ for Internet-connected Christmas lights. You can set a color on Cheerlights by posting a tweet with the hashtag #cheerlights somewhere in it, followed by a color name (white, warmwhite, red, orange, yellow, green, cyan, blue, purple, magenta).
The Cheerlights server listens for that sequence, then publishes the current color value in a JSON object accessible here. Devices can poll that object to learn the current Cheerlights color, and respond accordingly.
It’s all quite straightforward, and it was sitting on my to-do list until this week, when I started to give a real though to Christmas lights. We’ll be doing a special holiday video and livestream a ‘change the color Light under the Christmas tree’ for folks to have a play, but on the way to that, I decided to have a look at Cheerlights, saw that it was going to be almost trivially easy to implement in our API, then wrote the whole thing in about 30 minutes - in bash.
That’s right, we’ve implemented Cheerlights as a shell script. This means it’s quite fast – Python doesn’t need to load – and also very portable. It should run in some easily modified form on any *NIX box in the universe.
It does rely on two programs: cURL, which is considered part of the base package in most LINUX distributions, and jq, which is a command-line JSON parser in ‘C’, very fast, very flexible, and very powerful. As we do manipulate a lot of JSON data on the Light, we’ve compiled this package for the Light, and will ship it as part of the Light’s system software.
Here, in full, is the shell script:
#!/bin/bash # # Set the Light to the current CheerLights color # This is all done in bash. Because we can? # # Copyright (c) 2012, Mark D. Pesce # Released under the MIT License. # # First grab the current CheerLights JSON thingy, then parse it to extract field1 # Which happens to be the current color value # color=`curl -s http://api.thingspeak.com/channels/1417/field/1/last.json | jq .field1` echo $color # Now parse the color value which should be one of these fine colors maybe # case $color in \"red\") echo "We got red"; export QUERY_STRING="color=0x80FF80"; /srv/http/cgi-bin/ajaxcolor ;; \"cyan\") echo "We got cyan"; export QUERY_STRING="color=0xFF80FF"; /srv/http/cgi-bin/ajaxcolor ;; \"warmwhite\") echo "We got warm white"; export QUERY_STRING="color=0xFFFFbF"; /srv/http/cgi-bin/ajaxcolor ;; \"white\") echo "We got white"; export QUERY_STRING="color=0xFFFFFF"; /srv/http/cgi-bin/ajaxcolor ;; \"blue\") echo "We got blue"; export QUERY_STRING="color=0x8080FF"; /srv/http/cgi-bin/ajaxcolor ;; \"green\") echo "We got green"; export QUERY_STRING="color=0xFF8080"; /srv/http/cgi-bin/ajaxcolor ;; \"yellow\") echo "We got yellow"; export QUERY_STRING="color=0xFFFF80"; /srv/http/cgi-bin/ajaxcolor ;; \"magenta\") echo "We got magenta"; export QUERY_STRING="color=0x80bfFF"; /srv/http/cgi-bin/ajaxcolor ;; \"purple\") echo "We got purple"; export QUERY_STRING="color=0x80FFFF"; /srv/http/cgi-bin/ajaxcolor ;; \"orange\") echo "We got orange"; export QUERY_STRING="color=0x90FF80"; /srv/http/cgi-bin/ajaxcolor ;; *) echo "We got no match at all" ;; esac
The script needs to be run for every Cheerlights update, which could be done as a cron task, or it could be part of a background daemon.