Building a PhoneGap/Cordova Home Automation App

The first Android tablet app I did for the wall tablets in the house looked like this.


I’ve been planning a more fully featured app and put together a mock-up a while ago to focus my thoughts. I really wanted something that looked simple and clean and ended up quite liking the coloured blocky approach you tend to see on computerised cash-registers in bars and cafes – that also bears a striking resemblance to the Windows 8 UI.  I was envisaging something like this.

Tablet layout 1 nonames

As I mentioned in a previous post I really wasn’t looking forward to diving back into native Android app development again. So I looked at PhoneGap and got hooked on the idea of building a fully dynamic app in HTML5.

The current version (source code here) looks like this.


a fully dynamic web app

I found a framework for developing windows 8 style UIs on HTML5 but found it very feature heavy and quite hard to understand. There are other frameworks but they all seem quite bulky. So I decided to see how hard it could be to develop my own with the basic idea that the code structure would follow as closely as possible the UI requirements (since no data management is needed).

A big problem in getting things to work fluidly in HTML is the lack of programmability of CSS. I looked at frameworks like SkelJS but as far as I can see they handle CSS by switching between difference CSS files based on the platform. I really wanted to change the CSS programmatically in the same code that handles other UI changes like orientation and realised that this could be achieved with LESS – which I have used a little – but ended up coding CSS changes into CoffeeScript along with the rest of the logic.

The structure I’ve ended up with is:

UML wall tab sw

inheriting tile behaviour

Each tile in the UI is potentially a button and can provide some live data – a much simplified form of Windows 8 UI. I found CoffeeScript to be extremely succinct in expressing simple inheritance relationships.

Absolute positioning is used to layout the tiles with the left and top margins, width and height being set explicitly in javascript. The simplest derived class is the clock. This adds content to the inner div of the tile (div.sqInner) to display an animated digital clock.

The clock animation is borrowed from here with thanks.

laying out tiles

The tile container does most of the work calculating the layout. The algorithm for dealing with larger tiles (those that span more than one column) is pretty simple:

  • I sort the tiles into size order with the single tiles first and the wider ones later.
  • I estimate the number of columns needed for a group of tiles by calculating the total area of tiles and adjusting if there are wide tiles in the group that otherwise wouldn’t be accommodated (note that the algorithm I use might get it wrong if there are many 2 and 3 column tiles and few single ones but I don’t anticipate that scenario in my application).
  • I fill the tile grid for the group working across each row and then downwards so the top row(s) should be filled with smaller tiles and the larger ones placed lower down.

This code is in the file.

home automation server access

The primary job of the app is to control lighting and other home automation activities. I currently have two home automation servers as I’m transitioning from Vera 3 to Indigo – the reasons for the transition I will try to explain at some other time. The reason for doing it gradually is really the amount of work involved as I have over 100 Z-Wave devices in the house (mainly dimmers and relay-switches but also PIR sensors, thermostats and TRVs).  To achieve this I read the “action-groups” or “scenes” from the home automation server and populate the tiles in the tablet UI with “scene-buttons” each one having a label such as “Office On” or “Hall Mood”. These are then buttons which, when touched, cause a command to be sent back to the home automation server to get it to take the required action.

My initial take on this was to access the home server directly from the tablet. This got me into the mire of cross-domain requests and my previous wailing blog post.  Eventually I did get that approach working and was moderately happy with it.

But around this time my friend Andy Chitty came around and suggested that it would be easier to update the app if it was hosted on a server rather than resident on the tablets. I wasn’t in favour of this idea initially but after some thought, and having already gotten the software working well stand-alone, I realised that he was probably right. So I found a way to achieve this using PhoneGap with an external URL for the index.html file and built an intermediate server to handle all requests to the home automation servers.  I’m going to write something about the intermediate server sometime as I’m finding this is a good way to remind myself about what I’ve done – I’ve already referred to my earlier blog posts when I wanted to remember how something worked.

calendar access

Getting access to calendar information was easier than I’d expected as my calendar is on Office 365 and there is a way to publish it via a URL. The only problem with this is that the format for the received data is iCal which is pretty horrible to parse. Fortunately now that I’ve moved to having an intermediate server the problem can be moved to there and there are at least a couple of passable Python libraries for handling iCal so I’ve used those.  The end result is that the code for the calendar tile is very simple.

I’ve only included the part here which gets the calendar updates. The rest is a little longer since it attempts to set the font size optimally for the size of the box – I find this helps as I sometimes don’t need reading glasses to read the calendar entry – at least if there aren’t too many things going on in a day!


The final use of the intermediate server is to store the configuration information for each room. Currently this just sets a few “scene-button” tiles to be in the left-most group on the screen so they are easily accessible. So, for instance, in the kitchen the config file looks like this:

To avoid having each tablet identify themselves to the server I currently use the IP address of the tablet to access the configuration. I’m not sure how well this will work over time as the tablets are using DHCP for their IP addresses and they may be subject to change I guess.

robBuilding a PhoneGap/Cordova Home Automation App

Comments 3

  1. Kay

    Little old, but might be worth pinging in on.

    So as to avoid sugar coating it, you missed the point a little on skeljs/responsivedesign/media queries. You are correct it doesn’t do conditional changes itself based on ui conditions, that’s more the domain of media queries in CSS3. Skeljs keys off the window resize event and applies breakpoints to them and then loads a form factor appropriate style sheet that lets you reuse the same UI without rewriting it. The ability to turn css files off and on at random can be done manually easy enough (just use the disable attribute), skeljs just does it for you with configurable trigger management.

    function resizePage(){
    //check the page size
    //is it one we care about?
    //do something with it 🙂
    $(window).on(‘resize’, resizePage); //register it to fire like an badger with OCD

    You’re probably doing more work than you have to, but we all do that from time to time.

  2. Post

    Hi Charlie, Sorry to hear that. The first thing to try would be loading main.html instead of index.html (as index.html just references main in any case). If that doesn’t change anything then you could try putting something (like Hello World) in the section of the main.html file and try again. That should show up in the browser. If that works ok then maybe start looking at whether Javascript is turned on in your browser. It all relies on JQuery too so you could perhaps make sure that is loading ok. I hope this helps – let me know how you get on. Regards Rob

  3. Charlie

    I tried building your app in PhoneGap build for IOS and all I get is a black screen, same thing if I load index.html in Safari on my Mac. How do you get the program to run?

Leave a Reply

Your email address will not be published. Required fields are marked *