Getting GPS to work on Raspberry Pi 2 Model B

From Wiki for Dragino Project
Jump to: navigation, search

Connection

Connect the LoRa/GPS HAT to Rpi, use built-in patch GPS antenna.



Enable the UART

By default the UART is enabled to allow you to connect a terminal window and login, We needed to disable this to free it up for the GPS Module. Edit the boot options to change the UART so it doesnt provide a terminal connection by default: 

sudo nano /boot/cmdline.txt 

Change: 

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 
console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline 
rootwait 

to: 

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 
rootfstype=ext4 elevator=deadline rootwait 

Change inittab so it doesnt spawn a login to the serial connection: 

sudo nano /etc/inittab 

Change: 

 #Spawn a getty on Raspberry Pi serial line 
 T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 

to: 

 #Spawn a getty on Raspberry Pi serial line 
 #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 

If you just want to do a quick check to see what data is coming out of the GPS, you can enter the following command, following by CTRL+C to quit:

 sudo cat /dev/ttyAMA0
cat_ttyAMA0

Run the following two commands to stop and disable the tty service:

 sudo systemctl stop serial-getty@ttyAMA0.service
 sudo systemctl disable serial-getty@ttyAMA0.service



Reboot

 sudo shutdown -r now 



Install GPSD

You can always just read that raw data, but its much nicer if you can have some Linux software prettify it. We'll try out gpsd which is a GPS-handling Daemon (background-helper)

To install gpsd, make sure your Pi has an Internet connection and run the following commands from the console: 

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

Raspbian Jessie systemd service fix

Note if you're using the Raspbian Jessie or later release you'll need to disable a systemd service that gpsd installs.  This service has systemd listen on a local socket and run gpsd when clients connect to it, however it will also interfere with other gpsd instances that are manually run (like in this guide).  You will need to disable the gpsd systemd service by running the following commands:

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

Should you ever want to enable the default gpsd systemd service you can run these commands to restore it (but remember the rest of the steps in this guide won't work!):

 sudo systemctl enable gpsd.socket
 sudo systemctl start gpsd.socket

After disabling the gpsd systemd service above you're ready to try running gpsd manually.

Run gpsd

GPSD needs to be started up, using the following command: 

 sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock



Test gpsd

Now GPS doesn’t work indoors – as it needs a clear view of the sky so for this I’ve placed the PI on the window sill. Next I ssh into the pi and run cgps. There is a simple GPS client which you can run to test everything is working: 

 cgps -

The -s flag is there to tell the command not to write raw data to the screen as well as the processed data.

It may take a few seconds for data to come through, but you should see a screen like this: 

cgps-s

If you have any problems and cgps always displays 'NO FIX' under status and then aborts after a few seconds, you may need to restart the gpsd service. You can do that via the following commands:

 sudo killall gpsd
 sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

If here still shows 'GPS timeout' or 'NO FIX',edit /etc/default/gpsd as below:

 sudo nano /etc/default/gpsd

change it to look like this

START_DAEMON="true"
GPSD_OPTIONS="/dev/ttyAMA0"
DEVICES=""
USBAUTO="true"
GPSD_SOCKET="/var/run/gpsd.sock"

then reboot. CGPS should work then.

NOTE:If the GPS receiver is new, or has not been used for some time, it may need a few minutes or so to receive a current almanac.You need 3 GPS satellites for a 2D fix (i.e. no height) or 4 satellites for a 3D fix. Once fixed,the LED '3D_FIX' will blink.


You can view http://www.catb.org/gpsd/ to get more info about gpsd.You can also try to use the following command:

* cgps

cgps and gpspipe should both just show curated data in the same way as your cat command did.

cgps


Try running gpsmon to get a live-streaming update of GPS data!

* gpsmon

The gpsmon real-time packet monitor and diagnostic tool. (This replaces the sirfmon tool in older versions.)

gpsmon