Advertisement
Guest User

Eo

a guest
Aug 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1.  
  2. #! /usr/bin/python
  3. # Importamos los modulos necesarios
  4. import os
  5. from gps import *
  6. from time import *
  7. import time
  8. import threading
  9.  
  10. gpsd = None #Declaramos la variable GPSD
  11. os.system('clear') #Limpiamos la terminal
  12. #Este paso es opcional ya que no hace falta que muestre ningun mensaje en la terminal
  13. #Si ponemos el script como un 'daemon'
  14.  
  15. class GpsPoller(threading.Thread):
  16.     def __init__(self):
  17.         threading.Thread.__init__(self)
  18.         global gpsd #Declaramos GPSD como una variable global
  19.         gpsd = gps(mode=WATCH_ENABLE) #Iniciamos el streaming de datos GPS
  20.         self.current_value = None
  21.         self.running = True
  22.  
  23.     def run(self):
  24.         global gpsd
  25.         while gpsp.running:
  26.             gpsd.next() #Esto continuara el loop y recojera todos los datos para limpiar el buffer
  27.  
  28.  
  29. if __name__ == '__main__':
  30.     gpsp = GpsPoller() # Creamos el thread para recibir datos del modulo GPS
  31.     try:
  32.         gpsp.start() # Y lo arrancamos
  33.         while True: #Iniciamos un bucle
  34.  
  35.  
  36.             os.system('clear')#Limpiamos la terminal
  37. #Este paso es opcional ya que no hace falta que muestre ningun mensaje en la terminal
  38. #Si ponemos el script como un 'daemon'
  39.  
  40.             print 'Latitud: ' , gpsd.fix.latitude #Sacamos por pantalla la latitud
  41.             print 'Longitud: ' , gpsd.fix.longitude #Sacamos por pantalla la longitud
  42. print 'Velocidad: ' , gpsd.fix.speed #Sacamos por pantalla la longitud
  43.  
  44.  
  45.             #Si la latitud y la longitud son igual a 0.0
  46.             if gpsd.fix.latitude == 0.0 and gpsd.fix.longitude == 0.0:
  47.             #Sacamos por pantalla  este mensaje
  48.                 print "Esperando GPS..."
  49.             else:
  50.             #En caso contrario sacamos etse mensaje
  51.                 print "GPS OK"
  52.                 #Guardamos los datos en un archivo
  53.                 #(uso 'a' para agregar el texto. si usase 'w' solo se guardarian las ultimas coordenadas)
  54.                 data = open("locations.txt", "a")
  55.                 data.write("%s,%s\n" % (gpsd.fix.latitude, gpsd.fix.longitude))
  56.                 data.close()
  57.  
  58.             time.sleep(5) #Pausa de 5 segundos entre comprobaciones
  59.  
  60.     except (KeyboardInterrupt, SystemExit): #Al pulsar ctrl+c
  61.         print "\nDesconectando GPS..."
  62.         gpsp.running = False
  63.         gpsp.join() # Espera a que el thread finalice
  64.     print "Ok.\nSaliendo..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement