【问题标题】:Html/Php Code for turning on lights using Raspberry Pi not working使用 Raspberry Pi 开灯的 Html/Php 代码不起作用
【发布时间】:2017-08-28 01:32:36
【问题描述】:

所以我一直在做一个项目,通过我的 Raspberry Pi 远程打开/关闭 LED 灯,但我遇到了一个问题。

我的 index.html 代码应该可以正常工作,但是当我按下开或关按钮时,什么也没有发生。问题不在于实际命令(sudo python /var/www/html/scripts/lights/lampon.pysudo 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>&nbsp;
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>
</html>

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

注意:我能够以普通用户身份运行上面的 sudo 命令,并且灯可以工作,但是当我按下按钮时,它不起作用(网页似乎正在加载 - 所以它正在做某事......但是灯不要打开)。

【问题讨论】:

  • 我想你的 php 用户没有 sudo 权限。我猜当您直接在以 root 或其他管理员用户身份登录的终端上运行这些命令时?
  • 不...当我运行命令时,我以普通用户的身份执行它并且它可以工作...

标签: php python html raspberry-pi home-automation


【解决方案1】:

你可以这样做:

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

但是你需要先在树莓派上安装 Wiring Pi,才能安装它:

$ sudo apt-get install git-core
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build

我希望我是有用的:)

【讨论】:

    【解决方案2】:

    嗯,这就是问题所在。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2018-01-04
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      相关资源
      最近更新 更多