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.

4
  • Isn't it sudo python /var/www/html/button.py? And see stackoverflow.com/questions/5652986/php-sudo-in-shell-exec Commented Dec 17, 2019 at 10:48
  • Sadly no, I've tried it with, and without sudos, I already tried chmod +x button.py Commented Dec 17, 2019 at 10:53
  • Any error messages in the log files of your webserver? Commented Dec 17, 2019 at 11:02
  • Traceback (most recent call last): File "/var/www/html/button.py", line 10, in <module> 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. Commented Dec 17, 2019 at 11:22

0

Your Answer

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.