gpsData.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /usr/bin/python
  2. # Written by Dan Mandle http://dan.mandle.me September 2012
  3. # http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/
  4. # License: GPL 2.0
  5. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  6. # Preserve this property!
  7. from __future__ import absolute_import, print_function, division
  8. import gps
  9. import os
  10. import threading
  11. import time
  12. gpsd = None # setting the global variable
  13. os.system('clear') # clear the terminal (optional)
  14. class GpsPoller(threading.Thread):
  15. def __init__(self):
  16. threading.Thread.__init__(self)
  17. global gpsd # bring it in scope
  18. gpsd = gps.gps(mode=gps.WATCH_ENABLE) # starting the stream of info
  19. self.current_value = None
  20. self.running = True # setting the thread running to true
  21. def run(self):
  22. global gpsd
  23. while gpsp.running:
  24. # this will continue to loop and grab EACH set of
  25. # gpsd info to clear the buffer
  26. next(gpsd)
  27. if __name__ == '__main__':
  28. gpsp = GpsPoller() # create the thread
  29. try:
  30. gpsp.start() # start it up
  31. while True:
  32. # It may take a second or two to get good data
  33. # print(gpsd.fix.latitude,', ',gpsd.fix.longitude,'
  34. # Time: ',gpsd.utc
  35. os.system('clear')
  36. print()
  37. print(' GPS reading')
  38. print('----------------------------------------')
  39. print('latitude ', gpsd.fix.latitude)
  40. print('longitude ', gpsd.fix.longitude)
  41. print('time utc ', gpsd.utc, ' + ', gpsd.fix.time)
  42. print('altHAE (m) ', gpsd.fix.altHAE)
  43. print('eps ', gpsd.fix.eps)
  44. print('epx ', gpsd.fix.epx)
  45. print('epv ', gpsd.fix.epv)
  46. print('ept ', gpsd.fix.ept)
  47. print('speed (m/s) ', gpsd.fix.speed)
  48. print('climb ', gpsd.fix.climb)
  49. print('track ', gpsd.fix.track)
  50. print('mode ', gpsd.fix.mode)
  51. print()
  52. print("%s satellites in view:" % len(gpsd.satellites))
  53. for sat in gpsd.satellites:
  54. print(" %r" % sat)
  55. time.sleep(5) # set to whatever
  56. except (KeyboardInterrupt, SystemExit):
  57. # when you press ctrl+c
  58. print("\nKilling Thread...")
  59. gpsp.running = False
  60. gpsp.join() # wait for the thread to finish what it's doing
  61. print("Done.\nExiting.")