Last active
August 29, 2015 14:20
-
-
Save CiscoKidxx/7e086ed3e872964503ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# Written by Chris Newell, May 2015 | |
# License: GPL 3.0 | |
from gps import * | |
import os | |
import time | |
import threading | |
import Adafruit_BMP.BMP085 as BMP085 | |
import sqlite3 | |
import ftplib | |
############################### DB STUFF | |
sqlite_file = 'hud_db_master.sqlite3' # name of the sqlite database file | |
table_name = 'hud' # name of the table to be created | |
id_field = 'id' # name of the ID column | |
conn = sqlite3.connect(sqlite_file) | |
c = conn.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS hud (time blob, temp real, latitude real, longitude real, altitude real, pressure real, speed real, track real, id_field integer)''') | |
############################### FTP STUFF | |
server = '192.168.1.2' | |
session = ftplib.FTP(server, 'anonymous', '') | |
file = str(sqlite_file) | |
############################## BMP STUFF | |
sensor = BMP085.BMP085() | |
celcius = int(sensor.read_temperature()) | |
farenheit = 9.0 / 5.0 * celcius + 32 | |
feet = 3.28084 * int(sensor.read_altitude()) | |
############################## GPS STUFF | |
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 | |
################################## Execute | |
if __name__ == '__main__': | |
gpsp = GpsPoller() # create the thread | |
try: | |
gpsp.start() # start it up | |
id_field = 0 | |
while True: | |
temp = "{0}".format(farenheit) | |
gps_lat = gpsd.fix.latitude | |
gps_lon = gpsd.fix.longitude | |
altitude = "{0:0.2f}".format(feet) | |
pressure = "{0:0.2f}".format(sensor.read_pressure()) | |
sea_pressure = "{0:0.2f}".format(sensor.read_sealevel_pressure()) | |
speed = gpsd.fix.speed | |
track = gpsd.fix.track | |
climb = gpsd.fix.climb | |
print "[+] Gathering Data..." | |
time.sleep(1) | |
os.system('clear') | |
c.execute("INSERT INTO hud VALUES (DateTime('now'), ?, ?, ?, ?, ?, ?, ?, ?);", (temp, gps_lat, gps_lon, altitude, pressure, speed, track, id_field)) | |
print "[+] Added row: ", (temp, gps_lat, gps_lon, altitude, pressure, speed, track, id_field) | |
# set to whatever | |
send_file = open(file, 'rb') | |
session.storbinary('STOR {}'.format(file), send_file) | |
send_file.close() | |
print "[+] Sent DB to {}".format(server) | |
time.sleep(1) | |
os.system('clear') | |
id_field += 1 | |
conn.commit() | |
except (KeyboardInterrupt, SystemExit): # when you press ctrl+c | |
print "\n[-] Killing Thread..." | |
gpsp.running = False | |
gpsp.join() # wait for the thread to finish what it's doing | |
print "[-] Closing Database..." | |
conn.close() | |
session.quit() | |
print "Done.\n[-] Exiting." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment