I am a python newbie, very interested in raspberry pi and while I was crusading the code I had one idea, tried a lot of ways to make it but with no success.
I'm using this python script to get data from a gps receiver and send it to a database:
#! /usr/bin/python
import os
from gps import *
from time import *
import time
import threading
import MySQLdb
import re
conn = MySQLdb.connect(host= "localhost",
user="root",
passwd="root",
db="gps")
gpsd = None #seting the global variable
os.system('clear') #clear the terminal (optional)
time_to_use = strftime("%Y_%m_%d-%H_%M_%S", gmtime())
time_to_use = "track_"+time_to_use
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
self.time = time.time();
def run(self):
global gpsd
while gpsp.running:
gpsd.next()
if __name__ == '__main__':
gpsp = GpsPoller()
try:
gpsp.start()
while True:
if gpsd.fix.mode>1:
x = conn.cursor()
# print gpsd.status
# print 'mode ' , gpsd.fix.mode
try:
x.execute("""INSERT INTO raw_data (`data`, `date`, `track`) VALUES (%s, CURRENT_TIMESTAMP, %s)""",(gpsd, time_to_use))
conn.commit()
except:
conn.rollback()
print 'error on mysql query'
time.sleep(5) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
conn.close()
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print "Done.\nExiting."
exit(0)
I want to start and stop the script from an php file, already tried the next script, but it wont work, the python won't execute.
shell_exec('sudo python /var/www/gpsc.py > /dev/null &');
Can someone help me achieve the expected functionality?
I just want to call start.php and stop.php to start and stop the recording from the gps.
Best regards