Update: After further testing, it seems there are a couple of major problems with this setup. The first is that the iPhone doesn’t appear to update if it goes to sleep and isn’t plugged into mains power. The second is that the GPS coordinates I’m getting are wildly inaccurate for my location (varying by a few hundred kilometres) so I figure there’s something wrong with the “findme” application called below. I’ll keep looking for a better way, but for now take this article with a grain of salt.
In this article I’ll explain how to program your jailbroken iPhone to automatically upload its location to a server of your choosing at regular intervals. This will work whenever your iPhone is switched on and has a network connect (3G or Wifi).
As I’m going on holidays tomorrow, I’m going to be in situations where theft of personal belongings is a lot more likely. So after coming up with the idea this afternoon, I spent the last few hours hacking together a tracking solution for my phone. It idles silently in the background, popping up every fifteen minutes to write a latitude/longitude pair to a timestamped database on my server.
This tutorial is going to be a whirlwind because it’s 11pm and there’s so much to explain. And I have a holiday to go to.
You will need:
- One jailbroken iPhone: I used an iPhone 3G, not sure if this is possible with an original iPhone. To jailbreak your phone, use QuickPWN. Disclaimer, warranty, own risk etc etc.
- MobileTerminal, OpenSSH, Netatalk and cURL, installed as packages from within Cydia
- findme, a script written by Erica Sadun and available in a binaries package on her site
- access to a server with PHP5 & MySQL (possible with other configuration but outside the scope of this article)
- a moderate level of coding chops – I rate this project “moderately difficult” because of the diverse areas of skill required.
The basic steps involved are:
- Preparing your iPhone to be messed with
- Teaching your iPhone to query its location using GPS
- Constructing a server-side script to record location data
- Teaching your iPhone to run the location script at regular intervals
- Outputting the data in a readable format
Let’s get started.
The Setup
After jailbreaking your iPhone you will install MobileTerminal, OpenSSH, Netatalk and cURL through Cydia. Then you’ll need to connect to your phone to drop files onto its disk. The easiest way is to connect over Appletalk, but if you’re handy with a command line (or don’t have a Mac) you can ssh root@10.1.1.1 with default password ‘alpine’ and your iPhone’s actual IP address (get this in settings -> network) from your PC.
Drop the findme script into /bin. Now make it executable using chmod 775 findme. When you invoke findme from the command line, it will return some XML containing your phone’s latitude and longitude, like this:
<?xml version=”1.0″?><SearchResults><Success>true</Success><Latitude>-33.887242</Latitude><Longitude>151.256718</Longitude><Method>Skyhook WiFi Location</Method></SearchResults>
(Eagle-eyed readers will noticed I’ve given myself a fancier address in this example.)
The Storage
So now we’ve got a process on your iPhone that delivers GPS over XML. The next step is to transfer that information to a database. For this part, I’m relying heavily on Erica Sadun’s iPhone Lojack article on TUAW, which goes over the same stuff as this article but using Twitter as the output channel.
What we want to do is post the output of findme to a PHP script which then posts the data. You’ll need to create a shell script and upload it to the same folder as findme. Here’s a template:
#! /bin/sh
curl –basic –url http://www.yourserver.com/lojack/index.php5 \
–data status=”`findme`” \
Save this file as ‘loc’ (no extension) and copy it to the same directory as ‘findme’. Don’t forget to make it executable: chmod 755 loc
Now, on your server at the path specified above, create a PHP file that grabs the posted status message and writes it to a database. This script assumes you have a database with a table called ‘locs’ containing two DECIMAL(9,6) fields and a TIMESTAMP field with the current time as its default entry:
< ?php
$username=”username”;
$password=”password”;
$server=”localhost”;
$database=”mylocations”;
$status = $_POST['status'];
$status = stripslashes($status);
$xml = simplexml_load_string($status);
//echo “Status: ” . $status;
$lat = $xml -> Latitude;
$lon = $xml -> Longitude;
echo $lat . ” ” . $lon;
$conn = mysql_connect( $server, $username, $password ) or die(“Err:conn”); //connect
$rs = mysql_select_db( $database, $conn ) or die(“Err:db”); //select db
$sql = “insert into `locs` (lat, lon) values ($lat, $lon)”; //the query
$rs = mysql_query( $sql,$conn );
? >
Now you should be able to invoke loc from the command line (shell, whatever) and it will send the XML to your server script, which will parse out the GPS coordinates and save them to a database. If it’s not working, try removing the comment slashes from //echo “Status: ” . $status; to see what results cURL is getting.
Timing is Everything
Great, so that script sorts out a single location upload. To be useful as a lojack, we want this upload to occur at a regular interval. For that, we’ll use the iPhone’s own LaunchDaemon service. Modify the instructions found in step 4 of the TUAW article to suit your setup. Here’s the script I’m using, com.apple.lojack.plist:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>
<string>com.lojack.upload</string>
<key>ProgramArguments</key>
<array>
<string>/bin/loc</string>
</array>
<key>StartInterval</key>
<integer>900</integer>
</dict>
</plist>
The 900 in the script above specifies this script will run every 900 seconds (15 minutes). You’ll need to restart your iPhone before the LaunchDaemon will work.
Output your results
It’s no good storing all this away on a server somewhere. You need to make it user-friendly. This script will query the database and print the output according to your choice: vanilla, or in tables with links to Google Maps:
< ?php
$username=”username”;
$password=”password”;
$server=”localhost”;
$database=”mylocations”;
$layout = $_POST['layout'];
$conn = mysql_connect( $server, $username, $password ) or die(“Err:conn”); //connect
$rs = mysql_select_db( $database, $conn ) or die(“Err:db”); //select db
$query1 = mysql_query(“SELECT * FROM locs”);
echo ‘<form action=”where.php5″ method=”post”>’;
echo ‘<input type=”radio” name=”layout” value=”plain”>Plain<br>’;
echo ‘<input type=”radio” name=”layout” value=”table”>table<br>’;
echo ‘<input type=”submit”></form>’;
if ($layout == “plain”) {
while ($table1 = mysql_fetch_array($query1))
{
echo $table1[0] . “,” . $table1[1] . “,” . $table1[2] . “,” . $table1[3] . “<br>”;
}
}
if ($layout == “table”) {
echo “<table border=1><tr><td>Timestamp</td><td>Latitude</td><td>Longitude</td><td>id</td><td>Link</td></tr>”;
while ($table1 = mysql_fetch_array($query1))
{
echo “<tr><td>$table1[0]</td><td>$table1[1]</td><td>$table1[2]</td><td>$table1[3]</td><td><a href=\”http://maps.google.com/maps?q=$table1[1],$table1[2]\”>Map</a></td></tr>”;
}
echo “</table>”;
}
? >
While I’d love to show you the results, I’m not quite ready to share the intimate details of my physical location with all of you. Hopefully this article can act as a guide for those with a little knowledge in all of these areas – let me know in the comments if there’s anything that’s unclear. In the future I’d like to build a way to render the data collected as a heat map, or a series of paths – but for now, I’ve got a beach to go and lie on.