Because I’m an idiot, I’ve decided that what my life needs is voice activated lightbulbs. But voice control is so 2015. Let’s make these bulbs react to SINGING!
Here’s a demo – fair warning, my vocal prowess is “limited”.
OK, here’s how to do this.
- I recently got a bunch of Lfix bulbs as part of a bug bounty.
- There’s a full API for the Lifx bulbs.
- My mate Ruth has written a great demo on the Chome Web Speech API
- So, speech + Lifx = awesome.
This was a 5 minute hack, so my code is quite shameful, but here are the basics.
<?php $link = "https://api.lifx.com/v1/lights/all/state"; $authToken = ""; $colour = htmlspecialchars($_GET["colour"]); $data = array("color" => $colour, "duration" => "0.5"); $ch = curl_init($link); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data)); $headers = array('Authorization: Bearer ' . $authToken); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch);
Take the colour and send it to the bulb. Make the transition time fairly quick (half a second). The “Rainbow” command just sends random colours with a much shorter transition.
And… err… that’s it! The Lifx API lets you send colours as strings, hex codes, HSB, etc, so it is fairly flexible.
Sure, I could have bought an Amazon Echo (not available in my country) or a Google Home (not launched, will be abandoned next year) or a Raspberry Pi (too easy to lose) or built an app for my phone (just too much hard work) – but I’m quite happy playing around in the browser.
Here’s the video again, if you like watching things on Twitter.
My Lifx bulbs now react to my (*atrocious*) singing. Forgive me!
— Terence Eden (@edent) May 18, 2016