title: How to control the serial GPIO pin of Raspberry Pi via an Apache web sever
body: <p>I would like to send data from <a href="http://en.wikipedia.org/wiki/Raspberry_Pi" rel="nofollow">Raspberry Pi</a> to <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino Uno</a> via the GPIO serial port by using the Apache server running on Raspberry Pi. I connected the TX pin of RPI to a 3.3&nbsp;V-to-5&nbsp;V level shifter and its output to the RX Arduino PIN.</p>
  
  <p>To send data from Raspberry Pi to Arduino I used the following C program that I recompiled for Raspberry Pi, and it works fine. I renamed the executable code <code>SendUART</code>.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;   //Used for UART
  #include &lt;fcntl.h&gt;    //Used for UART
  #include &lt;termios.h&gt;  //Used for UART
  #include &lt;string.h&gt;
  
  main(int argc,char **argv)
  {
      //----- TX BYTES -----
      unsigned char tx_buffer[20];
      unsigned char *p_tx_buffer;
      int lx;
  
      //-------------------------
      //----- SETUP USART 0 -----
      //-------------------------
      //At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
      int uart0_filestream = -1;
  
      //OPEN THE UART
      //The flags (defined in fcntl.h):
      //    Access modes (use 1 of these):
      //        O_RDONLY - Open for reading only.
      //        O_RDWR - Open for reading and writing.
      //        O_WRONLY - Open for writing only.
      //
      //    O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
      //                                            if there is no input immediately available (instead of blocking). Likewise, write requests can also return
      //                                            immediately with a failure status if the output can't be written immediately.
      //
      //    O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
      uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);        //Open in non blocking read/write mode
      if (uart0_filestream == -1)
      {
          //ERROR - CAN'T OPEN SERIAL PORT
          printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
      }
  
      //CONFIGURE THE UART
      //The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
      //    Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
      //    CSIZE:- CS5, CS6, CS7, CS8
      //    CLOCAL - Ignore modem status lines
      //    CREAD - Enable receiver
      //    IGNPAR = Ignore characters with parity errors
      //    ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
      //    PARENB - Parity enable
      //    PARODD - Odd parity (else even)
      struct termios options;
      tcgetattr(uart0_filestream, &amp;options);
      options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;  //&lt;Set baud rate
      options.c_iflag = IGNPAR;
      options.c_oflag = 0;
      options.c_lflag = 0;
      tcflush(uart0_filestream, TCIFLUSH);
      tcsetattr(uart0_filestream, TCSANOW, &amp;options);
      p_tx_buffer = &amp;tx_buffer[0];
      strcpy(tx_buffer, argv[1]);
      lx=strlen(tx_buffer);
  
  
      if (uart0_filestream != -1)
      {
          int count = write(uart0_filestream, &amp;tx_buffer[0], 10);  //Filestream, bytes to write, number of bytes to write
          if (count &lt; 0)
          {
              printf("UART TX error\n");
          }
      }
      //----- CLOSE THE UART -----
      close(uart0_filestream);
  }
  </code></pre>
  
  <p>Then I used a PHP script (test.php) to handle the software via the "system" command:</p>
  
  <pre><code>&lt;?php
       $mes = "0123456789";
       $retval = 0;
       $last_line = 0;
       echo($mes);
       $SndMsg = "/var/www/SendUART " . $mes;
       $last_line = system($SndMsg, $retval);
       echo $last_line
  ?&gt;
  </code></pre>
  
  <p>And I executed it via the command line:</p>
  
  <pre><code>php -f test.php
  </code></pre>
  
  <p>The string is correctly received by the Arduino (I developed a simple <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow">sketch</a> for Arduino that turns on the onboard LED if all characters are received from the Rx pin).</p>
  
  <p>Then I called the PHP script via the Apache web server, writing in the address bar:
  <a href="http://192.168.1.103/test.php" rel="nofollow">http://192.168.1.103/test.php</a></p>
  
  <p>where 192.168.1.103 is the IP address of the Raspberry Pi and <code>test.php</code> is the PHP script. Of course, both <code>test.php</code> and the SenUART program are stored in the same folder /var/www/, but the following error is shown in the browser:</p>
  
  <blockquote>
    <p>0123456789Error - Unable to open UART. Ensure it is not in use by another application</p>
  </blockquote>
  
  <p>How do I fix the problem?</p>