Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- # Importamos los modulos necesarios
- import os
- from gps import *
- from time import *
- import time
- import threading
- gpsd = None #Declaramos la variable GPSD
- os.system('clear') #Limpiamos la terminal
- #Este paso es opcional ya que no hace falta que muestre ningun mensaje en la terminal
- #Si ponemos el script como un 'daemon'
- class GpsPoller(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- global gpsd #Declaramos GPSD como una variable global
- gpsd = gps(mode=WATCH_ENABLE) #Iniciamos el streaming de datos GPS
- self.current_value = None
- self.running = True
- def run(self):
- global gpsd
- while gpsp.running:
- gpsd.next() #Esto continuara el loop y recojera todos los datos para limpiar el buffer
- if __name__ == '__main__':
- gpsp = GpsPoller() # Creamos el thread para recibir datos del modulo GPS
- try:
- gpsp.start() # Y lo arrancamos
- while True: #Iniciamos un bucle
- os.system('clear')#Limpiamos la terminal
- #Este paso es opcional ya que no hace falta que muestre ningun mensaje en la terminal
- #Si ponemos el script como un 'daemon'
- print 'Latitud: ' , gpsd.fix.latitude #Sacamos por pantalla la latitud
- print 'Longitud: ' , gpsd.fix.longitude #Sacamos por pantalla la longitud
- print 'Velocidad: ' , gpsd.fix.speed #Sacamos por pantalla la longitud
- #Si la latitud y la longitud son igual a 0.0
- if gpsd.fix.latitude == 0.0 and gpsd.fix.longitude == 0.0:
- #Sacamos por pantalla este mensaje
- print "Esperando GPS..."
- else:
- #En caso contrario sacamos etse mensaje
- print "GPS OK"
- #Guardamos los datos en un archivo
- #(uso 'a' para agregar el texto. si usase 'w' solo se guardarian las ultimas coordenadas)
- data = open("locations.txt", "a")
- data.write("%s,%s\n" % (gpsd.fix.latitude, gpsd.fix.longitude))
- data.close()
- time.sleep(5) #Pausa de 5 segundos entre comprobaciones
- except (KeyboardInterrupt, SystemExit): #Al pulsar ctrl+c
- print "\nDesconectando GPS..."
- gpsp.running = False
- gpsp.join() # Espera a que el thread finalice
- print "Ok.\nSaliendo..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement