0

When I directly execute the script it is running (means LED turned on), but when I write script to click on button and than LED on it is not running (means LED is not turned on). I had also made this on.py file in that cgi-bin folder and give all permission to it. Here the first script is running perfectly but second is not running. Both scripts are nearly same, only difference is button code in PHP portion. Where is the problem in second script, I don't know ... Please help me! Thanks in advance!

1)index.php

<html>
<body>
<form method="POST" action="">
   <p>
    <input type="button" name="on" value="ON" /><br>
    <input type="button" name="off" value="OFF" />
   </p>
</form>

<?php
exec("sudo python /usr/lib/cgi-bin/on.py");
?>

</body>
</html>
----------------------------------
2)index.php
---------------------------------
<html>
<body>
<form method="POST" action="">
   <p>
    <input type="button" name="on" value="ON" /><br>
    <input type="button" name="off" value="OFF" />
   </p>
</form>

<?php
if ( isset( $_POST["on"] ) ) {
exec("sudo python /usr/lib/cgi-bin/on.py");
}
?>

</body>
</html>
Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
snehal desai
  • 119
  • 5
  • 23
  • What is your code inside `on.py`? – Aaron Christiansen Mar 25 '16 at 12:38
  • #!/usr/bin/env python import subprocess import time import RPi.GPIO as GPIO #import cgitb # Comment out when debugged. #cgitb.enable() # Comment out when debugged. print "Content-type: text/html\n\n"; print """led pigpio cgi"""; print '

    LED Flash

    '; GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(35,GPIO.OUT) #Turn LED on GPIO.output(35,GPIO.HIGH) time.sleep(1) print 'stopped by user'; print 'cleaning up GPIO pins'; print 'all done'; print ''; this is my code inside on.py file in cgi-bin folder..
    – snehal desai Mar 26 '16 at 03:43
  • PHP is not being used correctly here. PHP does not run after the page has been loaded- it runs on the server side (see [this](http://stackoverflow.com/questions/23258088/can-php-run-after-the-page-is-loaded), for example). You should be using Javascript. The reason that the first works is that the python file is called as soon as you load the page no matter what. The second does not do anything to reload the page, so the script is never called. – Alex Mar 27 '16 at 00:14

0 Answers0