Arduino and PHP

Hi again :slight_smile:
So I got my Arduino yesterday, played with it the whole day and managed to control it through PHP. All I do is send a string through the serial port which is then compared in Arduino, and if the string is "on" it lights up the LED on pin13. So I managed to get it to work just as I wanted through the Serial Monitor, I send the "on" string and the LED lights up. In PHP however I send the "on" string but the LED just blinks once and that's all. Could someone help me out here?

Here is the code for the Arduino:

String val;
void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() 
{
while (Serial.available()) 
  {
    delay(10);  //small delay to allow input buffer to fill
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
   break;
    }
    val += c; 
  }

  if (val.length() >0) 
  {
    if (val =="on")
    {
     digitalWrite(13,HIGH);
      Serial.println("The value is on");
    }
//val = "";
  }
}

Here is the code for the PHP (I also use the php_serial.class):

<?php
//check the GET action var to see if an action is to be performed
if (isset($_GET['action'])) {

	//Load the serial port class
	require("php_serial.class.php");
	
	//Initialize the class
	$serial = new phpSerial();

	//Specify the serial port to use
	$serial->deviceSet("COM9");
	
	$serial->confBaudRate(9600); //Baud rate: 9600

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

	if ($_GET['action'] == "on") {
		//send message to turn the LED on
		$serial->sendMessage("on");
	} 
	
	//We're done, so close the serial port
	$serial->deviceClose();
}


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>LED Controller</title>
</head>
<body>

<h1>LED Controller</h1>
<p><a href="<?php echo "./index.php?action=on"?>">Click here to turn on.</a></p>
</body>
</html>

Anyone?

Please use the # button for code

It might be that PHP closes the serial port, causing the Arduino to reset. Can be tested with modified code below

String val;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  for(int i=0; i<5; i++)
  {
    digitalWrite(13, HIGH);
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
  }
}

void loop()
{
  while (Serial.available())
  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') break;
    val += c;
     if (val.length() >10) break;
  }

  if (val.length() >0)
  {
    if (val == "on")
    {
      digitalWrite(13, HIGH);
      Serial.println("The value is on");
      val = "";
    }
    else
    {
      digitalWrite(13, LOW);
      Serial.println("The value is off");
    }
  }
}
	$serial->deviceOpen();
	if ($_GET['action'] == "on") {
		$serial->sendMessage("on");
	} 
	$serial->deviceClose();

Open the port, causing the Arduino to reset, then jam some data out and slam the port shut, causing another reset, without giving the Arduino time to get ready to read the data.

Got it.

And the problem was?

Did that already, the code now looks like this:

	if ($_GET['action'] == "on") {
		//to turn relay number 1 on, we issue the command 
		$serial->sendMessage("on");
	
	} else if ($_GET['action'] == "deviceoff") {
		$serial->deviceClose();

Still no luck

What opens the port? What operating system?

I'm on Windows 7 right now. I'm using Xampp.

Two questions. One answer.

Not quite the right ratio.

I don't quite understand what do you mean by what's opening the port... I guess you mean this $serial->deviceOpen(); line of code? :confused:

You posted all of your code. I pointed out why that wouldn't work, so you posted a snippet of your revised code. Posting snippets is not the way to get help. Post all of your code.

It is likely that when the PHP parser ends, that it closes the serial port. So, when you execute the script again (how you are doing is a bit of a mystery, since there is no form with submit buttons), with a different action, the port you are trying to write to is not open.

Do you have warnings/errors in PHP enabled?

Here is the PHP code (Arduino code remains the same):

<?php
//check the GET action var to see if an action is to be performed
if (isset($_GET['action'])) {
	//Action required
	
	//Load the serial port class
	require("php_serial.class.php");
	
	//Initialize the class
	$serial = new phpSerial();

	//Specify the serial port to use
	$serial->deviceSet("COM9");
	
	$serial->confBaudRate(9600); //Baud rate: 9600

	//Open the serial port
	$serial->deviceOpen();

        //send the "on" message
	if ($_GET['action'] == "on") {
		$serial->sendMessage("on");

	//send the "off message"
	} else if ($_GET['action'] == "deviceoff") {
		$serial->deviceClose();
	}
}


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>
<body>

<h1></h1>
<p><a href="<?php echo "./index.php?action=on"?>">Turn on.</a></p>
<p><a href="<?php echo "./index.php?action=deviceoff"?>">Turn off.</a></p>
</body>
</html>

There are no forms with submit buttons but I used links that do the same job.
Yes I have warnings/errors enabled in PHP and I don't get any messages while running the script.

Every time you invoke that script, it opens the serial port. Opening the serial port resets the Arduino.

As soon as PHP opens the port, it jams out the data ("on"), if required, and then ends.

The Arduino hasn't even finished resetting when the PHP script sends the data, so the data is lost.

Try adding a link to open the port, one to send data, and one to close the port.

Before sending data, test that the port is still open. Wait long enough for the Arduino to reset before clicking the send data link.

I modified the code a little.

<?php

//check the GET action var to see if an action is to be performed
if (isset($_GET['action'])) {

	//Load the serial port class
	require("php_serial.class.php");
	
	//Initialize the class
	$serial = new phpSerial();
	
	//Specify the serial port to use... in this case COM1
	$serial->deviceSet("COM9");
	
	$serial->confBaudRate(9600); //Baud rate: 9600
	
	if ($_GET['action'] == "turnon") {
		$serial->deviceOpen();
	} else if ($_GET['action'] == "on") {
		$serial->sendMessage("on");
	} else if ($_GET['action'] == "deviceoff") {
		$serial->deviceClose();
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Control LED</title>
</head>
<body>

<h1>Control LED</h1>
<p><a href="<?php echo "./index.php?action=turnon"?>">Click here to turn the system on.</a></p>
<p><a href="<?php echo "./index.php?action=on"?>">Click here to turn the LED on.</a></p>
<p><a href="<?php echo "./index.php?action=deviceoff"?>">Click here to turn the LED off.</a></p>
</body>
</html>

Nothing changes... It seems that every time the page refreshes the port is closed and reopened. I wonder if using Ajax or something similar will it be able solve the problem?

Certainly, C# (or other application that maintains state) is able to maintain an open connection. I was concerned that each invocation of the script opened and closed the serial port.

Can you see that the Arduino does indeed restart each time the script is invoked? Watch the onboard LEDs that flash when the Arduino starts. If they flash the same way when the script is invoked, then the script is opening and closing the port each time.

here's what worked for me

arduino code

 if (Serial.available() > 0) {
   incoming = Serial.read();
 }
   if (incoming == (10)) {
      digitalWrite(ledPin, HIGH);
   }
    }

php code

<?php
	require("php_serial.class.php"); //Initialize the class
	$serial = new phpSerial(); 
	$serial->deviceSet("COM4"); //Specify the serial port to use
	//Set the serial port parameters. The documentation says 9600 8-N-1, so
	$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->deviceOpen();
	$serial->sendMessage(chr(10)); // start transmission
	
	$serial->deviceClose();; 	  //We're done, so close the serial port again

   header( 'Location: index.php' ) ;   // forward to index.htm
?>

the php code is called by a link from index.htm
it opens COM4
sends "10" through serial
closes COM4
sends the browser back to index.htm

I think I've seen PHP code that tried putting a wait statement after opening the serial port to give the arduino time to reset. Not sure if it worked. The quick fix is to load the program in the arduino, then get a 100 ohm resistor and stick one end in the reset pin hole and the other in the +5v pin hole. This will keep the arduino reset while the serial port is opened and closed.