This is a quick and dirty (there's pretty minimal error handling) tool to make Philips Hue lights quickly flash different colors similar to the Caramelldansen Lights meme.
When I had this idea, I thought, "Oh, this'll be easy to whip up a little script in Python, right?" Ehh, no. My first attempt was to use the standard Hue REST API. The probem with this approach is that it's slow, with a decent amount of latency. Luckily there's the Hue Entertainment API for low-latency, high frequency updates. The details of how it works is pretty interesting, but as I read through the documentation I encountered DTLS. I'd never heard of it before, and thought that it would be pretty easy to use, maybe install a package from PyPI for it...not so much.
The Python standard library ssl
module doesn't implement DTLS. There are a
pair of DTLS libraries on PyPI, but they havent been updated in a while (one
was Python 2 only, the other didn't seem like it supported DTLSv1.2).
Hue Entertainment also uses DTLS with pre-shared keys, not certificates like
most other implementations assume. The Python cryptography
library
exposes some of functions and constants I needed, but not all of them.
From here I jumped into writing a tool in C using OpenSSL. Since OpenSSL is no
longer included with macOS (more accurately, the headers aren't, and Apple
discourges using the included library), I used the version installed by
Homebrew. From there I cobbled together just enough of a program to cycle the
lights through four colors at 165 BPM. Two very helpful pages were Christopher
Wood's DTLS with OpenSSL blog post, and especially
jxck's OpenSSL DTLS API Gist. Digging through the OpenSSL man pages
filled in the rest (SSL_CTX_set_psk_client_callback
took some
especially close reading).
Right now the Makefile assumes you're using macOS and have OpenSSL 1.1.x
installed through Homebrew, but it's pretty easy to change that (literally the
first line). From there a plain make
should build it all for you.
The IDs of the lights to change is also currently hardcoded (remember where I said this was quick and dirty?), so you will have to explore the Hue API and find the appropriate IDs and change the source if you want to use this.
This tool does precisely one thing, which is spam DTLS messages to a Hue bridge
to cycle through four colors at 2.75Hz (165 BPM). It does not register an
application with a Hue bridge, detect which lights to use, or enable the stream
mode through the Hue API. I did all those manually through the Hue CLIP
debugging tool (http://<Hue bridge IP>/debug/clip.html
, and then started
hue-dansen
once stream mode was active.
Once you've registered an application with a client key, and stream mode
is active (it will disable itself after a few second of inactivity, so be
quick!) you can start hue-dansen
like this:
./hue-dansen <hue IP address> <identity string> <PSK>
An optional duration in whole seconds can be added at the end as well. If you don't add one, just interrupt the program to stop the blinkenlights.