1

So I've a Raspberry Pi 4, and I've connected a push button to it's GPIO, and the python file is works fine if I execute it from the Terminal.

I get NULL if I execute my Python file from the web site.

So far, I want to achieve that; I want to execute the Python program if I push the button on the webpage, and if I push the button which is connected to the GPIO I want the result on the web page "The time was: %s, when the button was pushed."

Here is the python code:

#!/usr/bin/env python
import sys
import datetime
time=datetime.datetime.now()
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
def button_callback(channel):
    print("The time was: %s, when the button was pushed." % (time))
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback)
message =raw_input("Press Enter to exit.\n\n")

And I have made a PHP file, which looks like this:

<html>
 <body>
  <head>
   <title>
     Webpage
   </title>
  </head>

   <form method="post">

    <input type="submit" value="go" name="go">
   </form>
 </body>
</html>

<?php
        if(isset($_POST['go']))
        {
                $output = shell_exec("python sudo /var/www/html/button.py");
                var_dump($output);
        }
?>

Thanks for all of the advices.

TinM
  • 51
  • 8
  • Isn't it `sudo python /var/www/html/button.py`? And see https://stackoverflow.com/questions/5652986/php-sudo-in-shell-exec – Ocaso Protal Dec 17 '19 at 10:48
  • Sadly no, I've tried it with, and without sudos, I already tried `chmod +x button.py` – TinM Dec 17 '19 at 10:53
  • Any error messages in the log files of your webserver? – Ocaso Protal Dec 17 '19 at 11:02
  • Traceback (most recent call last): File "/var/www/html/button.py", line 10, in GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) RuntimeError: Not running on a RPi! This is the only one I can find. – TinM Dec 17 '19 at 11:22

0 Answers0