Introduction: 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
- Raspberry Pi, memory card to install OS
- 3G/4G USB dongle supporting plug and play on Ubuntu/Raspberry Pi
- A sim with data connection
- Optionally wifi USB dongle, USB power bank
- 5V /1A power point from a running vehicle
Software
- Raspbian or any other supported OS installed on Raspberry Pi
- Google Maps Geolocation API private key
- Private key to upload coordinates on data.sparkfun.com or any other online storage
- Basic knowledge of computer programming to understand Python, JavaScript and HTML code
- 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:
- 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.
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.
Attachments
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.
Attachments
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.
Attachments
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!
45 Comments
10 months ago
Hi, I've trying to do this but without raspi. I connect my pc to my phone hotspot so my pc is in the same network with my phone. I try to get lac, mnc, mcc, cell id from my phone using network cell info. When i try to do http request from my pc, i got accuracy result around 800-1500m from Geolocation API. The location remain same eventough i move to other location with my pc. So, does Geolocation API really work in your case?
Question 2 years ago on Step 1
Can plz tell me range???
Answer 2 years ago
Cell tower location was within a few meters. IP geolocation will depend a lot. Just test it out by google, ‘ what’s my IP location’.
2 years ago
Hi,
I tried to run this project but the http://192.168.0.1 page does not open, neither in my laptop nor in the raspberry pi. I tried other combinations as well, but it does not open.
How can I run this without opening this router homepage?
I am using usb dongle with CDMA sim card in raspberry pi 4
Also, if I wanted to use only the ip address, what codes should I modify?
Setting considerIp: true does not work?
Thanks for the project.
Reply 2 years ago
Hello,
The 192.168.0.1 IP address is specifically for Macromax brand dongle. Your brand may have a different router home page. You can check the router home page by several tools. One such tool is Fing mobile app. You can use this to scan the network and find the home page, I find it very handy. For location precision, if you can tap into cell tower data using dongle, it would be better. If you are okay with geolocation data, just google, ‘what’s my IP location’. You should be able to map IP address to a physical location.
6 years ago
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
Reply 3 years ago
Raspberry pi home page is very good starting point. They have all the documentation to start with. I also learned from there.
https://www.raspberrypi.org
6 years ago
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?
Reply 3 years ago
To plot on google maps, code is there. You can create an app to plot it on your mobile
6 years ago
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.
Reply 3 years ago
It's less than a minute but not real time. Yes I have been to track in run time..
Reply 6 years ago
i.e. How much time ideally does one cycle takes? Reading location from cell tower, and converting location into longitude and latitude?
5 years ago
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?
Reply 3 years ago
Yes you can rely on IP address for geolocation. See if you can step through
Question 5 years ago on Step 6
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.
Answer 3 years ago
Does the h/w used for GPS have any documentation you can follow. Sorry about that, I never tried it!
4 years ago
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?
Reply 3 years ago
Please try to debug it through
4 years ago
Amazing work, I am going to tested for sure. Thanks
Question 4 years ago on Step 2
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?