7 February 2017

Lightbulb Moment with Bluemix

Lightbulb Moment with Bluemix

I have seen the light.  Yes this one.  I live in a dark street so an outside light is essential on the front of the house. This is my light. It is a basic light without any motion detection or anything clever. I simply leave it on 24 hours a day during the winter and turn it off 24 hours a day during the summer. It works but it is not very efficient. Therefore this month I finally relented and bought myself a smart lightbulb.  I chose a Hive Active Bulb as I already have a Hive central heating controller and therefore didn't need to buy another smart hub for my router.

The Hive system provides a simple scheduler for turning the light on and off at specific times. But the reasons I went ‘smart’ was so that I could come up with a more sophisticated way of turning the light on and off; specifically by linking the light on/off to sunrise and sunset times.

If this then that

I did some research and found that IFTTT.com can be used to control Hive Active bulbs (and others such as the Philips Hue).  Even better there is a sunrise/sunset IFTTT event trigger provided by Weather Underground.

I wired up the IFTTT Weather Underground applets and was happy that it worked right away. However there is a slight annoyance.  Not a major annoyance I grant you but it still bugged me.  The problem was that the event to trigger the light turning on and off was often delayed. I then spotted the small print on the IFTTT website:

Admittedly the events were only 5-20 minutes delayed rather than an hour but I thought I could do better…

Custom Sunrise/Sunset Trigger

Enter IFTTTSunTimes.mybluemix.net!

Long story short I wrote my own sunrise/sunset trigger that is accurate to the nearest minute of a sunrise or sunset and hosted it on IBM Bluemix.

Check out my trigger app here:

Techie Bit

As with a lot of Internet of Things based solutions there is a bit of a daisy chain of elements used to actually control the light!

For the techies out there I am using a NodeJS app hosted on IBM Bluemix.  I chose NodeJS as I knew that there are a *lot* of ready built packages of function out there.  In this case there were ready built packages for providing things such as a web API (Express), calculating sunrise/sunset times (Suncalc), integrating with a database (Cloudant), scheduling a trigger (Node-schedule), encryption of Maker keys in the database (Crypto-js) and security (Helmet). The app simply sets timers based on calculated sunrise and sunset times that then call a web service at sunrise and sunset to control the light. I still rely on IFTTT but instead of using the WU trigger I use the custom ‘Maker’ trigger instead. This allows me to trigger the light when I want. Where the Weather Underground trigger can be delayed up to an hour, my custom trigger is triggering the events consistently within a minute.

It turned out to be quite easy to write the application, but as I have said in other posts, the non-functional elements of the solution took up significantly more time than getting the core functionality to work.  Take a look at the pie chart below where I have estimated the time it took to do each feature of the application:

As you can see about 95% of the effort was handling the non-core functional activities.  Some of these activities I had never done before so there was an element of learning time and some trial and error but still it does go to show that the bits of a solution that you can see really are only the tip of the iceberg.

The IFTTTSunTimes trigger is not just for Hive lights, it will work with any IFTTT applet that you want to trigger based on the the sunrise and sunset times.

I have joked that this is the most over engineered lightbulb application in existence, but it has been fun!