I thought I would explain how I created a wee sample app to learn the restrictions of the API and have a play with the LIFX bulb.
I have been looking into different “smart home” (buzzword alert) devices recently but my question is always “Is there an API?”, usually followed by “It costs how much?!”. Usually the ones that have an API are from more premium brands and quite often they need a hub to connect to within the home. Learning to work on IOT projects can get expensive quickly so you need to do your research before making an investment. Lighting for example, I was keen to try out Phillips Hue, but they are quite expensive for just me hacking around with and also, you need a hub as part of the infrastructure. But then… Amazon prime day happened and I saw a LIFX smart LED bulb, good price, no hub required and it had an API. Yass!
So, that’s why we are here in my blog...I thought I would explain how I created a wee sample app to learn the restrictions of the API and have a play with the LIFX bulb. One of the main reasons I went for LIFX was that they have a developer community website: https://community.lifx.com/ and well documented API: https://api.developer.lifx.com/
First of all you will need to follow the instructions to set up your bulb on your wifi and signup for a LIFX Cloud account: https://cloud.lifx.com/
Then you can go to “settings” when logged into your cloud account and get an API key. You need to give a meaningful name and it’ll give you an API key- copy and paste this, you can’t get it again later.
Then you get your API key to use in your calls to the LIFX APIs:
I always like an API reference that let’s you put in sample parameters and it gives you the call you should make, and LIFX has this. You just need to put in your API key and it will work… your light will magically switch on when testing the API docs before you even write any code! As you can see in the screenshot below, the bulb is lit up red just by my call via API docs.
*Excuse the rubbish quality photo.*
So, I took this info and started a new solution (basic MVC template) in Visual Studio. I am a .Net developer for my day job so this is the quickest way for me to get started without having to think too much about the stack and focus on the task in hand- my shiney new smart bulb. My code just changes colour of the light when the user presses the button on the page- nice and simple… a hello world for smart bulbs, I guess! I have shared the C# code (MVC controller) on GitHub https://github.com/crgrieve/LIFX-hello-world/ . Nothing too exciting here to be honest, but hopefully it can be useful!
Then in my razor .cshtml file I can add this line, substituting the appropriate colour from the LIFX docs.
@Html.ActionLink("Red", "Index", "Home", new { colour = "red" }, null)
Wee demo on Instagram here.
So there it is, I got started with the LIFX API super quickly, only really took an hour or so. Excited to build something a bit more useful with this bulb but I struggle for ideas!
As usual say hello on Twitter. Get in touch if you have built anything with LIFX!