I'm trying to set up a system that when a number will be given input into a web page, it will trigger a timer and turns on a GPIO pin of RPI for a few seconds then turns off.
I need to pass a number that is submitted into a HTML form, into my python script to work with.
I thought I found a solution but I'm not sure why its not working. I'm running apache2 on a Pi zero W with PHP7.
Here are the two files I have in the /var/www/html/ directory:
index.php:
<form action="index.php" method="post">
<input type="text" name="seconds"/>
<input type="submit" name="SubmitSeconds" /><br/>
</form>
<?php
if(isset($_POST['SubmitSeconds'])) {
shell_exec('/usr/bin/python /var/www/html/script.py'.$seconds);
}
?>
script.py:
#!/usr/bin/python
from datetime import datetime
from threading import Timer
import RPi.GPIO as GPIO
import time, cgi, sys
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
def GPIOTimer():
GPIO.output(23, 1)
time.sleep(10)
GPIO.output(23, 0)
def ten_seconds():
global x
global y
x = datetime.today()
y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute), second=(x.second+10), microsecond=0)
def five_mins():
global x
global y
x = datetime.today()
y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+5), second=(x.second), microsecond=0)
def ten_mins():
global x
global y
x = datetime.today()
y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+10), second=(x.second), microsecond=0)
def thirty_mins():
global x
global y
x = datetime.today()
y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+30), second=(x.second), microsecond=0)
def one_hour():
global x
global y
x = datetime.today()
y = x.replace(day=(x.day), hour=(x.hour+1), minute=(x.minute), second=(x.second), microsecond=0)
def three_hours():
global x
global y
x = datetime.today()
y = x.replace(day=(x.day), hour=(x.hour+3), minute=(x.minute), second=(x.second), microsecond=0)
def TimerMain():
global x
global y
global delta_t
global secs
global t
delta_t=y-x
secs = delta_t.seconds+1
t = Timer(secs, GPIOTimer)
t.start()
def TimerSequence(z):
if (z==1):
ten_seconds()
elif (z==2):
five_mins
elif (z==3):
ten_mins()
elif (z==4):
thirty_mins()
elif (z==5):
one_hour()
elif (z==6):
three_hours()
else:
print("InputError")
TimerMain()
result = int(sys.argv[1])
TimerSequence(result)
1) You forget to declare the variable "seconds"
<form action="index.php" method="post">
<input type="text" name="seconds"/>
<input type="submit" name="SubmitSeconds" /><br/>
</form>
<?php
if(isset($_POST['SubmitSeconds'])) {
$seconds = $_POST["seconds"];
shell_exec('/usr/bin/python /var/www/html/script.py'.$seconds);
}
?>