Use php to control arduino

I have problems with Arduino and php. I have a website with a page index.php, I want Arduino via serial would send the value 1 for example, to turn on the LED, and the value 0 to make it turn off.

If I try my sketches, works perfectly. If in the serial monitor send 1 or 0, the LED turns on and turns off properly. The problem comes when it has to be that page to send the value to Arduino. Page (index.php) is on an external server and not in local. On this server I have a database which contains a table that updates the value that is sent by the user in the index.php page.

Through an if then control which value is present in the database, and if $accensione equals 1 run the part inside the if, if equal to 0, the else.

I miss the part of the code to be inserted inside the if to send the values Arduino and in turn to turn off or turn on the LED.

I have read many topics and guides on the internet, but I did it and I do not know really how to do more.

I am attaching my sketch and the code of the page php. Please let someone give me a little help, I’m going crazy.

My Sketch:

#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");
    }

  }
}

My PHP page:

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

        

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





</body>
</html>

invio.php is a simple php page that takes the form data and make an update on the table in the database.

Can anyone help me ?? Thank you so much for everything and thanks to those who help me !

THANKS AGAIN! :slight_smile:

I want Arduino via serial would send the value 1 for example, to turn on the LED, and the value 0 to make it turn off.

Why would SENDing a value FROM the Arduino make an LED turn on or off?

The problem comes when it has to be that page to send the value to Arduino. Page (index.php) is on an external server and not in local.

The PHP script can send data VIA SERIAL to the Arduino ONLY when the Arduino is connected to the server that the script is running on.

How i can connect arduino to the server? Thanks!

wolfphp22:
How i can connect arduino to the server?
Thanks!

The same way you connect it to your PC to upload a sketch.

If the Arduino is wearing an ethernet shield, then the possibilities open way up.