Using Geo-fencing for triggering events

After I got my GPS location published to the MQTT broker relatively easy yesterday, I really wanted to see the geo-fencing working. How nice would it be if I (eh, my Domotica system) could determine whether I’m entering or leaving a certain area (a circle, to begin with).

For that, I needed a way to calculate distances between 2 GPS coordinates, which I found here. Not only does this site explain the math, but the Javascript is provided too, in such a way that it’s ready-to-use for what I want to do with it – brilliant, this makes life even easier than it already is ๐Ÿ˜‰

After some copy&paste the code for calculating the distance between 2 GPS points was ready to be used; now all I had to do was write a small script that would subscribe to ‘my’ GPS location provider (i.e. the Btraced GPS App), define our house in GPS coordinates, calculate the distance between our house and ย “me” and watch what happens when I drive from my work location back home. I also decided to draw a virtual circle around our house with a radius of 4 km and checked every incoming GPS point to see if I was in- or outside of that virtual circle. No big deal, around 25 lines of code should do the job:

var LatLon = require('./latlong.js');
var tools = require('./tools');
tools.systemId = 'geofence';

var thuis  = new LatLon(52.0071670, 6.0393751);

(function connect() {
    tools.clog('connecting...');
    tools.mqttClient.connect({clientId: tools.systemId});
})();

tools.mqttClient.on('connect', function(packet) {
  tools.clog('connected to broker, subscribing to all locations');
  tools.mqttClient.subscribe('raw/+/location');
  tools.mqttClient.on('message',function(packet){
    var txt;
    txt = packet.payload;
    lat = eval('('+packet.payload+')').latitude;
    lon = eval('('+packet.payload+')').longitude;
    txt +=', dst='+thuis.distanceTo(new LatLon(lat, lon), 4).toString()+' km';
    txt +=', in='+(thuis.distanceTo(new LatLon(lat, lon), 4)<=4?1:0).toString();
    tools.clog(txt);
  }
  );
});

And late in the afternoon, when I drove home this appeared on the screen:

16:35:04 {“latitude”:51.9812396,”longitude”:5.9806257}, dst=4.949 km, in=0
16:35:13 {“latitude”:51.9819519,”longitude”:5.9810719}, dst=4.878 km, in=0
16:35:18 {“latitude”:51.9828016,”longitude”:5.9816207}, dst=4.793 km, in=0
16:35:32 {“latitude”:51.9843738,”longitude”:5.9856472}, dst=4.467 km, in=0
16:35:41 {“latitude”:51.9861408,”longitude”:5.9893560}, dst=4.146 km, in=0
16:35:50 {“latitude”:51.9882380,”longitude”:5.9928792}, dst=3.816 km, in=1
16:35:59 {“latitude”:51.9905545,”longitude”:5.9960376}, dst=3.495 km, in=1
16:36:05 {“latitude”:51.9927831,”longitude”:5.9987393}, dst=3.209 km, in=1
16:36:14 {“latitude”:51.9949893,”longitude”:6.0015182}, dst=2.924 km, in=1

16:42:32 {“latitude”:52.0070857,”longitude”:6.0398408}, dst=0.03313 km, in=1

 

Now I have almost all the ingredients for triggering events based on geo-fencing…
Let’s start with something to improve safety – for instance, I can let the touchscreen speak a text like “Diner needs be ready in a few moments now!” or something similar – it will save me a phone call while driving home, so I can concentrate more on driving than something else! ๐Ÿ˜‰

You see? Home Automation is good for a lot of things!

Bookmark the permalink.

2 Responses to Using Geo-fencing for triggering events

  1. jwestra says:

    This could really be useful.
    This post also gives some nice insight how you communicate with your wife ๐Ÿ˜‰

Leave a Reply

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