使用Raspberry Pi打开灯的Html/Php代码不工作

2024-06-18 13:20:23 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在做一个项目,通过我的树莓皮远程开关LED灯,但我遇到了一个问题。在

我的索引.html代码应该可以正常工作,但是当我按下on或off按钮时,什么都没有发生。问题不在于实际的命令(sudo python/var/www/html/scripts/lights)/兰彭.pysudo python/var/www/html/scripts/lights/lampoff.py)因为当我直接在raspberry-pi终端中运行同一个命令时,它可以工作。我的其他代码也似乎是正确的。。。所以我不知道问题出在哪里。在

代码如下:

<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>&nbsp;
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>
</html>

任何帮助都将不胜感激。提前谢谢。在

注意:我可以作为一个普通用户运行sudo命令,并且指示灯工作,但当我按下按钮时,它不工作(网页似乎加载-所以它在做一些事情。。。但是灯不亮)。在


Tags: 代码py命令ifvarhtmlwwwscripts
2条回答

你可以这样做:

 <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>

但您需要先在树莓皮上安装接线Pi,才能安装:

^{pr2}$

希望我是有用的:)

好吧,这就是问题所在。在

您正在一个名为“Indexhtml”的文件中使用PHP代码。在

要使PHP代码工作,它必须位于名为“IndexPHP”的文件中。在

重要的部分是php代码放入扩展名为.php的文件中 对于html文件也是如此。在

相关问题 更多 >