Sooooooo, is Harry Potter nerdy? Yes. Is nerdy cool these days? Yup. Does that make my wife the coolest person I know? Yes. Is answering your own questions annoying? HECK YES. So no more of it. Karen was having a poor day today, so I finished a little project I have been promising her for ages. It looks simple but has a few things going on behind it, which I will explain, but first the actual project.

Anyone who has seen the Harry Potter film (2, 3?) with the Weasley clock will recognize this one. It’s a clock that geo-locates the members of the Weasley family. This one locates members of my family (only me so far). And get this, IT’S ACTUALLY HAPPENING!!! The hand will point to wherever I am, updating every 60s! I only have three locations at the moment: home, work and lost (somewhere other than home or work). But that captures about 85% of my life, so it’s fairly accurate. AND it is the background of our home computer, which is our main entertainment display! It’s also displaying a real time count down to the movie premiere next week.

OK, so for those who care, this is how I did it. The principal components used are: iPhone App InstaMapper’s GPS Tracker writing to a webpage in a simple text line, a crontab to grab that text line, a shell script to parse it and translate it into a location, a series of pre-edited .jpegs of the clock, and finally geektools to display it on the desktop. This is all done in Mac but I am certain nothing about this is Mac dependent (though you may need to find separate solutions for part of the components). In any case, this all sounds difficult but in reality it’s not too bad. Step by step.

Getting and using the iPhone App: The app was $.99 when I bought it a long time ago. I don’t exactly recall setting up the site for a text GPS coord. line but I recall it not being very difficult.

The crontab to grab that line: A crontab is an automated script that runs when you specify it to. In my case I run it as often as possible which is in 1 minute increments. InstaMapper only uploads GPS data every ~30s or so, which defeats the purpose of getting coordinates much faster than every 1 minute. Open up a terminal, enter the command crontab -e. Assuming vi is your editor, hit the i key to enter into input mode, copy and paste this line:

* * * * * curl “http://www.instamapper.com/api?action=getPositions&key=13399805532640859891” > ~/Documents/PotterApp/iphone_loc.txt;

Substituting your web page. When you are done esc key, wq enter. This will save the GPS locations in a text file on your computer. It is very small so don’t sweat the space.

A shell script to parse it and translate it into a location: This I put into the same folder as the downloaded text file. It calculates a proximity of my iPhone to the two locations, work and home, and defines potterCurrent.jpg based on this. Lost is the default.

#!/bin/bash
lat_iphone=`cut -f 4 -d , ~/Documents/PotterApp/iphone_loc.txt | tail -n 2 | head -n 1` ; long_iphone=`cut -f 5 -d , ~/Documents/PotterApp/iphone_loc.txt | tail -n 2 | head -n 1` ;
lat_home=33.76391;
long_home=-118.13983;
lat_work=33.63904;
long_work=-117.852681;
rm ~/Documents/PotterApp/potterCurrent.jpg
cp ~/Documents/PotterApp/potterlost.jpg ~/Documents/PotterApp/potterCurrent.jpg
lat_diff=`echo “$lat_iphone – $lat_home” | bc`;
lat_diff=`echo “1000 * $lat_diff” | bc`;
long_diff=`echo “$lat_iphone – $lat_home” | bc`;
long_diff=`echo “1000 * $long_diff” | bc`;
lat_diff2=`echo “$lat_diff * $lat_diff” | bc`;
long_diff2=`echo “$long_diff * $long_diff” | bc`;
sum_diff=`echo “$lat_diff2 + $long_diff2” | bc`;
sum_diff=`echo “$sum_diff / 1” | bc`;
if [ $sum_diff -eq 0 ]; then
rm ~/Documents/PotterApp/potterCurrent.jpg
cp ~/Documents/PotterApp/potterhome.jpg ~/Documents/PotterApp/potterCurrent.jpg
fi
lat_diff=`echo “$lat_iphone – $lat_work” | bc`;
lat_diff=`echo “1000 * $lat_diff” | bc`;
long_diff=`echo “$lat_iphone – $lat_work” | bc`;
long_diff=`echo “1000 * $long_diff” | bc`;
lat_diff2=`echo “$lat_diff * $lat_diff” | bc`;
long_diff2=`echo “$long_diff * $long_diff” | bc`;
sum_diff=`echo “$lat_diff2 + $long_diff2” | bc`;
sum_diff=`echo “$sum_diff / 1” | bc`;
if [ $sum_diff -eq 0 ]; then
rm ~/Documents/PotterApp/potterCurrent.jpg
cp ~/Documents/PotterApp/potterhome.jpg ~/Documents/PotterApp/potterWork.jpg
fi
echo “$def_loc”
echo “iPhone Coordinates: “${lat_iphone}”,”${long_iphone}

I added this line to my crontab above to execute it. It can go right after the other line as long as there is a semicolon separating the two. For those that are new to scripts, you will use vi get_loc.sh to enter into the editing mode, again hit the i key to get into insertion mode and copy and paste this in. When you are done esc key, wq enter. You will likely have to enter the following command to it to execute chmod 755 get_loc.sh.

/bin/bash ~/Documents/PotterApp/get_loc.sh

Now, the image potterCurrent.jpg should reflect the GPS coordinates of my phone. Note I edited these in Photoshop, I do not own them. Some one else should get credit for the artwork, not myself.

Finally, we want to have the desktop display the image, refreshing every 60s. Download GeekTool to do this. It’s a pretty amazing program capable of doing amazing things. You can follow the instructions online to see how to display an image. I also have parsed my system’s time to get the countdown to the movie, that line goes into a GeekTool shell script window. It is the following:

days=`date | awk ‘{print $3}’`;cntdays=`expr 19 – ${days}`;echo $cntdays “days”;hours=`date | awk ‘{print $4}’ | cut -c 1-2`;cnthours=`expr 23 – ${hours}`;echo “” “$cnthours” “hours”;mins=`date | awk ‘{print $4}’ | cut -c 4-5`;cntmins=`expr 59 – ${mins}`;echo “” “$cntmins” “mins”;secs=`date | awk ‘{print $4}’ | cut -c 7-8`;cntsecs=`expr 59 – ${secs}`;echo “” “$cntsecs” “secs”;

So, there it is! A bit complicated, but pretty fun. I hope it cheers her up! Happy 9th Anniversary my love!

Advertisements