Skip to content

Instantly share code, notes, and snippets.

@TheSkallywag
Created August 27, 2018 23:36
Show Gist options
  • Save TheSkallywag/23aa8bf182655a20ac1e61eefece0023 to your computer and use it in GitHub Desktop.
Save TheSkallywag/23aa8bf182655a20ac1e61eefece0023 to your computer and use it in GitHub Desktop.
Python script to connect to a GPSD daemon and print the latest coordinate datapoints to the console.
###############################################################
# File: gps_continuous.py
# Description: Connect to a GPSD daemon and print the latest
# coordinate datapoints to the console.
#
# Author: Carlos Garcia-Saura (@CarlosGS) - 2018
# Modified by: Author (email) - year
###############################################################
import gpsd
import time
gpsd.connect() # No arguments = local GPSD daemon
prevTime = 0
while True:
res = ""
try: # Poll the GPSD daemon for the latest data
packet = gpsd.get_current()
# Check the timestamp and discard duplicate points
if packet.time == prevTime: continue
prevTime = packet.time
res += str(packet.mode) + ","
res += str(packet.sats) + ","
if packet.mode >= 2: # 2D position locked
res += str(packet.lat) + ","
res += str(packet.lon) + ","
res += str(packet.track) + ","
res += str(packet.hspeed) + ","
res += str(packet.time) + ","
res += str(packet.error['c']) + ","
res += str(packet.error['x']) + ","
res += str(packet.error['v']) + ","
res += str(packet.error['t']) + ","
res += str(packet.error['s']) + ","
res += str(packet.error['y']) + ","
if packet.mode >= 3: # 3D position locked
res += str(packet.alt) + ","
res += str(packet.climb) + ","
except:
res += "Initializing... "
res += str(time.time())
print(res)
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment