Interfacing A GPS Module With Raspberry Pi

Published by frenoy on

This is part 2 of the dashcam project and in this post, we will learn how to interface a GPS module to the Raspberry Pi. We will then use the GPS data and add it to the video as a text overlay.

The video below gives you more information about GPS modules, communication along with an overview of how everything comes together. I would recommend watching it first before continuing.

Step 1: Enable Serial Port

We first need to SSH into the board and then enable the serial port. This can be done by running the following command:

  • sudo raspi-config

This will open up the configuration tool and you can use the arrows to navigate to the interfacing options, and then serial. Select NO to the console and then YES to the hardware serial port. Use the TAB key to navigate to the Finish option and then select NO when it asks you if you want to reboot. This will take you back to the terminal. Enter the following command so that we can shut down the board:

  • sudo shutdown -h now
Wiring
Connections

Once the board has shut down, we can connect the GPS module to the board using the reference image.

Step 2: Test the GPS Module

We will now test the GPS module to make sure it is working correctly. That can be done by running the following command:

  • sudo cat /dev/serial0
GPS Data

You should be able to see some text output starting with “$GP….” as seen in the image. This is data from the GPS module and it means that the serial communication is working as it should. You can press “CTRL+Z” to stop the output.

We then need to disable the “getty” service as it might interfere with the serial communication. This can be done by running the following commands.

Step 3: Write the Final Script

Before we write the final script, we need to run a few commands. First, we need to install the python-serial module which can be done by running the following command:

  • sudo apt install python-serial

We will be using the pynmea2 module to make sense of the GPS data and that can be installed by running the following command:

  • sudo pip install pynmea2

We also need to install psutil for the root user and this can be done by running the following command:

  • sudo pip install psutil

Finally, we can create a new script file using the following command:

  • sudo nano dashcam2.py

You can then copy the following contents and paste it in the script as seen in the video.

import picamera
import os
import psutil
import serial
import pynmea2
import time
from picamera import Color

MAX_FILES = 999
DURATION = 20
SPACE_LIMIT = 80
TIME_STATUS_OK = 0.5

file_root = "/home/pi/videos/"
port = "/dev/serial0"

def parseGPS(str):
	if str.find('GGA') > 0:
	   msg = pynmea2.parse(str)
	   print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Fix: %s --  Satellites: %s" % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units, msg.gps_qual,msg.num_sats)

if(psutil.disk_usage(".").percent > SPACE_LIMIT):
	print('WARNING: Low space!')
	exit()

serialPort = serial.Serial(port, baudrate = 9600, timeout = 0.5)

with picamera.PiCamera() as camera:
	camera.resolution = (1920,1080)
	camera.framerate = 30

	print('Searching files...')
	for i in range(1, MAX_FILES):
		file_number = i
		file_name = file_root + "video" + str(i).zfill(3) + ".h264"
		exists = os.path.isfile(file_name)
		if not exists:
			print "Search complete"
			break

	for file_name in camera.record_sequence(file_root + "video%03d.h264" % i for i in range(file_number, MAX_FILES)):
		timeout = time.time() + DURATION
		print('Recording to %s' % file_name)

		while(time.time() < timeout):
			gps_str = serialPort.readline()
			if gps_str.find('GGA') > 0:
				msg = pynmea2.parse(gps_str)
				camera.annotate_background = Color('black')
				camera.annotate_text = "TME:%s LAT:%s %s LON:%s %s ALT:%s %s SAT:%s CPU:%s" % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units, msg.num_sats, psutil.cpu_percent())
			
			time.sleep(TIME_STATUS_OK)
			if(psutil.disk_usage(".").percent > SPACE_LIMIT):
				print('WARNING: Low space!')
				break;

Dashcam With GPS

Running The Final Script

Once this is done, save the file by typing “CTRL+X”, then Y, then ENTER. You can then test the script by running the following command:

  • sudo python dashcam2.py

The script will then run as it should and you can use FileZilla to obtain the video files like last time. The video files will contain an overlay with the GPS data along with the CPU usage.

Video With GPS Overlay

The dashcam project is not yet over and there will be a follow-up post to continue this series. Do consider subscribing to our YouTube channel as this help support us.

This is the GPS test script that was mentioned in the video:

import serial
import pynmea2

port = "/dev/serial0"

def parseGPS(str):
	if str.find('GGA') > 0:
		msg = pynmea2.parse(str)
		print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Fix: %s --  Satellites: %s" % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units, msg.gps_qual,msg.num_sats)

serialPort = serial.Serial(port, baudrate = 9600, timeout = 0.5)
while True:
	str = serialPort.readline()
	parseGPS(str)

GPS Test

Thank you for reading!