Last Sunday was yet another wet day. The thought of decorating filled me with horror so what to do?
Thinking back to my childhood I was always fascinated when, in a film, a vehicle’s location could be tracked by a blinking light on a CRT screen. So given we now have a Raspberry Pi in the house and I am the proud owner of a Nexus 4, the germ on an idea was sown.
Essentially the idea was to write a simple Android app that can periodically write back its current position to a server.
The first step was to determine the best way to handle the server side communications. Python seemed the obvious choice of development language and after a little research I discovered a framework called Flask that could be used to handle the necessary http communication. Flask is a lightweight web framework that has been written in Python. With only about half a dozen lines of code the Pi was now acting as a webserver and was able to receive an incoming request that supplied a latitude and longitude. This information was written to a local CSV file.
Next step was the development of an Android application: this provided the ideal opportunity to have a ‘play’ with the Google supplied Android IDE. To date we have always used Eclipse.
The app at this stage would be required to make a call to the Pi with its current location. To facilitate this a background service was created. This in turn sets up a location broadcast receiver that is responsible for obtaining the current location of the device. All that was then needed was the ability to make an http call passing the latitude and longitude to the Pi. To ensure Android apps are ‘well behaved’, any network activity has to take place on its own thread; given this the code for the http request was moved to an AysncTask.
Time to play…after resolving a few issues, the location of the Nexus 4 was being stored in the CSV file on the Pi.
At this stage, though, the output from the Pi was a little boring – I simply had an http request that dumped all the stored data to the browser. How much better it would be if the points could be plotted on a map? So again a little research revealed a Python extension that would handle the creation of a ‘KLM’ file, the format used by Google Earth. So again with a very view lines of code I was able to take the CSV stored on the Pi and convert it to KLM format. This functionality is triggered by an http request for a KLM file.
So today when leaving for the office I turned on the tracking on my phone. On arrival at the office I then triggered the KLM file generation. The URL for the file was then pasted into the search box for google maps and voila…