Friday, February 1, 2019

GPS Tracker with Pi and UV-5R

I wanted to build a cheap, easy APRS tracker with a Raspberry Pi and a Baofeng radio. Fortunately someone already did and left a trail behind! (Thanks to "Midnight Cheese" blog for the bulk of the details. The blog appears dead, so I copied much of the info here to maintain it.)

Setting up GPS

A simple USB GPS receiver will do just fine for this project. I'm using an old Microsoft USB GPS. In order for the Pi to read the GPS data we'll install gpsd.

sudo apt-get install gpsd gpsd-clients

Setting up the local web server

The main PHP script will read the gpsd information through a JSON file. We'll need to install apache and PHP to serve the JSON file locally.
sudo apt-get install apache2 -y
sudo apt-get install php5 libapache2-mod-php5 -y
Drop this file from the gpsd project into the web server directory, /var/www/ and name it gpsd.php

Execute gpsd.php from the command line to build a needed configuration file.
cd /var/www
sudo php gpsd.php
If you open a web browser on your Pi and navigate to http://localhost/gpsd.php?
op=json with your GPS connected and a clear shot of the sky, you should see position information appear. This is where the main PHP script will be reading the GPS data.

Install Python modem

I mentioned earlier I'm not using a traditional APRS application to generate messages. Instead I'm using a simple Python library called afsk modem. Install this using pip.
sudo apt-get install python-pip python-dev
sudo pip install afsk
You may need to add the option: --index "https://....." on older builds of pip.

The optional PyAudio libraries are not required.

This library takes a properly formatted APRS message string as input and generates a Bell 202 AFSK audio sample and AFSK encoded APRS/AX.25 packet.

Make sure the Pi's sound output level is turned up. I set mine to 88.
alsamixer
Also, I turned up the squelch on my UV-5R since there was some interference on 144.39 and it was preventing the VOX from kicking in early enough to catch the entire packet.

And finally, be sure Raspi Config is set to play audio through the 3.5mm port, not HDMI.
sudo raspi-config
The main script

Start gpsd with
gpsd /dev/ttyUSB0
or whatever your port is called. You can use cgps or gpsmon to check for good data. It should start automatically when the USB initializes or is plugged in.

Download the the main PHP script (you may need this one) and place it in your Pi's home folder. You'll want to modify the first two variables entering your callsign and any extra message you want transmitted with your position.

Running this file from the command line will show you what's happening.
php aprs-position-beacon.php
Reading in the GPS data, the script converts that to an APRS message string, runs the message through the modem library, outputs a WAV file and then plays the audio file through the Pi's onboard sound card. That triggers the VOX enabled UV-5R and you're now transmitting your position via APRS over RF.

There are several methods to run this script automatically at startup.

Troubleshooting

I had to run through some troubleshooting, like setting the /dev/ttyUBS0 speed to 4800 with
stty -F /dev/ttyUSB0 4800
I also killed all other instances and removed any old socks.
sudo killall gpsd
sudo rm /var/run/gpsd.sock 
That's it.