Friday, March 23, 2012

Update: Convert that old iPhone into a GPS Tracker

If you have not already, take a look at the previous post. This is an update on that covering my Django server side component.

In the first part we created a simple python script which announced the GPS co-ordinates of the device to a web server. So to complete the solution we will do the following.
  • Create a model and view to store posted tracker co-ordinates
  • Create a view to visualize the data with google maps API
I won't cover setting up django itself, there are plenty of good docs on the django site for that. Once you have django up and running create a new application therein like this:
python manage.py startapp tracker
create tracker/models.py and tracker/views.py with the following code: http://pastebin.com/0rxDhaX5

create tracker/templates/tracker/plot.html with the following code: http://pastebin.com/T3ACVDpY

Add the following to your urls.py
(r'^tracker/(?P.*)', 'DJANGOPROJECT.tracker.views.tracker'),
(r'^plot/', 'DJANGOPROJECT.tracker.views.plot'),
Now add the APP to you INSTALLED_APPS in settings.py
'DJANGOPROJECT.tracker',
Now the python app on the device will be able update the location models in the django application. And you will be able to view the current position via the /plot/ url.

Kegan

Sunday, March 18, 2012

Convert that old iPhone into a GPS Tracker

Background
So I have been pondering for ages what to do with my old iPhone 3G, and after finding my steering lock broken on my motorcycle, I have decided to turn it into a GPS tracker to fit to my motorcycle.

Find my phone doesn't seem to work on my jailbroken device, so I have gone for my own implementation.

My solution is this. A python script on the phone will periodically query the CoreLocation subsystem for the phone location and then post the in a RESTlike manner to a django web service running on one of my websites. I won't cover the django web service in this post, but needless to say you can adapt this script to advertise the location of the phone to you in a variety of ways.

Once I have the location, just pasting the coordinates into google maps will show where the phone is located.

Requirements:
iPhone 3G running iOS 4.2.1 Jailbroken
Python ( Cydia )
pyobjc ( Cydia )
OpenSSH ( Cydia )
Some public web server you have access to. ( I host my own, if you don't have access to one, maybe use some other nifty trick / API / twitter to send yourself the location. )

If you haven't already, jailbreak your phone. You can google that separately as I'm not going to cover the details of that. Just remember that if you need to network unlock your phone, pay special attention to the modem firmware you load. The default one that comes with 4.2.1 is not unlockable at this stage without breaking the GPS.

Once jailbroken, use cydia to install Python, Pyobjc, VIM and OpenSSH. Now that we have all the software we need, ssh into your phone, the default username is root and password is alpine.

Get the script http://pastebin.com/mHwigZXT and adapt to you needs. You need to create a web service to receive the coordinates or conjure up some other creative way. I thought about using twitter REST API via python to twit the location.

I have just setup my own django app that is logging the REST calls to a specific url with location coordinates and accuracy. You can go low-tech and just use the apache logs of your server to track your phone as-well. Just pasting the coordinates into google will show you where the phone is.

Implementation
Get the script http://pastebin.com/mHwigZXT

Name the script without any extension, e.g. "location" ( I had troubles running it from launchctl as location.py )

SCP the script up to your phone : scp location root@10.0.0.100:/var/mobile/ ( username root passwd alpine )

SSH into your phone, chmod +x /var/mobile/location and then execute the script manually to check it. It takes a while to load all the objects and location, so be patient. 15 seconds or so at least.

To get your application to launch at boot time and to re-run the script every 15 minutes, you need to create a application plist file and put it in the right location. I have made one for you!

Get the application plist file file from http://pastebin.com/1jdb1d3d

scp that to your phone with:
scp com.my.tracker.plist root@10.0.0.100:/System/Library/LaunchDaemons/

reboot the phone.

And thats that. You should now see some calls to your apache server with a URL containing the coordinates of you phone.

Remember to leave WIFI and 3G and location services enabled! ;)

Integration
I have integrated the phone into my motorcycle and even wired to up to the bike to let it charge when the bike runs. There is also a small solar panel which charges the phone slowly to extend the life.

have fun!

Kegan