I have read through a bunch of forums and still no luck.. I have a php file that executes a python file (liveSensor.py). It runs the py file once and stops, how to I keep the file open in the background? If I run sudo python liveSensor.py in the command line it just runs once as well but if I put sudo python -i liveSensor.py it stays open. How to I do this in php?
php file -
<?php
$try = exec('python sensor.py');
print_r ($try);
?>
In the liveSensor.py file I have 2 vibration sensors that are being detected and then passing that info into sqlite, then my php file retrieves that data and using ajax I can display it. All of this works great except launching the py file continually..
py script -
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import sqlite3
KnockPin = 12
ShockPin = 13
inning = 1
runs = 0
strikes = 0
balls = 0
print ("Welcome to Bases Loaded")
def setstr():
global strikes
strikes = 0
global balls
balls = 0
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(KnockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(ShockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def knock(ev=None):
with sqlite3.connect('basesLoaded.db') as conn:
global strikes
global inning
strikes += 1
if strikes ==0:
pass
elif strikes == 1:
conn.execute("UPDATE bl set STRIKE = 1 WHERE ID =1");
print ("Strike 1 :", conn.total_changes);
elif strikes == 2:
conn.execute("UPDATE bl set STRIKE = 2 WHERE ID=1");
print ("Strike 2 :", conn.total_changes);
elif strikes == 3:
global inning
inning +=1
conn.execute("UPDATE bl set STRIKE = 0 WHERE ID=1");
conn.execute("UPDATE bl set INNING = inning WHERE ID=1");
print ("Strike 3 Your Out :", conn.total_changes);
setstr()
print ("Inning Number :", inning);
def shock(ev=None):
with sqlite3.connect('basesLoaded.db') as conn:
global balls
balls += 1
if balls ==0:
pass
elif balls == 1:
conn.execute("UPDATE bl set BALL = 1 WHERE ID=1");
conn.commit()
print ("Ball 1 :", conn.total_changes);
elif balls == 2:
conn.execute("UPDATE bl set BALL = 2 WHERE ID=1");
conn.commit()
print ("Ball 2 :", conn.total_changes);
elif balls == 3:
conn.execute("UPDATE bl set BALL = 3 WHERE ID=1");
conn.commit()
print ("Ball 3 :", conn.total_changes);
elif balls == 4:
global runs
runs += 1
conn.execute("UPDATE bl set BALL = 0 WHERE ID=1");
conn.execute("UPDATE bl set RUN = 'runs' WHERE ID=1");
conn.commit()
print ("Run Scored! score is :", runs);
setstr()
def register_callbacks():
GPIO.add_event_detect(ShockPin, GPIO.FALLING, callback=shock, bouncetime=2500)
GPIO.add_event_detect(KnockPin, GPIO.FALLING, callback=knock, bouncetime=2500)
if __name__ == '__main__':
try:
setup()
register_callbacks()
except KeyboardInterrupt:
destroy()
This returns my 'Welcome to Bases Loaded' message in the browser but the py file wont stay open to detect the sensors.