Skip to content

Instantly share code, notes, and snippets.

@mpkuse
Last active January 31, 2019 11:10
Show Gist options
  • Save mpkuse/c052674707a7d14d763db166a2a7cfce to your computer and use it in GitHub Desktop.
Save mpkuse/c052674707a7d14d763db166a2a7cfce to your computer and use it in GitHub Desktop.
NMEA0183 (GPS) and Python
import serial
import pynmea2
import collections
def signal_handle( sig, frame ):
# Save JSON
import json
with open( '/home/mpkuse/try/gps_nmea/data.json', 'w' ) as outfile:
json.dump( ARY, outfile )
exit(0)
ser = serial.Serial( '/dev/ttyUSB0', 4800 )
ARY = collections.OrderedDict() # To save the data for reference
import signal
signal.signal( signal.SIGINT, signal_handle )
while True:
print '\n---'
# Get data from the device
line = ser.readline()
print "raw data:", line
# Parse the data
try:
msg = pynmea2.parse( line )
#print 'status=', msg.status,
print 'lat=', msg.lat,
print 'lon=', msg.lon,
#print '#sats=', msg.num_sats,
print 'datetime=', msg.datetime
#ARY.append( (msg.datetime,msg.latitude, msg.latitude_minutes, msg.latitude_seconds, msg.longitude, msg.longitude_minutes, msg.longitude_seconds) )
Q = {}
Q[ 'latitude' ] = msg.latitude
Q[ 'latitude_minutes' ] = msg.latitude_minutes
Q[ 'latitude_seconds' ] = msg.latitude_seconds
Q[ 'longitude' ] = msg.longitude
Q[ 'longitude_minutes' ] = msg.longitude_minutes
Q[ 'longitude_seconds' ] = msg.longitude_seconds
ARY[ msg.datetime.__str__() ] = Q
except:
continue

A demo of how to read nmea devices and some related utils for data manipulation.

# Geojson is a common format used on the internet for plotting geo-cordinates
import json
with open( 'data.json') as f:
data = json.load( f )
cords = []
for k in data.keys():
#print data[k]
lat = float(data[k]["latitude"]) +\
float( data[k]["latitude_minutes"] )/60. +\
float( data[k]["latitude_seconds"] )/3600.
long = float(data[k]["longitude"]) +\
float( data[k]["longitude_minutes"] )/60. +\
float( data[k]["longitude_seconds"] )/3600.
print lat,',', long
cords.append( [long, lat] )
out = {}
out["type"] = "MultiPoint"
out["coordinates"] = cords
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
# Sample Output
# /dev/bsg/6:0:0:0 - VT_SSD_USB_3.0_000000000002-0:0
# /dev/sda - VT_SSD_USB_3.0_000000000002-0:0
# /dev/sda1 - VT_SSD_USB_3.0_000000000002-0:0
# /dev/sg0 - VT_SSD_USB_3.0_000000000002-0:0
# /dev/input/event2 - LiteON_HP_Basic_USB_Keyboard
# /dev/hidraw0 - LiteON_HP_Basic_USB_Keyboard
# /dev/input/event3 - PixArt_USB_Optical_Mouse
# /dev/input/mouse0 - PixArt_USB_Optical_Mouse
# /dev/hidraw1 - PixArt_USB_Optical_Mouse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment