Tuesday, 20 May 2014

Pretty Colours via MQTT

What does a geek do when they have some spare RGB LED strip (addressable WS2812B) and some cheap nasty LED devices? LED transplant time...

So, first to go was the LED glass prism stand received as a christmas present - out went the potted pcb with three fading LEDs, and in went a single piece of RGB strip fixed in place with a hot glue gun.
wire comes out the bottom and goes to a nanode.
So far so good, but I don't just want fixed or fading colours so time to revisit an IoT idea: Cheerlights

The cheerlights API defines 10 colors that can be set, but I want the possibility of sending any RGB value, so I created @FakeCheerlights as an MQTT series of topics on the test.mosquitto.org broker

fakecheerlights/rgb
fakecheerlights/colour
fakecheerlights/raw

which contain the hex RGB value, the identified colour name and the raw tweet.

A separate script (running on the NAS) uses the twitter API via tweepy to follow the twitter stream search for 'cheerlights' and 'fakecheerlights' mentioned in a tweet. If a colour name (matched from the X11 rgb.txt) is found then it publishes the corresponding hex value to the broker

Since fakecheerlights uses a publish/subscribe model, it's *much* faster to react than the original cheerlights protocol which relies on a client polling the server API. The downside is there's no nice fade time between colours.

The nanode (I have one of the earlier batches) was designed as a low-cost ethernet enabled arduino, so uses the Microchip ENC28J60 ethernet rather than the wiznet of the arduino shield. Thanks to UIPEthernet.h and PubSubClient.h it's just possible to code in a basic subscriber which sets the strip output to match.

Since I plan to use the pub/sub model at work for monitoring the machine status and batch queues, I gutted an old ikea childs lamp and replaced the LED with another WS2812B and hooked that up to a freetronics etherten with a PoE daughterboard attached. Sadly the hardware revision I had didn't include the MCP 24AA025E48 that my old nanode did, so my sketch had to include a hard-coded MAC address.

A short youtube video demonstrates the reaction time, and all the source code is on github. (with the exception of the nanode sketch as I didn't save it before closing the arduino ide)

No comments:

Word of mouth Skye History

Many years ago we lived in the Old Manse in Waternish, Skye. If you look on the maps, you'll spot that unlike nearly all the other house...