SHARE
TWEET

gps-tracker.py

DeaD_EyE Jan 11th, 2019 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. '''
  4. https://raspberry.tips/projekte/gps-tracking-mit-dem-raspberry-pi
  5. '''
  6.  
  7. from subprocess import call, PIPE
  8. from time import sleep
  9. from datetime import datetime as dt
  10. from pathlib import Path
  11. import logging
  12.  
  13.  
  14. logging.basicConfig()
  15. logger = logging.getLogger(__file__)
  16.  
  17.  
  18. def terminate():
  19.     '''
  20.    Terminate existing gpsd instances and
  21.    start gpsd and sleep a few seconds
  22.    '''
  23.     ret_val1 = call(['sudo', 'killall', 'gpsd'], stderr=PIPE, stdout=PIPE)
  24.     ret_val2 = call(['sudo', '/usr/sbin/gpsd', '/dev/ttyAMA0', '-F', '/var/run/gpsd.sock'], stderr=PIPE, stdout=PIPE)
  25.     if ret_val1 or ret_val2:
  26.         raise Exception('Could not kill gpsd or start gpsd')
  27.     sleep(30)
  28.  
  29.  
  30. def refresh_time():
  31.     '''
  32.    Refresh the local time
  33.    '''
  34.     call(['sudo', 'service', 'ntp', 'restart'])
  35.     sleep(3)
  36.  
  37.  
  38. def make_filename():
  39.     '''
  40.    output file name with actual date
  41.    '''
  42.     now = dt.now().replace(microsecond=0).isoformat()
  43.     return Path('/home/pi/') / (now + '-gps_track')
  44.  
  45.  
  46. def start():
  47.     '''
  48.    start gpspipe and output
  49.    the GPS Data as NMEA to file
  50.    '''
  51.     filename = make_filename()
  52.     call(['gpspipe', '-d', '-l', '-r', '-o', filename], stderr=PIPE, stdout=PIPE)
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     logger.setLevel(logging.INFO)
  57.     exc = lambda err: logger.critical(err)
  58.     try:
  59.         logger.info('Terminating process')
  60.         terminate()
  61.     except Exception as err:
  62.         exc(err)
  63.     try:
  64.         logger.info('Refreshing time')
  65.         refresh_time()
  66.     except Exception as err:
  67.         exc(err)
  68.     try:
  69.         logger.info('Start reading process')
  70.         start()
  71.     except Exception as err:
  72.         exc(err)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top