Created
September 11, 2024 03:50
-
-
Save logston/483a97eec8dc9e196732925015428a9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Read from VK-162 USB GPS Dongle | |
# ie. https://www.amazon.com/VK-162-G-Mouse-External-Navigation-Raspberry/dp/B01EROIUEW | |
# https://www.gpsworld.com/what-exactly-is-gps-nmea-data/ | |
# https://gist.github.com/logston/67e8d1dff12fe194e04812753a871a4f | |
# | |
# Example data: | |
# $GPTXT,01,01,02,u-blox ag - www.u-blox.com*50 | |
# $GPTXT,01,01,02,HW UBX-G70xx 00070000 FF7FFFFFo*69 | |
# $GPTXT,01,01,02,ROM CORE 1.00 (59842) Jun 27 2012 17:43:52*59 | |
# $GPTXT,01,01,02,PROTVER 14.00*1E | |
# $GPTXT,01,01,02,ANTSUPERV=AC SD PDoS SR*20 | |
# $GPTXT,01,01,02,ANTSTATUS=OK*3B | |
# $GPTXT,01,01,02,LLC FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFD*2C | |
# $GPRMC,232047.00,A,3751.53046,N,12207.76361,W,0.795,,080924,,,A*69 | |
# $GPVTG,,T,,M,0.795,N,1.472,K,A*28 | |
# $GPGGA,232047.00,3751.53046,N,12207.76361,W,1,05,1.84,252.3,M,-29.3,M,,*62 | |
# $GPGSA,A,3,03,16,09,04,07,,,,,,,,3.27,1.84,2.71*03 | |
# $GPGSV,3,1,09,01,31,062,27,02,05,200,09,03,75,178,31,04,63,330,21*73 | |
# $GPGSV,3,2,09,07,18,234,28,09,33,297,26,16,41,118,27,26,38,069,08*76 | |
# $GPGSV,3,3,09,31,33,048,08*46 | |
# $GPGLL,3751.53046,N,12207.76361,W,232047.00,A,A*7C | |
# $GPRMC,232048.00,A,3751.53024,N,12207.76363,W,0.657,,080924,,,A*6F | |
from datetime import datetime | |
import subprocess | |
def read_block(device_path: str) -> list[str]: | |
""" | |
Return block of GPS lines from device. | |
""" | |
with open(device_path) as fp: | |
should_break = False | |
block = [] | |
while line := fp.readline(): | |
line = line.strip() | |
if line.startswith('$GPTXT'): | |
continue | |
if line.startswith('$GPRMC'): | |
if should_break: | |
break | |
should_break = True | |
block.append(line) | |
return block | |
def ping(url: str) -> (str, str): | |
try: | |
out = subprocess.run( | |
f"ping -c 1 {url}".split(), | |
capture_output=True, | |
timeout=5, # seconds | |
) | |
stdout = out.stdout.decode() | |
stderr = out.stderr.decode() | |
except subprocess.TimeoutExpired as e: | |
stdout = "" | |
stderr = str(e) | |
return stdout, stderr | |
def main(gps_device_path, url): | |
while True: | |
now = datetime.now().isoformat() | |
gps_block = '\n'.join(read_block(gps_device_path)) | |
stdout, stderr = ping(url) | |
print('=' * 10, flush=True) | |
print(now, flush=True) | |
print('-' * 10, flush=True) | |
print(gps_block, flush=True) | |
print('-' * 10, flush=True) | |
print(stdout, flush=True) | |
print('-' * 10, flush=True) | |
print(stderr, flush=True) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('device_path', help='eg. /dev/cu.usbmodem2101') | |
parser.add_argument('url') | |
args = parser.parse_args() | |
main(args.device_path, args.url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment