我试图从这个PHP代码调用python脚本但它不起作用。这是一个简单的代码,它有一个开/关按钮。这用于打开/关闭覆盆子pi的gpio引脚。当我单独运行它时,python脚本工作正常。但是php代码无法执行python脚本。代码是:
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<?php
if (isset($_POST['LightON']))
{
exec("sudo python /home/pi/lighton_1.py");
}
if (isset($_POST['LightOFF']))
{
exec("sudo python /home/pi/lightoff_1.py");
}
?>
<form method="post">
<button class="btn" name="LightON">Light ON</button>
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>
</html>
*我没有代码。我是从youtube视频中获取的。
这是Python代码:
#!/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 = [15]
# loop through pins and set mode and state to 'low'
for i in pinList:
GPIO.setwarnings(False)
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
def trigger() :
for i in pinList:
GPIO.output(i, GPIO.HIGH)
# GPIO.cleanup()
break
try:
trigger()
except KeyboardInterrupt:
print " Quit"
# Reset GPIO settings
GPIO.cleanup()