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.
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.
- 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.
- 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
- On a terminal window, type ‘sudo apt-get install apache2' to install apache2 webserver.
- 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.
- 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
- Modify line number 4 of the server-socket.py file with the IP address obtained from step 2.
- Open a terminal window: Run the server-socket.py file using the command: python server-socket.py &.
Now your TCP server is running and ready to collect GPS location data from the Android phone.
Description: The server-socket.py 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.
For more detail: PiDroidGPSTracker