gpsData.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #! /usr/bin/python3
  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. # Codacy D203 and D211 conflict, I choose D203
  8. # Codacy D212 and D213 conflict, I choose D212
  9. """gpsd data dump example."""
  10. from __future__ import absolute_import, print_function, division
  11. import gps
  12. import os
  13. import threading
  14. import time
  15. gpsd = None # setting the global variable
  16. os.system('clear') # clear the terminal (optional)
  17. class GpsPoller(threading.Thread):
  18. """Class to poll gpsd"""
  19. def __init__(self):
  20. """Initializer for GpsPoller."""
  21. threading.Thread.__init__(self)
  22. global gpsd # bring it in scope
  23. gpsd = gps.gps(mode=gps.WATCH_ENABLE) # starting the stream of info
  24. self.current_value = None
  25. self.running = True # setting the thread running to true
  26. def run(self):
  27. """Run."""
  28. global gpsd
  29. while gpsp.running:
  30. # this will continue to loop and grab EACH set of
  31. # gpsd info to clear the buffer
  32. next(gpsd)
  33. if __name__ == '__main__':
  34. gpsp = GpsPoller() # create the thread
  35. try:
  36. gpsp.start() # start it up
  37. while True:
  38. # It may take a second or two to get good data
  39. # print(gpsd.fix.latitude,', ',gpsd.fix.longitude,'
  40. # Time: ',gpsd.utc
  41. os.system('clear')
  42. print()
  43. print(' GPS reading')
  44. print('----------------------------------------')
  45. print('latitude ', gpsd.fix.latitude)
  46. print('longitude ', gpsd.fix.longitude)
  47. print('time utc ', gpsd.utc, ' + ', gpsd.fix.time)
  48. print('altHAE (m) ', gpsd.fix.altHAE)
  49. print('eps ', gpsd.fix.eps)
  50. print('epx ', gpsd.fix.epx)
  51. print('epv ', gpsd.fix.epv)
  52. print('ept ', gpsd.fix.ept)
  53. print('speed (m/s) ', gpsd.fix.speed)
  54. print('climb ', gpsd.fix.climb)
  55. print('track ', gpsd.fix.track)
  56. print('mode ', gpsd.fix.mode)
  57. print()
  58. print("%s satellites in view:" % len(gpsd.satellites))
  59. for sat in gpsd.satellites:
  60. print(" %r" % sat)
  61. time.sleep(5) # set to whatever
  62. except (KeyboardInterrupt, SystemExit):
  63. # when you press ctrl+c
  64. print("\nKilling Thread...")
  65. gpsp.running = False
  66. gpsp.join() # wait for the thread to finish what it's doing
  67. print("Done.\nExiting.")