Python Script GPS Logger: Unterschied zwischen den Versionen
Erscheinungsbild
Keine Bearbeitungszusammenfassung |
|||
| Zeile 96: | Zeile 96: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Pushover=== | ===Pushover=== | ||
nano scripts/push_online.py | |||
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div"> | <syntaxhighlight lang="python" line start="1" highlight="5" enclose="div"> | ||
import httplib, urllib | import httplib, urllib | ||
Version vom 21. März 2015, 10:49 Uhr
Installation
Zusätzliche dienste installieren
sudo apt-get install python-mysqldb
Ordner erstellen
mkdir /home/pi/scripts
Datei erstellen
nano /home/pi/scripts/start.sh
Einfügen:
python /home/pi/scripts/gps_logger.py
Script beim Start ausführen
sudo nano /etc/rc.local
Einfügen
sh /home/pi/scripts/start.sh
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."
Pushover
nano scripts/push_online.py
import httplib, urllib
from subprocess import call
import os
# Application specific variables
application_token = "aDhLdifpd6aTkuuPjLAjYS19hYtQPU"
user_token = "uqsV4ZLmM8q7FjgtbjzyXrUZHheLdx"
# Message specific variables
title = "Logger.02 Online"
message = os.popen("hostname -I").read()
url = "http://www.zeber.li/logger"
# To remove any of these fields you will need to remove them
# from the conn.request() below
# Start your connection with the Pushover API server
conn = httplib.HTTPSConnection("api.pushover.net:443")
# Send a POST request in urlencoded json
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": application_token,
"user": user_token,
"title": title,
"message": message,
"url": url,
}), { "Content-type": "application/x-www-form-urlencoded" })
# Listen for any error messages or other responses
conn.getresponse()
print "Statusmeldung gesendet"