Skip to content

Instantly share code, notes, and snippets.

@wolfg1969
Last active December 15, 2023 08:06
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save wolfg1969/4653340 to your computer and use it in GitHub Desktop.
Save wolfg1969/4653340 to your computer and use it in GitHub Desktop.
python-gps sample codes, Raspberry Pi 4 + u-blox GPS module (uart)

Raspberry Pi 4 + u-blox GPS module (uart)

$ sudo apt-get update
$ sudo apt-get install gpsd gpsd-client python-gps

Enable the serial interface and disable serial login shell with raspi-config then reboot. Confirm /dev/ttyS0 exists

$ ls -l /dev/serial*
lrwxrwxrwx 1 root root 5 May  9 10:38 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 May  9 10:38 /dev/serial1 -> ttyAMA0

Configure gpsd to use /dev/ttyS0 in /etc/defaults/gpsd then restart gpsd service. Use cgps command to make sure it works.

# Deprecate. The latest gps python module has been changed significantly.
import gps, os, time
session = gps.gps()
while 1:
os.system('clear')
session.query('admosy')
# a = altitude, d = date/time, m=mode,
# o=postion/fix, s=status, y=satellites
print
print ' GPS reading'
print '----------------------------------------'
print 'latitude ' , session.fix.latitude
print 'longitude ' , session.fix.longitude
print 'time utc ' , session.utc, session.fix.time
print 'altitude ' , session.fix.altitude
print 'eph ' , session.fix.eph
print 'epv ' , session.fix.epv
print 'ept ' , session.fix.ept
print 'speed ' , session.fix.speed
print 'climb ' , session.fix.climb
print
print ' Satellites (total of', len(session.satellites) , ' in view)'
for i in session.satellites:
print '\t', i
time.sleep(3)
#! /usr/bin/python
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0
# Updated by wolfg1969 https://guoyong.dev May 2020
import os
from gps import *
from time import *
import time
import threading
gpsd = None #seting the global variable
os.system('clear') #clear the terminal (optional)
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
# It may take a second or two to get good data
# print(gpsd.fix.latitude,', ',gpsd.fix.longitude,' Time: ',gpsd.utc)
os.system('clear')
print
print(' GPS reading')
print('----------------------------------------')
print('latitude ', gpsd.fix.latitude)
print('longitude ', gpsd.fix.longitude)
print('time utc ', gpsd.utc, ' + ', gpsd.fix.time)
print('altitude (m)', gpsd.fix.altitude)
print('eps ', gpsd.fix.eps)
print('epx ', gpsd.fix.epx)
print('epv ', gpsd.fix.epv)
print('ept ', gpsd.fix.ept)
print('speed (m/s) ', gpsd.fix.speed)
print('speed (mph) ', gpsd.fix.speed * 2.237)
print('climb ', gpsd.fix.climb)
print('track ', gpsd.fix.track)
print('mode ', gpsd.fix.mode)
print
print('sats ', gpsd.satellites)
time.sleep(5) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print("\nKilling Thread...")
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print("Done.\nExiting.")
@wolfg1969
Copy link
Author

@ewe-akj Do you wanna a paid support for you requirements?

@wolfg1969
Copy link
Author

What version of gpsd does this target? There is no query() in my gps object under Python 2.7 on Ubuntu 16.04 (arm64) .

Use Python 3 and gpsdData.py.

@wolfg1969
Copy link
Author

Thank you for this. Is there a way to get the speed in km/h instead of m/s?? I've tried taking "gpsd.fix.speed" value and then converting that into km/h. But unfortunately that wasn't fruitful, I tried something like this

speed = gpsd.fix.speed
km_h = ((speed*18)/5)

@dear-stan The formula is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment