Introduction: PiDroidGPSTracker

For the #CUITPHackathon organized at the University of Colorado at Boulder on 11/15/2014, our team built a GPS tracker using a Raspberry Pi and an Android device.

Team members: Bhaumik Bhatt, Gavin Joseph, Sanket Nasre and Vishal Nagaonkar. Thanks to Prof. Joe McManus for organizing the Hackathon and guidance on the project.

Summary: Spinning our minds over uses of the Raspberry Pi mini-computer, we decided to create a GPS tracker for the CU Night Ride Service, which provides cars for students to get around in the evening/night. The idea behind it was being able to track the cab drivers using their phones, so the student can locate the cabs over a map and get an idea of it's cab's location/estimated arrival time. We decided to plot the GPS co-ordinates of an Android device placed in a cab and use the Google Maps API to plot that location on a website which will show it on the map. We used the Raspberry Pi as a web-server to host the website and update it with the cab's location website every 5-10 seconds to refresh the GPS co-ordinates.

1) The cab driver will possess an Android phone which will fetch the GPS coordinates every 5 seconds and send it over to the Raspberry Pi web server through a TCP socket connection.

2) The web server will then update the HTML file with the new GPS co-ordinates and update it on the webpage.

Step 1: Prerequisites

All you need to know and use:

1) SL4A - We used the Scripting Layer for Android, SL4A, an open source application that allows programs written in a range of interpreted languages, like Python, to run on Android. It provided us with a high level API to interact with the Android device, and sending data to Raspberry Pi.

2) QPython - We installed QPython, which is a script engine running on Android devices and allows SL4A application projects/scripts to run on them. Download QPython from Google Play.

3) Raspberry Pi - It is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. We used the Pi to integrate with the Android and receive GPS co-ordinates. We used the CanaKit (Available on Amazon).

4) Raspbian - It is a free operating system based on Debian optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. We used noobs for the Raspbian OS on Raspberry Pi B+. Click here for installation instructions.

5) Python modules used - androidhelper, socket and time. Information on androidhelper is available here.

6) Web server - Apache2 - We installed a popular web server application on the Raspberry Pi to allow it to serve web pages. Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages.

7) ALL THE CODE you need is here on Github.

Step 2: Getting the Raspberry Pi Ready

We used the Raspbian image for the Pi as the team was comfortable with a debian-based distribution.

We also used the USB WiFi dongle which came along with the CanaKit for the Pi.

  1. Double-click the WiFiconfig icon on the desktop, which is a GUI based WiFi configuration tool present by default on Raspbian. It assigns an SSID to the Pi based on the network which it connects to gives it an IP address.
  2. You can view that ip address by checking the wlan portion of the output for the ifconfig command and looking at the IPv4 address for the wlan interface. - This will be used in step 4 and 5, so note it down.

Step 3: Web Server on Pi

  1. On a terminal window, type 'sudo apt-get install apache2' to install apache2 webserver.
  2. After that run 'chkconfig httpd on', so that the http daemon will start on-boot henceforth and have the web page will be up and running.
  3. Replace the index.html file in /var/www/ with the one present on the github link.

Description: We now have the web server running on the Pi and the web page is later updated with the GPS co-ordinates received from the Android device.

The web page uses code from the Google Maps API, referenced online using StackOverflow, etc.

Step 4: Receiving Data on the Pi

  1. Modify line number 4 of the file with the IP address obtained from step 2.
  2. Open a terminal window: Run the file using the command: python &.

Now your TCP server is running and ready to collect GPS location data from the Android phone.

Description: The file acts as a TCP server listening on port no. 12345 and receives data from the Android phone (client) and writes it to a file, which is file2 in our case.

Notice the '&'? We use it so that the script runs in the background on the terminal window.

Step 5: Getting Android Ready

  1. Make sure you are on the same WiFi network as the Pi and ensure that the IP address assigned to your Android device is the same for at least the first two parts of the IPv4 address (example: if server IP is a.b.c.d, Android device IP should be a.b.*.*, where '*' means don't care). On your Android device, Go to Settings>About Phone/Tablet>Status and check the IP address field.
  2. Install Qpython on Android.
  3. Get the file from the github link.
  4. Modify line number 9 of the file with the IP address obtained from step 2.
  5. Place the edited file on the sdcard/in-built storage of the Android phone in the com.hipipal.qpyplus/scripts/ directory.
  6. Launch Qpython on your phone and run the script by clicking on the Qpython button in the center and choosing 'Run Local Script', then choosing in the Run Program menu.

Now your Android is connected to the Pi using a TCP socket and is sending GPS location data every 5 seconds.

Description: The IP address problem can be averted but it is better to let the DHCP server assign a common address to both devices as of now, to avoid hassles of making one of the devices have a static IP (to not block internet connectivity).

The file file sits on the Android device and sends network data and GPS co-ordinates, of which, Latitude and Longitude are later parsed by the Raspberry Pi server.

Step 6: Parsing Data and Updating the Web Page

  1. In a terminal, run in the background using the command ./ &.
  2. Now, run, which will run and finally, update the web page and display the updated GPS location on the map, every 10 seconds.

Description: The file, 'file2' received from the will have a lot of redundant data which will need to be removed. This will be done by file which uses a linux golf command - a one liner. Later, we have file that runs the file every 10 seconds and update the html web page with the latest obtained location from 'file'.

Note that: The web page is set to auto-refresh every 10 seconds.

Step 7: Challenges and Future Scope

Challenges we had to complete within 8 hours:

  1. A major challenge was integrating the GPS co-ordinates fetched by the Pi with the Google Maps API. The data received from the Android device was to be plot and tracked continuously using Google Maps service and we took significant time to figure this out.
  2. Getting the socket connection to work over WiFi.
  3. Using the right location fetch functions for Android and debugging on the phone to know if it is receiving and sending correct data.

Future scope:

  1. We demonstrated this as a Proof of Concept only for one cab. Our future scope will to locate and track the multiple cabs.
  2. To make a secure connection (SSL) between the Android device and the Pi to to transfer data securely. (Having an SSL certificate for the Raspberry Pi server for establishing a secure connection over TCP/IP between Android phone and Raspberry Pi can be implemented pretty fast).
  3. To build the Android app with a GUI which will display and track the locations of the cab and other cabs.

Step 8: References