从php发送序列到arduino

时间:2015-03-20 11:21:49

标签: php mysql c arduino

我遇到了Arduino和php的问题。 我有一个带有index.php页面的网站,我希望Arduino通过串口发送值1,例如,打开LED,值0,使其关闭。

如果我尝试草图,那就完美无缺。如果在串行监视器中发送1或0,则LED将亮起并正常关闭。 问题来自于必须将该页面发送给Arduino的那个页面。 Page(index.php)位于外部服务器上,而不是本地服务器上。 在这个服务器上,我有一个数据库,其中包含一个表,用于更新用户在index.php页面中发送的值。

通过if控制数据库中存在哪个值,如果$ accensione等于1,则运行if中的部分,如果等于0,则运行else。

我想念要插入if中的部分代码,以便发送值Arduino,然后关闭或打开LED。

我在互联网上阅读了很多主题和指南,但我做到了,我真的不知道如何做更多。

我附上我的草图和页面php的代码。 请让别人给我一点帮助,我会发疯的。

我的草图:

#include <SPI.h>
#include <Ethernet.h>

void setup() 
{
  pinMode(3, OUTPUT);  // initialize the green LED pin as an output
  digitalWrite(3, LOW);
  Serial.begin(9600);         // initialize serial communication

}

void loop() 
{
  // Controllo: se la porta seriale sta trasmettendo informazioni...
  if (Serial.available() > 0) 
  {
      char letter = Serial.read();


    if (letter == '1') 
    {
      digitalWrite(3, HIGH);
      Serial.println("LED ON");
    } 
    if (letter == '0') 
    {
      digitalWrite(3, LOW);
      Serial.println("LED OFF");
    }

  }
}

我的PHP页面:

<html>
<head>
        <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
</head>
<body>


 <font color="#000"><u>Status LED, SELECT * FROM arduino</u>
        <br>
        <br>
        <table style="color:#000">

       <th>ID&nbsp;&nbsp;</th>
       <th>Acceso&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
       <?php
$username="***********";
$password="***********";
$database="***********";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database)
   or die( "Impossibile selezionare il database.");


$query = "SELECT * FROM arduino";
$res = mysql_query( $query ) or die( "Attenzione errore: " . mysql_err());

while( $row = mysql_fetch_array( $res ))
{

$turn=$row['acceso'];
echo "L'accensione e' uguale a: $turn \n";
echo "<tr>

      <td> ".$row['id']."</td>
      <td> ".$row['acceso']."</td>
      </tr>";


}

if($turn==1)
{


   // CODE HERE FOR TURN ON LED


}
else
{
  if($turn==0)
  { 

     // CODE HERE TURN OFF LEDE

  }

}


?>                      



</table>

        </font>




<table style="color:#000000">

            <form name="formsend" id="formsend" action="invio.php" method="post">  


<tr>
       <td colspan="2">Set LED status | PHP + MySQL</td>
       <td></td>
      </tr>


      <tr>
       <td>ID: (Readonly)</td>
       <td><input name="id" type="text" id="id" value="1" readonly/></td>
      </tr>

 <tr>
       <td>Acceso: (1 for YES | 0 for NO)</td>
       <td><input name="acceso" id="acceso" type="text"/></td>
      </tr>


      <tr>
       <td colspan="2">

                     <label>&nbsp;</label>
                        <input type="submit" value="Turn On/Off">


           </td>

      </tr>     


      </form>

</table>

<br>
<br>
</body>
</html>

invio.php 是一个简单的php页面,它接收表单数据并对数据库中的表进行更新。

任何人都可以帮助我吗? 非常感谢你的一切,感谢那些帮助我的人!

0 个答案:

没有答案