Introduction: Raspberry Pi Location Tracker

Picture of Raspberry Pi Location Tracker

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

Picture of 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

Picture of 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

Picture of 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!

Comments

ShifanH (author)2017-10-21

Hi, I am implementing this project into my raspberry pi. I am not using the router login page to get my location. I am just using my IP address which means that the simlocator script is useless to me. The problem I have is that whenever I run main.py, I get an error that is as follows:

program begins

<class 'KeyError'> exception captured

'location'

restarting in 8 seconds

Process finished with exit code 1

"location" is a word in the geolocation.py script that I have modified to work off of IP. I don't know what to do. Can you please help?

Nur Qurratul AinZ (author)2017-05-09

hi,

how to connect this project if i use wifi instead of using dongle?

Nur Qurratul AinZ (author)2017-05-08

hi, nice article !

btw, i have to build some project with rasp pi and google maps route. my title is Lost-item tracking system. if you can gv an idea how can i build this project . tq.

abdistaa (author)2017-02-18

did anyone successfully implement this project?

lukehinton122 (author)2016-04-14

Hi! This is a great project thanks! I am having the following error message when I run the main.py script: <type 'exceptions.Index.Error'> list index out of range

Does anyone know whats causing this problem and how I could correct it? Thanks!

abdistaa (author)lukehinton1222017-02-18

Hi....were u able to complete this project

VikashG2 (author)lukehinton1222016-04-16

Line number of the error should tell what exactly is breaking. Tip- you can execute geolocation.py' getLocation() method separately and see what's going on.

lukehinton122 (author)VikashG22016-04-17

I have tried everything and just get this error. I have no idea what line its on but i'm guessing its to do with the import in simlocator.py? Any help would be appricated.

lukehinton122 (author)VikashG22016-04-17

When running the geolocation.py script is there suppose to be any messages displayed as it runs as if it has no problems then the terminal moves on ready for a new command. Everything in the geolocation.py script looks good. Thanks in advance.

KaushikP23 (author)2017-02-13

3) Also how fast can I see locations on my mobile? Is it in seconds, minutes? Can I track my vehicle on mobile in run time? Or it takes time? Assume i have very good internet connectivity.

KaushikP23 (author)KaushikP232017-02-13

i.e. How much time ideally does one cycle takes? Reading location from cell tower, and converting location into longitude and latitude?

KaushikP23 (author)2017-02-13

1) What is the best resolution that i can get through this solution?

2) Does this work for indoors? Say I want to track where have i parked my car in a mall parking. Say there is good internet 3G/4G connetivity. How will I plot it on google maps? Or can i plot it elsewhere?

IftikharD (author)2016-12-27

Hi MR.Vikas i am beginner whom has use raspberry pi

so pleas help me how to start

and how can i install noobs on my pi bot using lappy

PaoloC68 (author)2016-11-23

Hi! This is a great project!

I have a query, I can not access my 3G dongle web page. How can I configure the file simlocator.py and geolocation.py to get the data. Waiting for your answer.

VikashG2 (author)PaoloC682016-11-25

Hey, yeah it's possible to use only IP to get the location. Please refer my previous comments down below

rrrpatil (author)2016-08-10

This is a great site, way to go.

Can somebody suggest me some 3g dongles with built in GPS modules in it?

Thanks,

Rajesh

abhisheks183 (author)2016-04-26

Hi there

I want to access the gps location of mobile over internet on raspberry pi such that current cell location (as on google maps) can be traced. How can I get this done?

any clues?

VikashG2 (author)abhisheks1832016-06-04

I think you will have to write some Java code to be run inside an Android app that can monitor the mobile's GPS location and send to a destination machine.

If you want to get a Raspberry Pi's location using GPS, there are special h/w modules you can purchase and hook them up with RPi.

JeffM164 (author)abhisheks1832016-05-02

Hi there, I have a ready-made solution for this on Raspberry pi (if I understand correctly). I have a tracker app (runs on mobile web browser) that collects location data and logs it remotely to a web server running on the pi and keeps location files. The paths are stored on the pi and can be plotted with Google maps.

You will have to download and install OliWeb (https://github.com/m2ware/OliWeb) which takes about 5 minutes to do. Once you have it running, you will be able to log location from your mobile anywhere in the world (provided that you have port forwarding enabled to allow you to hit the web server from outside of your home network).

lukehinton122 (author)2016-05-20

Hi guys! When I run the main.py script I get the error except IndexError: List Index Out Of Range

Its really fustrating me as I feel like it is so close to working? Anyone have any ideas? I think its something to do with lat, lon parameters on line 12 of the script.

Any help would be very grateful.

Thanks!

VikashG2 (author)lukehinton1222016-06-04

Hello!

Running main.py runs a series of functions internally

maintain()->getLocation()->getCellTowerInfo()

Run the function that's called first i.e. getCellTowerInfo() on console. If this returns data as expected, move to getLocation() and finally run main.py.

The source code may not run as is, since it depends upon a specific brand of USB dongle. However if you start with testing one function at a time, you will be able to complete it easily. You may have to change a few lines of Python code, so please run some sample programs to get introductory lessons on the programming language. This will help you grasp the source code.

FasiF1 made it! (author)2016-03-16

Hi!

rat project!. But sir the problem I face is that I can't access my 3G dongle's web page .e. 192.168.0.1 (not even 192.168.1.1 and 192.168.2.1) so thereby I cannot get my cell tower's data. I am using an Airtel 3G data card which I installed on my Pi. Please help me with this issue as I am planning to take up this project as my final year project. Thank you

nikhilfeb (author)FasiF12016-03-20

i am getting the same problem with the airtel dongle ... how did you solve it ... pleaseee help

VikashG2 (author)nikhilfeb2016-03-26

Hi!

It's not necessary to get location from raw Cell Tower data. You can try with IP address given by your 3G dongle. Google API will convert that into physical address. Refer

https://www.instructables.com/id/Raspberry-Pi-Locat...

Good luck with DIY !

FasiF1 (author)VikashG22016-03-28

Wow that's great !. What are the changes I am supposed to make in the script to utilise IP address instead of cell tower data?. Help will be greatly appreciated. Thanks alot

VikashG2 (author)FasiF12016-04-16

In geolocation.py you should see this line

payload = {'considerIp':'false','cellTowers':[{'cellId':cellTower,'locationAreaCode':lac,'mobileCountryCode':mcc,'mobileNetworkCode':mnc}]}

Try replacing it with

payload = {'considerIp':'true'}

FasiF1 (author)VikashG22016-04-17

Thanks alot!!

adityat9 (author)2015-12-18

Hi !
I've tried so many times and I got the progress. But, when I ran the main.py it shows "can't define y". so I tried to delete line "x=y" and then I ran it again, and now it shows:

program begins

Logging in to router home page

<class 'requests.exceptions.ConnectionError'> exception captured

('Connection aborted.', error(110, 'Connection timed out'))

my question is, what does it means? and how to fix it?
Thank you very much !

VikashG2 (author)adityat92015-12-20

Hello Aditya,

Please connect RPi to a 3G dongle, login to dongle's router home page e.g. 192.168.01 and check if you see the 'cell tower data' required by Google API on your router page. Steps to get this data programmatically may differ from what I followed. You will need to modify simlocator.py accordingly. You can run the script standalone and see if this working for you.

Hope this helps!

neas (author)VikashG22016-03-11

According to someone I was speaking to in a forum. Not ALL 3G dongles have that router home page. Can you lend some insight into this?

"Link with the comment below"

http://raspberrypi.stackexchange.com/questions/43826/gps-from-cell-tower-gprs-to-pi

Bob Bowie (author)2015-11-16

How precise is the location tracker? Is the accuracy within +/- 1 meter?

VikashG2 (author)Bob Bowie2015-11-17

Accuracy depends upon data provided by Cell Tower and other inputs to the Google API. The API will also return 'accuracy' as part of response.

Without GPS, 1 meter radius accuracy sounds too optimistic to me though.

jupiassa (author)2015-11-03

Nice! I will try using normal ISP internet (I have several RPis in diferent places) so i need to know where is the Pi!

VikashG2 (author)jupiassa2015-11-05

Thanks! Yes you can use your public IP address given by your ISP to get latitude and longitude using Google API. In geolocation.py set 'considerIP' to true and you are all set!

DIY Hacks and How Tos (author)2015-10-18

Great Raspberry Pi project.

About This Instructable

48,214views

75favorites

License:

Bio: IT professional, automation enthusiast
More by VikashG2:Raspberry Pi Location Tracker
Add instructable to: