Recently I’ve been worried about recent car thefts and thought I’d create something to give me some security over the matter. This is why I decided to create a GPS tracker. It’s not the first time I’ve installed a Raspberry Pi in my car, I created a dashcam that used the Raspberry Pi V2 camera and the inbuilt WiFi to act as an AdHoc AP. This allowed me to see what’s happening in front of the car when I wasn’t there and also transfer files as soon as I arrived back at home to my local home server as a USB WiFi dongle was also attached that connected to my home WiFi.
For this project I used;
- USB mobile GSM dongle (I used a Huawei E303).
- USB GPS Dongle (U-Blox 7).
- Raspberry Pi 2 (I had this spare laying around + good processing power).
How it works
The raspberry pi has Debian Jessie installed that is currently the latest OS you can run from the Raspberry Pi website. I chose to have the desktop already installed as it comes in useful when I want to remotely control the pi via VNC. More on this later.
WVDial – Connecting to the GSM network
Setting up the USB GSM Modem was a bit of a pain. I used a piece of software called wvdial that you can install via apt-get. The configuration for this, since I used giffgaff is;
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2
Init3 = AT+CGDCONT=1,"IP","giffgaff.com"
Modem Type = Analog Modem
Baud = 9600
New PPPD = yes
Modem = /dev/ttyUSB0
ISDN = 0
Stupid Mode = 1
Phone = *99#
Password = " "
Username = giffgaff
The config file is located at /etc/wvdial.conf
There’s no private parts retracted from this as it is all generic since the SIM card is what is registered with the network.
Once this was setup you can simply run sudo wvdial and it should dial in and connect fine. You’ll notice if you also run ifconfig -a it created a ppp0 interface that is used to access the internet.
Now, for me when testing, I also had my Ethernet cable connected so in order to test I had to change the default route. To do this, just run;
sudo route del default
sudo route add default ppp0
Now, if you ping google.co.uk or any other site, you should see some times that are quite high due to latency when on the mobile network.
Once I know this was working, I created a little script that launches on startup;
FOUND=`grep "ppp0" /proc/net/dev`
if [ -n "$FOUND" ]; then
echo 'Modem connection established. Adding routes and starting tracker...'
sudo route del default
sudo route add default ppp0
screen -dmS tracker bash -c 'sudo /etc/init.d/nodeTracker start; exec bash'
echo 'Executing WVDial startup command...'
screen -dmS wvdial bash -c 'sudo wvdial'
This could be written better, but I’m no bash expert, but it works.
This script checks to see if the interface ppp0 is available, if not, then it continues with the loop and attempts to start wvdial again. Once wvdial is running an an interface is created, it then starts the tracker application that is written in node.
Setting GPS up on the raspberry pi is a relatively simple task once I knew what I was doing. Simply plug in the USB dongle and identify which port it is using. I found that mine was on /dev/ttyACM0. I found this out because if I ran sudo cat /dev/ttyAMC0 I could see a lot of raw GPS data.
Once this port was identified, then I proceeded onto installing some utilities by doing sudo apt install gpsd gpsd-clients.
Once this has finished installing, proceed to run the following commands, assuming the port you identified was /dev/ttyAMC0 like mine was.
# Doing this prevents problems which require killing gpsd, this just makes life easier.
sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket
# Set the port that the GPS dongle has mounted to.
sudo gpsd /dev/ttyAMC0 -F /var/run/gpsd.sock
# Check to see if the GPS dongle is working
If you have a GPS lock, you will see all the fields populated in that window. You can close it by doing CTRL+C.
The tracker + AWS
At some point I will upload this to github. For now, I’ll explain what I did.
The tracking application is built in NodeJS and uses a node module I come across called node-gpsd. This handles the parsing of the raw GPS data that you saw earlier and returns a constant stream of GPS data in the form of a JSON object. I then setup the necessary IAM roles and policies in AWS so that only the Lambda has permission to read/write to the DynamoDB table and Cloudwatch for logging. I also setup an AWS gateway and added a POST method that is passed to the Lambda that just handles putting the data into dynamo, nothing else.
Here is how it looks in AWS Gateway:
And here’s what the DynamoDB table looks like (with the coordinates retracted):
NOTE: I should note that the tracking application determines whether to send the location by checking to see if the vehicle has moved by matching the previous sent coordinates to the current coordinates. This saves bandwidth and write units on DynamoDB.
You can clone the repositories for both projects from Github.
I will improve this article soon and provide images for you to see what I have done visually and provide the source code for my Lambda and tracking application.
I have tested this system and it works very well, the location is highly accurate!
Please feel free to leave a comment on any questions you may have and i’ll try my best to help you.