php serial connection to arduino uno

control your arduino with your browser… the basic steps.

add the user running the webservice (e.g. www-data ) to the dialout group. ( http://www.fritz-hut.com/php-serial-class/)

 sudo useradd www-data dialout

restart your machine

the code for the arduino to turn the LED on and off:

int  pin = 13;
int incomingbyte;

void setup()
{
pinMode(pin, OUTPUT); // connected LED at Pin13
Serial.begin(115200);      // start communication at 115200bps
}

void loop()
{
if( Serial.available() > 0)     // if connection is set
  {incomingbyte = Serial.read();
  }
  if(incomingbyte == '0')
  {
  digitalWrite(pin, LOW);  // turns LED off
  }
  if( incomingbyte == '1' )
  {
  digitalWrite(pin, HIGH); // turns LED on
  }
}

and the php file

deviceSet("/dev/ttyACM0");
$serial->confBaudRate(115200);
//$serial->confParity("none");
//$serial->confCharacterLength(8);
//$serial->confStopBits(1);
$serial->deviceOpen();
sleep(2);
// $serial->sendMessage('1');
 
if ($_GET['action'] == "0") {
        $serial->sendMessage('0\r');
        }
else if ($_GET['action'] == "1") {
        $serial->sendMessage('1\r');
}
 
 
$serial->deviceClose();
 
}
?>

Author: Stefan Schwaha

-<>-

Leave a Reply