Monday, 12 November 2012

on{X} and Geo-Fencing

Every single day when I leave work I send a text to my wife to the effect "I've left work".  I've always wanted to automate this long and onerous (!) task.  Android devices have GPS and you can get your position using an application but a)it's a pain writing these applications when all you want to do is tinker and b)constantly polling for your location is going to ruin your battery life.

Once again, on{X}came to the rescue.  It has the concept of "regions" where you can define a position (with lat and long) define a radius around it and then trigger when you go in and out of this region.  on{X} state they have advanced algorithms to do this, for example they spot you're moving using the handset accelerometer and only then do they check your location.  Hard to verify but what I do observe is the handset is not dumbly polling away for your location every 2 minutes.

The second thing I wanted to be able to do is automatically turn on WiFi when I come home and turn it off when I leave home (saves battery, avoids sniffing out unwanted WiFi hotspots, saves on cellular data usage).

Best explained with the example code from the on{X} site, (you'll understand that I don't want to put my personal code in here):

// remove old regions device.regions.stopMonitoringAll(); // create a new region var region = device.regions.createRegion({ 'latitude': 32.166476, 'longitude': 34.799817, 'radius': 500, 'name': ' Dan Acadia' }); // subscribe to event on entering any region (same works for 'exit' event) device.regions.on('enter', function (signal) { console.info('Got GF event. Lat: ' + signal.latitude + 'Lon: ' + signal.longitude + 'Radius: ' + signal.radius + 'Name: ' + signal.name); var notification = device.notifications.createNotification('Welcome to:' + signal.name + ' !'); notification.show(); }); // start monitoring for region device.regions.startMonitoring(region); console.info('Show notification to the user.'); var completionNotification = device.notifications.createNotification('Geofences example script received'); completionNotification.show();

Here you create a region:

var region = device.regions.createRegion({ 'latitude': 32.166476, 'longitude': 34.799817, 'radius': 500, 'name': ' Dan Acadia' });

...and define an event handler for when you enter it:

// subscribe to event on entering any region (same works for 'exit' event) device.regions.on('enter', function (signal) { console.info('Got GF event. Lat: ' + signal.latitude + 'Lon: ' + signal.longitude + 'Radius: ' + signal.radius + 'Name: ' + signal.name); var notification = device.notifications.createNotification('Welcome to:' + signal.name + ' !'); notification.show(); });

You can equally define event handlers for when you exit a region as well.

So using Google Maps to tell me the lat and long, I've defined 7 regions.  Home, work, half way from home to work, town centre for work, town centre for home, the town that I grew up in, local leisure  centre.

When I exit the work location the handset sends a text message to my wife.  When I enter my home region it turns WiFi on, (and off when I leave the home region).  Job done.

I've also got the on{X} application logging to COSM, (see previous post).  I associate an integer with each region and post a positive value to COSM when I enter a region (e.g. 1 for home) and a negative value when I exit a region (e.g. -1 for home).

COSM also has a trigger option that allows you to send Tweets when values hit certain thresholds.  So for example when I send a "1" to COSM, it detects this and then tweets this to a specific account.  Here's an example screenshot:



Gloriously geeky!