Offline
Newbie
Karma: 0
Posts: 21
|
 |
« on: February 22, 2011, 08:09:02 am » |
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>
|
|
|
|
« Last Edit: February 22, 2011, 10:28:12 am by LastTemplar »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #1 on: February 22, 2011, 09:17:07 am » |
Anyone?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: February 22, 2011, 10:06:20 am » |
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"); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #3 on: February 22, 2011, 10:32:08 am » |
$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
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #4 on: February 22, 2011, 10:35:42 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: February 22, 2011, 10:41:31 am » |
What opens the port? What operating system?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #6 on: February 22, 2011, 10:44:05 am » |
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
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #7 on: February 22, 2011, 10:57:02 am » |
Two questions. One answer.
Not quite the right ratio.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #8 on: February 22, 2011, 11:36:36 am » |
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? :/
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: February 22, 2011, 11:41:07 am » |
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
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #10 on: February 22, 2011, 11:59:28 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: February 22, 2011, 12:07:27 pm » |
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
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #12 on: February 22, 2011, 12:55:07 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: February 22, 2011, 01:41:43 pm » |
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
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #14 on: February 22, 2011, 06:47:19 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
|