Sailing Pi - GPS tracking on your boat

By Russell Barnes. Posted

Track and map your sailing adventure, wherever you go

My weekly sailing crew asked for a way to visualise where we had been, and for a live display of the true speed over ground to see whether we were fighting tides. This project delivers both, without relying on internet connections or cell phone data. Add a battery and it would also be useful for car rallies or cyclists. All you need is a Raspberry Pi Zero W with some additional hardware, Python, and Mathematica!

The full article can be found in The MagPi 59 and was written by Bill Ballard.

You'll need

  • Raspberry Pi Zero W with case and GPIO header
  • Ultimate GPS breakout
  • Pimoroni Scroll pHAT
  • 40-pin stacking header for the Scroll pHAT
  • USB to micro USB cable for power
  • 12 V USB power adapter suitable for a car


Solder the dual male header to the Raspberry Pi Zero W, and solder the stacking header to the Scroll pHAT. Cut off the header wires except for pins 4, 6, 8, and 10. Double-check the pin numbering before you do this! Bend these four pins through 90 degrees so that they point outwards.

Take four wires of different colours, about 10 cm long, and solder them into the GPS breakout Vin, GND, TX, and RX connectors, bringing the wires in from the top of the board. Next, solder the Vin wire to pin 4 of the GPIO, GND wire to pin 6, RX wire to pin 8, and TX wire to pin 10. There should be no crossover wires.

Software setup

Boot up your Raspberry Pi Zero without the GPS attached. Go to Preferences > Raspberry Pi Configuration. In the Interfaces tab, be sure to enable SSH, Serial, and I2C. In the System tab, you will also ensure the Boot option is ‘To CLI’ (command line interface). Next, set up the wireless and reboot.

Now load some special software for the Scroll pHAT and the GPS:

sudo apt-get install python-scrollphat gpsd gpsd-clients python-gps

The GPS uses the TX/RX pins that are the console defaults, so you need to make some modifications to the system:

sudo nano /boot/cmdline.txt

Remove the console=serial0,115200 portion of the line. Then save the file and exit the editor.

sudo systemctl stop serial-getty@AMA0.service
sudo systemctl disable serial-getty@AMA0.service
sudo halt

When the system stops, remove the power and install the scroll pHAT with the GPS breakout. Power the system up. The Fix light on the GPS board will blink once every second with no satellite fix, and every 15 seconds with a fix. If you don’t have a fix, move the system to a window with a good view of the southern sky (northern sky in the southern hemisphere) where the antenna can see the satellites.

While you are acquiring a lock, we need to disable the standard gpsd socket. In a Terminal window, type:

sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket


Download the Python program and Mathematica notebook LatLonPlot.nb from GitHub. The system will not have a live internet connection while you are sailing, and so it will not know the date straight away. As a result, the program waits for the GPS to get a fix and uses the UTC date information to generate file names with the current sailing date. Automate everything so that the program starts at boot time:

sudo nano /etc/rc.local

Add the following two lines to the file just before the exit 0 line:

gpsd /dev/ttyS0 -F /var/run/gpsd.socket
python /home/pi/ > /home/pi/gpsd-boat.log 2> /home/pi/gpsd-boat.err &

Then save and exit. The last line will start the Python program as a background job, redirect output to a log file, and redirect any error messages to an error file for later debugging.

We are sailing

Now go sailing, or perhaps driving. A 12 V automotive USB plug works in a car or in the boat, in one of the many 12 V outlets. Above deck these are likely to be corroded, so be prepared to clean the contacts (Scotch Brite scouring pads work well), and keep the Raspberry Pi and GPS in a plastic bag to ward off water. Power everything up and go for a sail. When you’ve finished sailing, unplug the system and take it home.

When you get home and have internet access again, connect the Raspberry Pi to a monitor and keyboard. The CSV output file is designed to be easily read with Mathematica. However, because we crashed the Pi to power it off on the boat, you will need to edit the file with nano and remove the last line or two, which will contain some garbage. If the application crashed and restarted at any point, there will be extra headers you should search for and delete. These files were written as root, so, where 2017-05-04 is replaced by your activity date:

sudo nano 2017-05-04-latlon.csv

Remove the last few lines and any errant blank lines or additional headers. Then save the file and exit.

Start Mathematica and use the LatLonPlot.nb notebook you downloaded to visualise your sail, but replace the date with the date identifier for your file. The first line of the file imports the data from the comma-delimited file and loads the header and data separately. The output of this command should be a partial list of all the latitude and longitude pairs. The second line converts the latitude-longitude data into a GeoPosition set of variables, and then a GeoPath construct for plotting. The output of this should be a small graph of the path taken, but with no map. The third line places the GeoPath on an automatically sized map and places the output in the file image.jpeg. PlotStyle controls the colour and thickness of the sailing path plot.

Hit SHIFT+ENTER to force an evaluation and wait a while (the header will show running), particularly if it was a long sail. It takes quite some time for Mathematica to load the map data over the internet, so be patient. When the calculation is complete, open the image.jpeg file to have a look.

 Where have you been today?

From The MagPi store


Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.