0

I have a php script that should (I think) run a python script to control the energenie radio controlled plug sockets depending on which button is selected. It seems to work in that it echos back the correct message when the button is pressed but the python scripts don''t appear to run. I have added the line:

www-data ALL=NOPASSWD: /usr/bin/python /home/pi/lampon.py

which should give the apache user privileges to run the python script at least for turning on the power socket but it doesn't work. The script itself does work when run via the pi command line itself. Any suggestions? (the code for the php is below)

<html>
 <head>
 <meta name="viewport" content="width=device-width" />
 <title>LED Control</title>
 </head>
         <body>
         LED Control:
         <form method="get" action="energenie.php">
                 <input type="submit" value="ON" name="on">
                 <input type="submit" value="OFF" name="off">
         </form>
         <?php
          if(isset($_GET['on'])){
                 shell_exec("python /home/pi/lampon.py");
                 echo "LED is on";
         }
         else if(isset($_GET['off'])){
                 shell_exec("python /home/pi/lampoff.py");
                 echo "LED is off";
         }
         ?>
         </body>
 </html>
ARussell
  • 11
  • 4

2 Answers2

0

Have you tried setting the permissions of the lampoff.py and lampon.py to 777?

chmod 777 /home/pi/lampoff.py && chmod 777 /home/py/lampon.py

Anne Douwe
  • 681
  • 1
  • 5
  • 19
0

I think you need add "sudo" the python script for it to work which means you have to add the www-data user to /etc/sudoers.

shell_exec("sudo python /home/pi/lampon.py");

or

exec("sudo python /home/pi/lampon.py");

There was another post that deal with this recently on Execute Python script from Php

Community
  • 1
  • 1
Matz
  • 371
  • 1
  • 2
  • 5