Introduction: Raspberry Pi Location Tracker

About: IT professional, automation enthusiast

In this Instructable I will walk you through the steps to build a DIY location tracker system running inside a Raspberry Pi without GPS. RPi can be connected to any moving vehicle, making a cheap option to track your vehicle or any moving object for that matter in real time on Google Maps. Tracking is done by using sim card's cell tower location.

What you need?

Hardware

  1. Raspberry Pi, memory card to install OS
  2. 3G/4G USB dongle supporting plug and play on Ubuntu/Raspberry Pi
  3. A sim with data connection
  4. Optionally wifi USB dongle, USB power bank
  5. 5V /1A power point from a running vehicle

Software

  1. Raspbian or any other supported OS installed on Raspberry Pi
  2. Google Maps Geolocation API private key
  3. Private key to upload coordinates on data.sparkfun.com or any other online storage
  4. Basic knowledge of computer programming to understand Python, JavaScript and HTML code
  5. Optionally a weaved.com account to control Raspberry Pi over internet

Each step will build a logical entity of the system and you can vary it according to your need and resources available.

Step 1: Setup Raspberry Pi to Run on Moving Vehicles

While on the move, first thing you need is power supply. Raspberry Pi needs 5V and around 1A as power input. Please check the exact specifications for your model.

You can use a mobile charger/ USB input in your in-car entertainment system or any other source - just watch out for output reading. If you are not sure, you may measure with a multi meter. It's safer to use a USB power bank that can draw uninterrupted power supply.

Secondly you need a location provider. You have at least 2 options:

  1. Connect a 2G/3G/4G USB dongle directly as shown in the image. Advantage is that you don't need additional hardware, however you loose the benefit of getting additional data in terms of wifi access points which as shown in subsequent steps is used by Google Geolocation API to return location details.
  2. Alternatively, connect a USB wifi dongle to Raspberry Pi. Wifi dongle should have access permanent access to a hotspot

This completes our hardware setup.

Step 2: Get Location From Cell Tower/IP Address

It's now turn for doing some software programming. The first step is to get location of the moving Raspberry Pi using cell tower data from USB dongle.

If you google, ' what's my location' you will notice that HTML5 based web sites will ask for permission to share your location. They use a combination of cell tower data, wifi access points, IP address, etc. to get the location.

Getting the same data in an non-browser application on Raspberry Pi is not that easy though. Following steps are specifically for Macromax brand USB dongle, but after completion of this step you will get understanding of how to get this data in your locator system.

As shown in the image, configuration page http://192.168.0.1 shows location access code and cell tower ID. Pressing F12 on browser shows network calls and data being sent/received from client(the browser) and server(the dongle). Client-Server communication can be emulated using Python. Code to get and parse this data is in Simlocator.py. Just play around with the code and see it goes for you.

Apart from cell tower data, you can also try getting location based on your IP address. If that gives you good enough results, you can ignore getting data from cell tower and fetch location directly based on IP address as we'll see in next step.

Step 3: Convert Location to Latitude/longitude

To get latitude/longitude data, there are various web based geolocation services. I used Google Maps Geolocation API as I found it easy to use and the per day limit is enough for this system. All you need to do is signup to get a private key. The key is passed to the API along with input parameters like IP address, cell tower, wifi access points. The result from API is a pair of latitude/longitude. You should send as many inputs as available to get accurate results. It all depends upon the location where you are in and how much data you can gather from USB internet dongle/wifi dongle connected to Raspberry Pi. You need to test what works best for you.

Replace the placeholder for private key with your key in the attached code.

Step 4: Upload Location Data

To store geolocation data you need an online storage e.g. Sparkfun. Uploading data requires a few lines of code as shown in Publisher.py. Just like with Google API, you will have to get a private key from Sparkfun to upload data.

It's up to you to choose another storage provider.

Lastly, we need to put together all the Raspberry Pi side code. As you can see in the attached main.py, the main logic is to get cell tower location from USB dongle, send it to Google API to convert it into lat/long pair and save the location on Sparkfun server. These steps are run inside a loop. Before running again, there is a sleep period. If the location has not changed since last time, it's not uploaded to Sparkfun.

Complete source code for Raspberry Pi Location Tracker is present on GitHub.

Step 5: Controlling Raspberry Pi When It's on the Move

During development and testing your location tracker, you may want to login to Raspberry Pi and check if you are getting the desired results when it's on the move.

I used Weaved service as suggested on raspberrypi.org to control it over internet. At the time of writing signing up with weaved.com is free and it allows to stay connected with your Raspberry Pi for 30 minutes.

While on the move, there are chance of loosing signals which means your SSH connection might get disconnected. One of the ways to get over this problem is to use a utility package called screen. You can install it by running sudo apt-get install screen . Run screen command first and then run your comamnds. Now if the session gets disconnected say due to network connectivity, you can still get onto that session by running screen-r command.

This marks the completion of hardware and software setup required on the Raspberry Pi side. Next we'll look at the client side code to show last few locations on Google Maps.

Step 6: Client- Display Locations in Browser

Google Maps APIs expect a latitude and longitude pair to show the location on Google Maps in a browser window. Map.html has the JavaScript and HTML code for it.

As shown in the screenshot, last few locations are fetched from Sparkfun and shown as Google Markers. You can also track the distance from a starting point say your home, college or work place. You need to give the latitude and longitude of the starting point in map.html. Upon hovering mouse over a marker, it shows the local time when the position was recorded and distance from starting point.

Apart from showing the location on a browser, you can display it on Google Maps app on your smartphone or may be desktop based application. Coding is not yet done for it. You are welcome to contribute over GitHub!