Go Down

Topic: Arduino and PHP (Read 2 times) previous topic - next topic

LastTemplar

Here is the PHP code (Arduino code remains the same):
Code: [Select]
<?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.

PaulS

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.

LastTemplar

I modified the code a little.

Code: [Select]
<?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?

PaulS

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.

-Moe-

here's what worked for me

arduino code
Code: [Select]
if (Serial.available() > 0) {
   incoming = Serial.read();
}
   if (incoming == (10)) {
      digitalWrite(ledPin, HIGH);
   }
    }


php code
Code: [Select]

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

Go Up