Skip to content

Instantly share code, notes, and snippets.

@sloev
Last active March 8, 2022 12:03
Show Gist options
  • Select an option

  • Save sloev/5de3ed6bc5d3e78f656029071eb16409 to your computer and use it in GitHub Desktop.

Select an option

Save sloev/5de3ed6bc5d3e78f656029071eb16409 to your computer and use it in GitHub Desktop.
gpspipe log time with micros and lat lon, python3 only
curl 'https://gist.githubusercontent.com/sloev/5de3ed6bc5d3e78f656029071eb16409/raw/728aee9e9a88281d7bcb398f2408aa5051632c1d/gpslog.py' > ~/gpslog.py 
    
curl 'https://gist.githubusercontent.com/sloev/5de3ed6bc5d3e78f656029071eb16409/raw/728aee9e9a88281d7bcb398f2408aa5051632c1d/gpslog.sh' > ~/gpslog.sh 
    
curl 'https://gist.githubusercontent.com/sloev/5de3ed6bc5d3e78f656029071eb16409/raw/728aee9e9a88281d7bcb398f2408aa5051632c1d/gpslog.service' > ~/gpslog.service

sudo cp ~/gpslog.service /etc/systemd/system/gpslog.service
sudo systemctl enable gpslog.service
sudo systemctl restart gpslog.service

import subprocess
import json
import io
import sys
import time
import logging
logging.basicConfig(level=logging.INFO)
logging.info("starting gpspipe error tolerant loop")
logging.info("output format is\niso_time,lat,lon")
while True:
try:
proc = subprocess.Popen(['gpspipe','-wul','--zulu'],stdout=subprocess.PIPE)
logging.info("starting gpspipe...")
printed_first_gps_log = False
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"):
if not line:
break
if "TPV" in line and "lat" in line:
t, d = line.split(": ")
d = json.loads(d)
output = f"{t},{d['lat']},{d['lon']}"
sys.stdout.write(output+"\n")
sys.stdout.flush()
if not printed_first_gps_log:
logging.info(f"got first gps pipe log: {output}\noutputing them to stdout...")
printed_first_gps_log=True
exit_code = proc.wait()
if exit_code != 0:
raise RuntimeError(f"gpspipe exited with: exit_code")
except KeyboardInterrupt:
break
except:
logging.exception("gpspipe errored, waiting 1 second and trying again")
time.sleep(1)
[Unit]
Description=gps log service
[Service]
Type=simple
Restart=always
RestartSec=3
ExecStart=/bin/bash /home/pi/gpslog.sh
[Install]
WantedBy=multi-user.target
#!/bin/bash
#/bin/python3 /home/pi/gpslog.py >> /mnt/data/gps.log
/usr/bin/gpspipe -Ruv --zulu | grep '$GP' --line-buffered | sed -u "s/: /,/g" >> /mnt/data/gps.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment