Martin's corner on the web

My Raspberry Pi knows where I am..

Here is a small project with some potential in home automation. I had an idea to create a script, that would track my (and not only) location and use that information to make decisions for home automation. Some ideas:

  • Climate control: Turn down heating a bit when I am some distance away from home and turn it up when I get closer. I am also aware of the weather, so this can aid decisions as well.
  • Track and log my and family members positions, send me alert messages if kids leave or enter certain perimeter
  • Switch on/off certain power plugs, lights i.e. garage when I am getting closer to or leaving the house

We use Android phones at home, these allow you to track location using Google Latitude. I found out, that you can create the so-called “location badge”, which provides you some small html code that you can show on your web site and let users know where you are. The code for the badge can take a parameter and report back your position in JSON format, exactly what I need. So I sign up for the service and get the code for my location badge. I set it to report best location, but will not make that public, as I want to keep that information for internal use only.

So here is a small PHP script that can be run as a cron job to grab my position every once in a while. The script can be extended to grab many users’ position if needed.



//Change user ID below
$json_string = file_get_contents("**********************&type=json");
$parsed_json = json_decode($json_string,TRUE);

$lon = $parsed_json['features'][0]['geometry']['coordinates'][0];
$lat = $parsed_json['features'][0]['geometry']['coordinates'][1];

$accuracy= $parsed_json['features'][0]['properties']['accuracyInMeters'];
$timestamp= $parsed_json['features'][0]['properties']['timeStamp'];
$geocode= $parsed_json['features'][0]['properties']['reverseGeocode'];

echo "Current location is:". $lat . "," . $lon . ", accuracy is " . $accuracy . " m (" . $geocode .") from " . date("m/d/y H:i:s",$timestamp);

//Change the URL and EmonCMS API below
$url = 'http://URL/api/post?apikey=KEY&json={longitude:' . $lon . ',latitude:' . $lat .'}';
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);


2 thoughts on “My Raspberry Pi knows where I am..

  1. Pingback: Calculating distance between two GPS coordinates | Martin's corner on the web

  2. Greg

    I really like this post, I am doing it on my server rather than a Raspberry Pi.
    I will at some point soon use my arduino ethernet to lock and unlock my PC when I am out of range or returning as I am forever leaving my monitor on.