Pages: [1]   Go Down
Author Topic: Arduino serial comunication with php  (Read 501 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone!

How i can comunicate on serial port with Arduino Uno board without serial monitor is turned on?
I have a php script with i can turn on and off a led on Arduino board, but this work just when the Serial Monitor is turned on.

my arduino code

Code:
const int ledPin = 12;
int incomingByte;      
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // see if there's incoming serial data:
 
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  
}

my php code


  
Code:
       public function actionTurnLed()
        {
            error_reporting(E_ALL);
            if( !empty($_GET["state"]) )
            {
                
                $serial = new phpSerial();
               $serial->deviceSet("/dev/ttyACM0");
               $serial->confBaudRate(9600);
               $serial->confParity("none");
                $serial->confCharacterLength(8);
                $serial->confStopBits(1);
                
                 $serial->deviceOpen();
                 if( $_GET["state"] == "1" )
                 {
                     $serial->sendMessage("H");
                 }
                 else if($_GET["state"] == "2" )
                 {
                     $serial->sendMessage("L");
                 }
            }
            $this->render("led");
        }
( Sorry for my English )
« Last Edit: December 08, 2012, 10:44:47 am by Ervin » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are a number of things that the Serial Monitor does to configure the serial port. You need to make sure that your PHP script does all the same things. I don't think that the correct number of bits per word is a smiley face.

What other methods does the phpSerial class have?
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to delay 2 seconds after opening the serial port before sending the first command to the arduino.  Otherwise the command is eaten by the bootloader, and the arduino looks unresponsive, as in this case.

-br
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8923
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless times have changed, php communicates with the serial port in a open port > send data > close port method. When php opens the serial port, it causes the arduino to reset/reboot. Disabling the auto reset of the arduino might be3 a workaroung for reset issues.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

Unless times have changed, php communicates with the serial port in a open port > send data > close port method. When php opens the serial port, it causes the arduino to reset/reboot. Disabling the auto reset of the arduino might be3 a workaroung for reset issues.

Thank you ! The problem was with automatic reset, i disabled it with a 10uF condenzator on board and it works without serial monitor.
Logged

Pages: [1]   Go Up
Jump to: