简体   繁体   English

HTML / Php代码,无法使用Raspberry Pi打开灯

[英]Html/Php Code for turning on lights using Raspberry Pi not working

So I have been working on a project to turn on/off LED lights remotely through my Raspberry Pi but I have run into an issue. 因此,我一直在从事通过Raspberry Pi远程打开/关闭LED灯的项目,但是遇到了问题。

My index.html code should work properly but when I press the on or off button, nothing happens. 我的index.html代码应该可以正常工作,但是当我按下开或关按钮时,什么也没发生。 The issue is not with the actual commands ( sudo python /var/www/html/scripts/lights/lampon.py or sudo python /var/www/html/scripts/lights/lampoff.py ) because when I run the same command directly in the raspberry pi terminal, it works. 问题不在于实际命令( sudo python /var/www/html/scripts/lights/lampon.pysudo python /var/www/html/scripts/lights/lampoff.py ),因为当我运行相同的命令时直接在树莓派终端中工作。 And the rest of my code also seems to be correct... So I don't know what the problem is. 我的其余代码也似乎是正确的……所以我不知道问题出在哪里。

The code looks like this: 代码如下:

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

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

NOTE: I am able to run the sudo command above as a normal user and the lights work but when I press the button, it doesn't work (webpage seems to load - so its doing something... but the lights do not turn on). 注意:我能够以普通用户身份运行上面的sudo命令,并且指示灯可以工作,但是当我按下按钮时,它不起作用(网页似乎已加载-因此其正在执行某项操作……但是指示灯没有亮起上)。

You can do it just like this: 您可以这样做:

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

But you need to install Wiring Pi on Raspberry Pi first, to install it : 但是您需要首先在Raspberry Pi上安装Wiring Pi,然后进行安装:

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

I hope that i was useful :) 我希望我有用:)

Well, that's the problem. 好吧,这就是问题所在。

You are using PHP code inside a file called, " Index . html ". 您使用的是被称为“ 指数 。HTML”文件中的PHP代码。

For PHP code to work it has to be in a file called, " Index . php ". 对于PHP代码工作,它必须是在一个名为“ 指数 。PHP的 ”文件。

The important part is that php code goes into files with an extension of .php and the same for html files. 重要的部分是php代码进入扩展名为.php的文件 ,而html文件也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM