GPS tracker with Raspberry Pi Zero

xddd
4 min readJul 14, 2021

--

I have recently joined a guided tour traveling in the Yellowstone national park. Our group spent quite a bit time driving, going in and out the park numerous times. While I didn’t have to worry about where to go next, I was thinking what if I could log the GPS coordinates of the path I have traveled, and plot them on a map? The map, aside from being cool to show to friends, would become a memoir of places I have traveled, and serve as a reference when I travel to the same location again next time.

It ends up to be easier than I think, since there are already a list of tutorials online on how to hook up a GPS module with a Raspberry Pi. For example, I followed this guide to set up my own system. At the same time, I want a few new features —to list a few, my system should be plug-n-play, and all the logged coordinates should be saved in a database, so later I can plot them on a Google Map.

Here are the things you’ll need, summing up to be ~30 USD.

(Note if you don’t already have it, you may also need some jumper wires, like this one, to connect the pins with each other, as well as soldering tools)

Step 1: Set up your Pi Zero

There are many guides online on how to set up a headless Pi Zero, therefore I will skip this part to avoid repeating those guides.

Step 2: Set up NodeJS environment

We’ll use NodeJS to interact with the GPS module. I followed this guide to install it on my Pi. Try to install the exact versions mentioned in the guide, as the latest version may not be compatible with your Pi.

Step 3: Set up MySQL

We’ll use MySQL to persist all the coordinates. To set it up in Pi Zero, simply run this command in your pi:

sudo apt install mariadb-server-10.0

Step 4: Create a database and necessary authentications

Here’s a sample schema of the table I created for storing the coordinates. I also created a database user to access this table.

Step 5: Connect the GPS module with the Pi

4 pins on the GPS module needs to be connected with the Pi. There are Power (VCC), Ground (GND), Receiver (RXD), Transmitter (TXD). The GPS module from Amazon has those terminals marked clearly on the back. The VCC and GND need to be connected to the 5V and GND pins on the Pi respectively, and the RXD needs to be connected with the UART0_TXD pin on the Pi, and the TXD with the UART0_RXD pin on the Pi, as indicated on this pin map.

Step 6: Write a NodeJS script

In this script, we want a continuous loop to wait and parse the raw data coming from the GPS module (through the serial port), and store the parsed coordinate data into our database. This is the script I created as an example.

Step 7: Run the script when system starts up

We don’t want to log into our headless Pi Zero to start the script manually each time. Instead, we’ll create a new service and let systemd to manage it so it can be automatically started whenever the system starts. A note is that we need to run our script after the mysqld service is started, otherwise we won’t be able to connect to our database. I have also created this file as an example. You need to place your copy in the /etc/systemd/system folder. Once the file is there, run:

sudo systemctl daemon-reload
sudo systemctl enable gps_loc.service
sudo systemctl start gps_loc.service

Step 8: Demo!

I did a test drive along my local highway, and uploaded the coordinates to a website called https://maps.co/gis/. It allows me to plug my coordinates onto a Google map for visualization. I also averaged out the coordinates in every 10s so the total pins are within the limit of the free tier provided by the site. A surprise usefulness I discovered was that the coordinates allowed me to spot congestions — the section where the pins are closer to each other near the SFO was where I slowed down due to traffic.

I’m pretty satisfied with the results. The next time when I travel far, I can simply plug this tiny system to my car’s usb drive, and generate the map when I’m back. Hope you’ll also enjoy doing this little fun project, and let me know if you find other interesting things to do with the system!

--

--

xddd
xddd

Written by xddd

An engineer working in bay area

Responses (1)