Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Monday, 13 March 2017

Cheerlights meet the Hue

In the pervious post we hooked cheerlights up to first Flotilla, and then NeoPixels on an Arduino using MQTT. However for some reason we didn't implement the most obvious thing - Cheerlights controlling Philips Hue bulbs! It turns out this is really simple...

make server cheerlight device

make brightness number
make hue number
make saturation number

make targetHue number
make targetSaturation number
make targetBrightness number

when start
..tell server to "update"
..set targetHue to hue
..set targetSaturation to saturation
..set targetBrightness to brightness
..wait 30 secs

The cheerlights bit is pretty simple, and standard... I just read the colour from the cheer light server, and store it in the "target" variables. We need to be a bit careful, as at any time, the value displayed on the actual lights might be different from the desired colour, and if we want to cross fade gently, then we need to record the target colour and the current colour separately.

make bridge hueBridge device
make bridgeAddress string ""
make authorisation string "87XNzAMNY23423423408O61sF66FyqvHFLq0j"

make light number
make state boolean

make currentHue number
make currentSaturation number
make currentBrightness number

make speed number 0.02

when start
.set light to 5
.set state to on
.tell bridge to "set state"
.set light to 6
.set state to on
.tell bridge to "set state"
.set currentBrightness to 1
.set currentHue to 0
.set currentSaturation to 1

Now we can start adding a second script to start talking to the Hue. We make a hue Bridge device, which in this case corresponds to the one on my home network. You need the IP address (or name) of your brigdge and an authorisation key, which you can get from the Hue Bridge (see previous posts). The two coloured bulbs I want to control are numbered 5 and 6 in my system - again you can find the number of your bulbs either by talking to your bridge, or running the hueInfo.sniff program, which will list out your bulbs.

I turn on my two bulbs, and choose an initial value of Red to get them started. 

..set currentHue to currentHue*(1-speed)+targetHue*speed
..set currentSaturation to currentSaturation*(1-speed)+targetSaturation*speed
..set currentBrightness to currentBrightness*(1-speed)+targetBrightness*speed
..set hue to currentHue
..set saturation to currentSaturation
..set brightness to currentBrightness
..set brightness to brightness * 0.5
..set light to 5
..tell bridge to "set color"
..set light to 6
..tell bridge to "set color"
..wait 2 secs

Now this slightly clever bit... I want the bulbs to change slowly so I take the current colour and mix it with a small amount of the target colour. If speed was 0 then they would never change, and if speed was 1 they'd change instantly. It only takes a very small value of speed (0.02) to produce a gentle change over about a minute.

Now we know the colour the bulbs should be right now, we send that to the hue bridge. My bulbs are in the TV room which gets used in the late evening, so I also half the brightness to create a more relaxed environment.

The result is really great - though cheerlight traffic is quite slow some evenings, and people pick some boring colours (white? and pink looks boring). Normally the Hue lights look great when you turn them on, but then they're just  static. Having them change over the evening is really fun, and because they change gradually they're not distracting. It's also quite cool knowing that someone random somewhere in the world has changed the colours in your TV room!

1 comment:

  1. It was very much trouble in the way of getting the right code in the system. Thankfully your post has helped me quite a lot in the understanding of the concepts.