PHP + Arduino

A way to interact with the serial port directly from PHP is there, but I had some problems until I finally got it. Writing, the sent data, is simple and there are a lot of information on the Internet. The problem has been with the writing, since my Arduino program was in infinite loop sending States of their analog pin in Json format.

 
This command runs the commands in the console in this case Windows.
It serves to open the serial port with the given characteristics.
$consola = shell_exec ("mode com4: BAUD = 9600 PARITY = n DATA = 8 STOP = 1 to = off dtr = off rts = off");
We tried to make the com port as a file 
This is the important part for "w +" is the option to work correctly
$fp = fopen("com4", "w+");
fwrite ($fp, "E");
$json = "";
$lectura = "0";
If you cannot read the port gives us error
If (! $fp) {}
   $status = "Not on";
   echo $status;
} else {}
We read 300 words of the port, if instead of "w +" place "r +" reads from the beginning
As this infinite loop does not give us the most up-to-date results.
	   $lectura = fread ($fp, 300);
	   echo "";
	   $json = $lectura;
File sending Arduino shaped I JSON F by it look for the beginning of the JSON and eliminating the above
	   $inicio = strpos($json, "I") + 1;
	   $tamano = strlen ($json);
	   $json = substr ($json, $inicio, $tamano);  
	   $tamano = strlen ($json);
Finally look for the F and all that is within in a JSON in the States
	   $fin = (strpos ($json, "F"));
	   $fin = $fin - $tamano;
	   $json = substr($json,0,$fin);   	   
   echo $json; 
}
fclose ($fp);

Later I will give in detail the Arduino file because find it me interesting the interaction that I have done, which is a protocol to control each PIN directly from an external file in this case PHP and receive when you request the States of each PIN.

Leave a Reply

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.