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.

Thursday, 21 July 2016

Getting Friendly with Hue

Philips Hue system is an expensive addiction. I picked up a cheap starter kit that was on sale, and a couple of months later, I've spent hundreds of pounds installing it in as many rooms as I can afford. It's crazy fun, and while its not without technical issues, it works really great and does simple stuff that's genuinely useful. While the colour changing is its headline feature, having timers to turn lights on and off automatically, and being able turn on/off multiple table lamps when you enter/leave a room is something you grow to love quickly.

It's also very hacker friendly. Philips have built it as a totally open system. Not only do they provide a documented API and tutorials, but there are even tools built into the Hue Bridge to encourage you to play with the thing. Now with Sniff support its really easy to turn a hue light on and off, just as easily as you flash an LED!


The first thing you need to do is find the IP address of your bridge. It should be four numbers, something like 192.168.0.164. Then open up the Hue's debug tool in a web browser: 

http://<bridge ip address>/debug/clip.html

fill out the first two fields
URL: 
/api
Message Body:
{"devicetype":"Sniff"}

then press physical button on your Hue Bridge (a security feature, so it knows its you and not someone hacking your network). Finally press the  POST button on the web page.

In the response box you should see a value for your "username", which looks something like: 1028d66426293e821ecfd9ef1a0731df. Though they call it a username, its actually like more like  a password. It's whats know as a sparse space - a really big number, where only a few numbers are accepted to let you in. If you know the number then we know who you are, even though you haven't explicitly told us.

If you have problems, then check the linked tutorials from Philips to get the data you need. Now we've got a password we can start writing in Sniff. The first thing we need to do is make a device, and set up the "username" we got from the bridge:

make bridge hueBridge device
make bridgeAddress string
make authorisation string

when start
.set bridgeAddress to "192.168.0.164"
.set authorisation to "oCckjJ2ApQNHc76Q6vrAVmnrsnOJKaLESMK2uqLk"


There are lots of tutorials on how to blink and LED, but now we can "go large":

make bridge hueBridge device

make bridgeAddress string
make authorisation string
make light number
make state boolean

when start
.set bridgeAddress to "192.168.0.164"
.set authorisation to "oCckjJ2ApQNHc76Q6vrAVmnrsnOJKaLESMK2uqLk"
.
.set light to 1
.forever
..set state to on
..tell bridge to "set state"
..wait 1 secs
..set state to not state

To turn a light on or off, just select the light by number, (here we're using light number 1! The Philips tutorial shows you how to get a list of all your lights, and see there names - we'll probably support this in the future), set state to be on or off, and then tell the bridge to set the state.

The next thing to do would be to control brightness:

make brightness number

when start
.set bridgeAddress to "192.168.0.164"
.set authorisation to "oCckjJ2ApQNHc76Q6vrAVmnrsnOJKaLESMK2uqLk"
.
.set light to 1
.set state to on
.tell bridge to "set state"
.
.forever
..set light to 1
..set brightness to (sin of (timer*100))*0.5+0.5
..tell bridge to "set brightness"
..wait 0.2 secs

Here we turn the light on as before, but now we set the variable brightness, and call "set brightness to fade it in and out (using a value between 0 and 1). Note the delay - It's recommended not to send more than about 10 requests per second, just so the system can keep up.

Finally if you have a colour bulb can set the colour, using Hue, Saturation and Brightness, and telling the bridge to "set color". Hue is from 0 to 360, while Saturation is 0-1.


Running this from the desktop is fun, but we can go a stage further. If you've got an Arduino with an Ethernet shield then everything will work exactly the same on the stand alone hardware. All you need to do is tell it to use the ethernet shield instead of normal networking, so you need to add the following lines at the beginning of your code:

make spi device
make ethernet device D10
make networkMAC string "b6:ee:63:ed:95:cb"
make networkIP string "192.168.0.200"


Then use uno-sniff to download your program to an Arduino with an ethernet shield attached. That means you can write code to turn the lights on in response to any kind of hardware event. You could use a PIR motion sensor to turn the lights on automatically when you go into a room (and off when you leave), or just add a whole load of buttons to make a monster Hue control panel!

No comments:

Post a comment