0

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
asked Apr 12, 2018 at 2:38

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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
How is secure to post inputs from php to python ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.