Hi again
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>
$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.
if ($_GET['action'] == "on") {
//to turn relay number 1 on, we issue the command
$serial->sendMessage("on");
} else if ($_GET['action'] == "deviceoff") {
$serial->deviceClose();
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.
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.
<?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.
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.