Pages: [1] 2   Go Down
Author Topic: Arduino and PHP  (Read 1555 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again smiley
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:
Code:
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):

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

« Last Edit: February 22, 2011, 10:28:12 am by LastTemplar » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12480
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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");
    }
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46212
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
$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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did that already, the code now looks like this:
Code:
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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46212
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What opens the port? What operating system?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm on Windows 7 right now. I'm using Xampp.
« Last Edit: February 22, 2011, 10:47:46 am by LastTemplar » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46212
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Two questions. One answer.

Not quite the right ratio.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46212
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46212
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I modified the code a little.

Code:
<?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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46212
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here's what worked for me

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

php code
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
Logged

Pages: [1] 2   Go Up
Jump to: