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)
Pang
10.2k146 gold badges87 silver badges126 bronze badges
1 Answer 1
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);
}
?>
answered Apr 12, 2018 at 3:07
Ricardo
1,3402 gold badges11 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Bradderz
Thanks! I added that line but for some reason the GPIO pin is still not being triggered. Is there anyway I can find out if the script is actually being run or debug the process somehow?
Ricardo
what i recommend you to do is run the python script first (separated) print the values in every function so you can see what is going on. And then that you know the python script works you can call it from php
miztizm
How is secure to post inputs from php to python ?
default