Python Script GPS Logger
Erscheinungsbild
import os
from gps import *
import time
import threading
import MySQLdb
import sys
gpsd = None #seting the global variable
os.system('clear') #clear the terminal (optional)
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
time.sleep(1) #set to whatever
os.system('clear')
gpsd.fix.latitude, gpsd.fix.longitude, gpsd.utc,' + ', gpsd.fix.time, gpsd.fix.altitude, gpsd.fix.eps, gpsd.fix.epx, gpsd.fix.epv, gpsd.fix.ept, gpsd.fix.speed, gpsd.fix.climb, gpsd.fix.track, gpsd.fix.mode
#print "https://maps.google.de/maps?q=",gpsd.fix.latitude,gpsd.fix.longitude,"(",time.localtime()[3],":",time.localtime()[4],":",time.localtime()[5],")&t=h&z=17&om=0"
#print time.localtime()[3],":",time.localtime()[4],":",time.localtime()[5]
#Connect to MySQL Server
try:
db = MySQLdb.connect(
host = "194.126.200.54",
user = "zeberlie_raspber",
passwd = "Xe-2UY*7Me?8G",
db = "zeberlie_raspberry"
)
except Exception as e:
sys.exit('keine verbindung zur Datenbank')
#time.sleep(29) #set to whatever
cursor = db.cursor()
now = time.localtime(time.time())
ALT =gpsd.fix.altitude
SPE =gpsd.fix.speed
TRA =gpsd.fix.track
os.environ['TZ'] = 'Europe/Amsterdam'
NAME =time.strftime("%d %b %H:%M:%S", time.localtime())
ADDRESS =ALT
LAT =gpsd.fix.latitude
LNG =gpsd.fix.longitude
TYPE ="track"
cursor.execute('INSERT INTO markers (NAME, ADDRESS, ALT, SPE, TRA, LAT, LNG, TYPE)VALUES (%s,%s,%s,%s,%s,%s,%s,%s)',
(NAME, ADDRESS, ALT, SPE, TRA, LAT, LNG, TYPE))
#cursor.execute('SELECT * FROM markers')
result = cursor.fetchall()
#if result:
#for z in result:
#print z[1]+': '+z[2]
time.sleep(1) #set to whatever
#Wile wiederhohlen
sys.exit('keine verbindung zur Datenbank')
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
#print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
#print "Done.\nExiting."