Raspberry PI:通过服务器打开 LED On/OFF

Raspberry PI: Turning LED On/OFF via server

我创建了一个服务器/使用 raspberry pi 作为服务器并将 LED 灯连接到 GPIO 7 的面包板...

我正在使用这个脚本:

<html>
 <head>
<?php
if (isset($_POST['RedON']))
{ exec('sudo python /var/www/on.py');
}
if (isset($_POST['RedOFF']))
{
exec('sudo python off.py');
}
?>

  <title>Hub</title>
</head>
<body>
<form method="post">
  <table
 style="width: 75%; text-align: left; margin-left: auto; margin-right: auto;"
 border="0" cellpadding="2" cellspacing="2">
    <tbody>
      <tr>
        <td style="text-align: center;">Turn LED on</td>
        <td style="text-align: center;">Turn LED off</td>
      </tr>
      <tr>
        <td style="text-align: center;"><button name="RedON">Red On</button></td>
        <td style="text-align: center;"><button name="RedOFF">Red Off</button></td>
      </tr>
    </body>
  </table>
</form>
</body>
</html>

但是,当我单击按钮时 ON/OFF 没有任何效果。

如果我 运行 sudo 命令本身,IE: sudo python off.py 它可以工作,但是当把它放在脚本中时它不起作用

你应该在你的表单中使用一个动作,这是很好的做法。

form action="page.php" method="post">

另外,你可以使用print_r($_POST);在页面顶部检查您从您提供的输入中收到的内容

你试过听控制台输出吗?可能您的脚本正在运行,但它没有 sudo pass。

您可以将 php 权限设置为 sudo,但我不会。

尝试将 chmod 777x 和 chown all:all 设置为您的 python 脚本