title: PHP serial port data return from Arduino
body: <p>I wonder if there is a way to accomplish reading my serial port via PHP - that works :-)</p>
  
  <p>In practising my Arduino skills, I developed a simple LED ON/OFF sketch. It works by entering <strong>on</strong> or <strong>off</strong> in the serial monitor.</p>
  
  <p>Next step, I put together a webpage to act as an GUI interface to click a link and perform the  on and off function above. This webbased GUI works via PHP.
  I am using the <a href="http://www.phpclasses.org/browse/file/17926.html" rel="noreferrer">PHP SERIAL</a> class to interact with the serial port my Arduino is using.</p>
  
  <p><strong>The issue is I need to find a way of getting feedback from the serial port. Using the Arduino IDE serial monitor, I can see my printed messages in response to each of my serial input and I need to retrieve the same feedback in my PHP code.</strong></p>
  
  <p>The PHP Serial class offers a <strong>readPort()</strong> function but I does not return my data.</p>
  
  <p><strong>UPDATE[2]:</strong></p>
  
  <p>ARDUINO:</p>
  
  <pre><code>const int greenPin = 2;
  const int bluePin = 3;
  const int redPin = 4;
  
  int currentPin = 0; //current pin to be faded
  int brightness = 0; //current brightness level
  
  void setup(){
    Serial.begin(9600);
  
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    pinMode(redPin, OUTPUT);
  }
  
  void loop(){
    //if there's any serial data in the buffer, read a byte
    if( Serial.available() &gt; 0 ){
      int inByte = Serial.read();
  
        //respond only to the values 'r', 'g', 'b', or '0' through '9'
        if(inByte == 'r')
          currentPin = redPin;
  
        if(inByte == 'g')
          currentPin = greenPin;
  
        if(inByte == 'b')
          currentPin = bluePin;
  
        if(inByte &gt;= '0' &amp;&amp; inByte &lt;= '9'){
          //map the incoming byte value to the range of the analogRead() command
          brightness = map(inByte, '0', '9', 0, 255);
          //set the current pin to the current brightness:
          analogWrite(currentPin, brightness);
        }
  
        Serial.print("Current Pin : ");
        Serial.println(currentPin);
  
        Serial.print("Brightness : ");
        Serial.println(brightness);
  
    }//close serial check
  
  }
  </code></pre>
  
  <p>PHP/HTML:</p>
  
  <pre><code>&lt;?php
      require("php_serial.class.php");
  // include("php_serial.class.php");
  
      // Let's start the class
      $serial = new phpSerial();
  
      // First we must specify the device. This works on both Linux and Windows (if
      // your Linux serial device is /dev/ttyS0 for COM1, etc.)
      $serial-&gt;deviceSet("/dev/ttyACM0");
  
      // Set for 9600-8-N-1 (no flow control)
      $serial-&gt;confBaudRate(9600); //Baud rate: 9600
      $serial-&gt;confParity("none");  //Parity (this is the "N" in "8-N-1")
      $serial-&gt;confCharacterLength(8); //Character length     (this is the "8" in "8-N-1")
      $serial-&gt;confStopBits(1);  //Stop bits (this is the "1" in "8-N-1")
      $serial-&gt;confFlowControl("none");
  
      // Then we need to open it
      $serial-&gt;deviceOpen();
  
      // Read data
      $read = $serial-&gt;readPort();
  print "&lt;pre&gt;";
  print_r($read);
  print "&lt;/pre&gt;";
  
      // Print out the data
      echo $read;
          // print exec("echo 'r9g9b9' &gt; /dev/ttyACM0");
      print "RESPONSE(1): {$read}&lt;br&gt;&lt;br&gt;";
  
      // If you want to change the configuration, the device must be closed.
      $serial-&gt;deviceClose();
  ?&gt;
  
  &lt;?php
      if( isset($_REQUEST['LED']) )
          response();
  ?&gt;
  
  &lt;form action='index.php' method='POST'&gt;
      &lt;select id='led' name='LED'&gt;
          &lt;option id='nil'&gt;-&lt;/option&gt;
          &lt;option id='red'&gt;RED&lt;/option&gt;
          &lt;option id='green'&gt;GREEN&lt;/option&gt;
          &lt;option id='blue'&gt;BLUE&lt;/option&gt;
          &lt;option id='all'&gt;ALL&lt;/option&gt;
      &lt;/select&gt;
      &lt;input type='submit' value='SET'&gt;
  &lt;/form&gt;
  
  &lt;?php
      print "Hi, Earthlings!";
  
      function response(){
          $CMDString = "";
          $execute   = false;
  
          if( isset($_REQUEST['LED']) ){
                  switch ($_REQUEST['LED']) {
                      case 'RED':
                          $CMDString = 'r9';
                          $execute = true;
  
          exec("echo 'r9g0b0' &gt; /dev/ttyACM0");
  
                  print "&lt;br&gt;FOUND: {$_REQUEST['LED']}";
                          break;
                      case 'GREEN':
                          $CMDString = 'g9';
                          $execute = true;
                  print "&lt;br&gt;FOUND: {$_REQUEST['LED']}";
                          break;
                      case 'BLUE':
                          $CMDString = 'b9';
                          $execute = true;
                  print "&lt;br&gt;FOUND: {$_REQUEST['LED']}";
                          break;
                      case 'ALL':
                          $CMDString = 'r9g9b9';
                          $execute = true;
                  print "&lt;br&gt;FOUND: {$_REQUEST['LED']}";
                          break;
                      default:
                          print exec("echo 'r0g0b0' &gt; /dev/ttyACM0");
                          $execute = false;
                          break;
                  }
  
                  if($execute){
                      print exec("echo '{$CMDString}' &gt; /dev/ttyACM0");
                      print "&lt;br&gt;&lt;br&gt;executing: {$CMDString}";
                  }
          }
      }
  ?&gt;
  </code></pre>