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

Hi,

      Project: Php program that light on a led connected to the Arduino.


So I followed a few tutorials:   php_serial.class.php+phpprogram.php+arduinoprogram.ino

I tried the same Arduino code with a python program and it works. Not fot PHP.

The Arduino reacts everytimes a execute the program , but the led doesn't light on.


For the php_serial.class.php, they only thing that needs to be changed is the serial port(I think):

Code:
function deviceSet ($device)
{
if ($this->_dState !== SERIAL_DEVICE_OPENED)
{
if ($this->_os === "linux")
{
if (preg_match("@^COM(\d+):?$@i", $device, $matches))
{
$device = "/dev/ttyACM0" . ($matches[1] - 1);
}

if ($this->_exec("stty -F " . $device) === 0)
{
$this->_device = $device;
$this->_dState = SERIAL_DEVICE_SET;
return true;
}


As for the php program :


Code:
<?php
include("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");

$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->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none.

//Now we "open" the serial port so we can write to it
$serial->deviceOpen();

$serial->sendMessage("*1" ); //sleep(1); // echo "hi"; $serial->deviceClose();

?>


Arduino code :

Code:
int OLight1 = 5 ;



void setup(){
  // Open serial connection.
  Serial.begin(9600);
  pinMode(OLight1, OUTPUT);
 
}

void loop(){

 
  if(Serial.available() > 0){      // if data present, blink
    digitalWrite(OLight1, HIGH);
    delay(500);           
    digitalWrite(OLight1, LOW);
    delay(500);
    digitalWrite(OLight1, HIGH);
    delay(500);           
    digitalWrite(OLight1, LOW);
   
  }
}





Any idea ?  Thank you
Logged

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

Opening the serial port resets the Arduino. Sending serial data before the Arduino is ready is a good way to ensure that the data is lost.

Once the data actually gets to the Arduino, don't you suppose that reading it would be a good idea?
Logged

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

It actually works by adding this :   sleep(3);

Serial.available() > 0 is enought to initiate the blink.  Once it receives data(whatever the size), it starts.

Code:
<?php
include("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");

$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->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none.

//Now we "open" the serial port so we can write to it
$serial->deviceOpen();

sleep(3); ADDED CODE

$serial
->sendMessage("*1" ); //sleep(10); // echo "hi"; $serial->deviceClose();

?>


« Last Edit: February 26, 2013, 10:03:18 pm by playagood » Logged

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

Quote
Serial.available() > 0 is enought to initiate the blink.  Once it receives data(whatever the size), it starts.
Sure, but if you actually read the serial data, you could see whether blinking should start or stop, or which pin should blink, or what the PWM value for a pin should be, or...
Logged

Earth
Offline Offline
Edison Member
*
Karma: 53
Posts: 1883
My browser no longer is binding static IP, Floating is the way to go.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I tried the same Arduino code with a python program and it works. Not fot PHP.

If your python program has no time.sleep() between open and write, u might use python php bridge to bridge python into php.  I highly doubt is the case.

http://pecl.php.net/package/python


sleep(3) is not solution but Band-Aid.




Logged

Pages: [1]   Go Up
Jump to: