RPi Quadcopter blog

RPi Quadcopter blog
Click to open

May 28, 2011

Arduino and PHP

To comunicate between arduino and pc, you have many options. Php works really well for sending data to arduino, but I'd say you cannot get data straight from arduino without 100% cpu usage and days of trial and error.

The code below requires "php_serial.class.php". Download it to the same folder where you save this code:

// hello from php.php
 <?php
if(isset($_REQUEST['message'])){
$msg=$_REQUEST['message'];
require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0"); // Arduino usb-port
$serial->confBaudRate(115200);  //baud rate
$serial->confParity("none");  //Parity
$serial->confCharacterLength(8); //Character length  
$serial->confStopBits(1);  //Stop bits
$serial->confFlowControl("none");
$serial->deviceOpen(); // open connection

$serial->sendMessage($msg); //send the message
}
?>
<html><body>
<h3>If you're running Linux: remember to give permissions to www-data to access usbport ($ sudo chmod 777 /dev/ttyACM0)</h3>
<a href="hello_from_php.php?message=HelloFromPhp">Send hello message</a>
</body></html>
If you are not getting any data to Arduino, you must first (and every time you connect usb) give permissions to www-data to access your Arduino usb-port.

Try the example above with "Arduino: How to read serial to string" for best results!

No comments:

Post a Comment