Raspberry Pi Location Tracker




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?


  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


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



  • Audio Contest 2018

    Audio Contest 2018
  • Metalworking Contest

    Metalworking Contest
  • Furniture Contest 2018

    Furniture Contest 2018

34 Discussions

Amazing work, I am going to tested for sure. Thanks

Now i have a new problem... I removed the function and tried to run in without the function.. Finally I got an output. But the output was something like this.

Please help me with this. I am getting a remote and closed connection withoutresponse error. Is this the problem of the server? Can changing the dongle solve the problem?


If I run the code given to me in step 2, it doesn't print anything as written in the code. I have changed the IP Adress according to my 4G WIFI Dongle... is that making a difference? I am also not able to run the program through Command Prompt, so i am using an online Python IDE. Is that the cause of the problem, or is it ok that there is no output on running the code?

1 more answer

You may step through the code, to find out what's going on. Different wifi dongles have different means to get location from cell tower. Please check if dongle admin page show it. An easier but may not be accurate way would be to get location from your IP given by your dongle internet connection.

Any ideas of how to do this with a GPS out of /dev/ttyUSB0 instead of the cell tower?
There is so much info on Google, but none with actually what I want.. .
This is the closest project, but no GPS -- any ideas or thoughts would be really awesome.

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


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?


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

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.

did anyone successfully implement this project?

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!

4 replies

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.

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.


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.

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.

1 reply

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

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?

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

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.

1 reply

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