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!

Release 28: And About Time too...

We normally seem to push out a release every month or so, with some new drivers and a few fixes and optimisations. However since the massive Microbit push back in April we've got almost 3 months without an update. I'm blaming end of school year burn out!

So here we have release 28. To be honest, its been such a long release cycle some of the thing that have gone into it seem like ancient history. There are lots of updates, and fixes for Microbit that just missed the last release, including code for the demos. It should also be a little faster.

jsniff has seen a lot of work. This lets you compile sniff programs to javascript and embed them in a web page. We've been using this internally for a fairly major application that we'll announce soon. If you compile with "jsniff -S xxx.sniff" it will include code to load and save files to a web server for permanent storage, so you can make really advanced web apps. There's a bit of work you need to do server side, but we'll document that soon. If you get a compiler error about "blacklist"ing when compiling with jsniff then you need to make a minor mod to your emscripten install - check the error line which should specific a line of code in the emscripten code... just edit that file, and delete the two lines and you're good to go again. More details later...

If you use the Arduino ethernet shield we've made a change to the way the ip address is specified. Previously this was stored in eeprom, but to increase portability you need to add this to your code. Check the examples/Embedded/webclient example to see how.

Finally we added support for Philips Hue light bulbs!! This is lots of fun, as you can make your own devices to turn your lights on and off. This is still a pretty basic implementation, and we'll probably add more to it in the future

There's a few other odds and ends in there too, but head over to the downloads page and try it out!

Incidentally "and about time too" is the title of Bernie Marsden's first solo album from 1979.