Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2012 03:12
Show Gist options
  • Save joewalnes/4282399 to your computer and use it in GitHub Desktop.
Save joewalnes/4282399 to your computer and use it in GitHub Desktop.
Am I home yet?
#!/usr/bin/env python
"""A tiny script that polls your location on Google Latitude, and updates
the color of a Blink1 LED.
Red = At work
Blue = At home
Green = On my way
Should work on Windows, OS-X and Linux. Requires Python 2.7 or later.
1: Download the blink-1-tool and put in the current directory:
2: Update your user ID (see
3: Update your home and work latitudes.
#### Config
user_id = 'FILL THIS IN'
# Latitude, Longitude
work = (41.882217, -87.641367)
home = (THIS, BIT)
#### End of config
import atexit
import json
import urllib
import subprocess
from time import sleep
from math import sqrt
poll_interval = 20 # in seconds
def get_coords(user_id):
"""Return (latitude, longitude)"""
url = '' % user_id
content = urllib.urlopen(url)
data = json.load(content)
coords = data['features'][0]['geometry']['coordinates']
return coords[1], coords[0]
def distance(coord1, coord2):
"""Dumb implementation. Doesn't take into account curvature of earth, but good enough for my commute home."""
return sqrt((coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2)
def blink_tool(arg):
subprocess.Popen(['./blink1-tool', arg]).wait()
def main():
atexit.register(lambda: blink_tool('--off'))
while True:
current_coords = get_coords(user_id)
current_distance = distance(current_coords, home)
max_distance = distance(work, home)
ratio = min(1.0, max(current_distance / max_distance, 0)) # value from 0.0 (home) to 1.0 (work)
print 'Position:%s Ratio:%s' % (current_coords, ratio)
if ratio < 0.05:
blink_tool('--blue') # home
elif ratio > 0.95:
blink_tool('--red') # work
blink_tool('--green') # commuting
except Exception as e:
print 'Error', e
if __name__ == '__main__':
Copy link

@joewalnes Code looks fun. What unit are you returning from distance() ? I couldn't make it out. Is this some kind of known formula/algorithm?

Copy link

kgashok commented Mar 13, 2014

Pythagorean theorem ring a bell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment