Apache PHP命令不会控制Raspberry pi GPIO



好吧,我对Raspberry Pi和Python/PHP/Apache很陌生。我试图让我的PHP脚本运行我创建的一个python文件,通过我的家庭网络激活一个5v中继。在终端中,我可以通过SSH激活Python脚本,它可以控制我的中继。当我尝试在我的网络服务器上通过我的PHP脚本激活中继时,我一无所获。下面是我的PHP脚本和Python脚本。非常感谢您的帮助!

我的Python代码位于/home/pi中,名为"garagedoorleft.py"我的PHP代码位于/var/www/html中,作为"index.PHP"

<html>
<head>
<meta charset="UTF-8" />
<title>Example document</title>
</head>

<?php
if (isset($_POST['LeftOPEN']))
{
exec("sudo python /home/pi/garagedoorleft.py");
}
?>
<form method="post">
<button name="LeftOPEN">Left Door</button>&nbsp;

</form>
</html>

#!/usr/bin/python
# Import required Python libraries
import RPi.GPIO as GPIO
import time
# Use BCM GPIO references instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# init list with pin numbers
pinList = [2]
# loop through pins and set mode and state to 'low'
for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)
def trigger() :
        for i in pinList:
          GPIO.output(i, GPIO.LOW)
          time.sleep(0.5) 
          GPIO.output(i, GPIO.HIGH)
          GPIO.cleanup()

try: 
    trigger()

except KeyboardInterrupt:
  print "  Quit" 
  # Reset GPIO settings
  GPIO.cleanup()

我不确定exec是如何与管道一起工作的,但我认为你可以进行

exec("echo SUDO_PASSWORD_FOR_MACHINE | sudo -S -p '' python my_gpio.py")

这将通过stdin将sudo密码传递给sudo命令。。。我不确定这样做有多安全。。。

或者你可以尝试提升自己的权限

如果python脚本

 import sys,os
 if os.getuid() != 0:  # not sudo so spawn as sudo
    exit(os.system("echo SUDO_PASSWORD_FOR_MACHINE | sudo -S -p '' python %s"%sys.argv))

最新更新