[英]Html/Php Code for turning on lights using Raspberry Pi not working
因此,我一直在從事通過Raspberry Pi遠程打開/關閉LED燈的項目,但是遇到了問題。
我的index.html代碼應該可以正常工作,但是當我按下開或關按鈕時,什么也沒發生。 問題不在於實際命令( sudo python /var/www/html/scripts/lights/lampon.py或sudo python /var/www/html/scripts/lights/lampoff.py ),因為當我運行相同的命令時直接在樹莓派終端中工作。 我的其余代碼也似乎是正確的……所以我不知道問題出在哪里。
代碼如下:
<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 /var/www/html/scripts/lights/lampon.py");
}
if (isset($_POST['LightOFF']))
{
exec("sudo python /var/www/html/scripts/lights/lampoff.py");
}
?>
<form method="post">
<button class="btn" name="LightON">Light ON</button>
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>
</html>
任何幫助,將不勝感激。 提前致謝。
注意:我能夠以普通用戶身份運行上面的sudo命令,並且指示燈可以工作,但是當我按下按鈕時,它不起作用(網頁似乎已加載-因此其正在執行某項操作……但是指示燈沒有亮起上)。
您可以這樣做:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
LED Control:
<form method="get" action="gpio.php">
<input type="submit" value="ON" name="on">
<input type="submit" value="OFF" name="off">
</form>
<?php
$setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out");
if(isset($_GET['on'])){
$gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 1");
echo "LED is on";
}
else if(isset($_GET['off'])){
$gpio_off = shell_exec("/usr/local/bin/gpio -g write 17 0");
echo "LED is off";
}
?>
</body>
</html>
但是您需要首先在Raspberry Pi上安裝Wiring Pi,然后進行安裝:
$ sudo apt-get install git-core
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build
我希望我有用:)
好吧,這就是問題所在。
您使用的是被稱為“ 指數 。HTML”文件中的PHP代碼。
對於PHP代碼工作,它必須是在一個名為“ 指數 。PHP的 ”文件。
重要的部分是php代碼進入擴展名為.php的文件 ,而html文件也是如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.