Arduino+Raspberry PI+PHP

Hi,

Project: Php program that light on a led connected to the Arduino.

So I followed a few tutorials: php_serial.class.php+phpprogram.php+arduinoprogram.ino

I tried the same Arduino code with a python program and it works. Not fot PHP.

The Arduino reacts everytimes a execute the program , but the led doesn't light on.

For the php_serial.class.php, they only thing that needs to be changed is the serial port(I think):

	function deviceSet ($device)
	{
		if ($this->_dState !== SERIAL_DEVICE_OPENED)
		{
			if ($this->_os === "linux")
			{
				if (preg_match("@^COM(\d+):?$@i", $device, $matches))
				{
					$device = "/dev/ttyACM0" . ($matches[1] - 1);
				}

				if ($this->_exec("stty -F " . $device) === 0)
				{
					$this->_device = $device;
					$this->_dState = SERIAL_DEVICE_SET;
					return true;
				}

As for the php program :

 <?php
include("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");

$serial->confBaudRate(9600); //Baud rate: 9600 
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1") 
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") 
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") 
$serial->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none.

//Now we "open" the serial port so we can write to it
$serial->deviceOpen();

$serial->sendMessage("*1" ); //sleep(1); // echo "hi"; $serial->deviceClose();

?>

Arduino code :

int OLight1 = 5 ;



void setup(){
  // Open serial connection.
  Serial.begin(9600);
  pinMode(OLight1, OUTPUT);
  
}

void loop(){ 

  
  if(Serial.available() > 0){      // if data present, blink
    digitalWrite(OLight1, HIGH);
    delay(500);            
    digitalWrite(OLight1, LOW);
    delay(500); 
    digitalWrite(OLight1, HIGH);
    delay(500);           
    digitalWrite(OLight1, LOW);
    
  }
}

Any idea ? Thank you

Opening the serial port resets the Arduino. Sending serial data before the Arduino is ready is a good way to ensure that the data is lost.

Once the data actually gets to the Arduino, don't you suppose that reading it would be a good idea?

It actually works by adding this : sleep(3);

Serial.available() > 0 is enought to initiate the blink. Once it receives data(whatever the size), it starts.

 <?php
include("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");

$serial->confBaudRate(9600); //Baud rate: 9600 
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1") 
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") 
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") 
$serial->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none.

//Now we "open" the serial port so we can write to it
$serial->deviceOpen();

sleep(3); ADDED CODE

$serial->sendMessage("*1" ); //sleep(10); // echo "hi"; $serial->deviceClose();

?>

Serial.available() > 0 is enought to initiate the blink. Once it receives data(whatever the size), it starts.

Sure, but if you actually read the serial data, you could see whether blinking should start or stop, or which pin should blink, or what the PWM value for a pin should be, or...

I tried the same Arduino code with a python program and it works. Not fot PHP.

If your python program has no time.sleep() between open and write, u might use python php bridge to bridge python into php. I highly doubt is the case.

http://pecl.php.net/package/python

sleep(3) is not solution but Band-Aid.