Using PHP serial class with Arduino and Raspberry Pi

Last time I talked about connecting the Raspberry Pi with the Arduino, I could send simple echo strings from the Raspberry Pi to my Arduino. Great now lets step it up! During research I came across the PHP serial class, a great implementation of serial control for PHP. Although the class lacks decent support for Windows, it fully supports Linux (and Raspberry Pi).

Configuring your Raspberry Pi for PHP serial class

Before you can use the class some stuff must happen (I assume you have PHP enabled Raspberry Pi running, there are a gazillion tutorial blogs out there, Google one). First we need to find out what user runs PHP.

For Lighttpd it is www-data, apache may be different. Every serial connection (virtual of physical) is owned by the dialout group, so if we add www-data to the dialout group our PHP script should be able to open/read/write the serial device. The following command will add the group to www-data.

Running the command groups www-data give the following result

Great, www-data belongs to dialout and www-data. Now RESTART your Raspberry Pi. I literally wasted hours debugging to finally realized I just needed a restart (I know it sounds stupid).

Testing the connection

So on my Arduino I still have the same old pass through script in place. For the PHP I’ll write a simple script that just sends a string to the Arduino.

First I enable all the errors, the PHPserial class issues warnings on failure and by default they aren’t displayed (in a normal PHP configuration). Then I include the PHP serial class file. Next I initiate a new phpSerial object called $serial and configure some parameters. We don’t have parity, characters are 8 bits and we use 1 as a stop bit. After that I can open the device send my message and close it. Finally I echo some feedback to the browser saying I did my job.

I don’t know how or why but for every connection I open, I get question marks (unknown chars). They have a decimal value of 254 and I really don’t have a clue what they are. When I use a normal echo command in the terminal I don’t get those characters.

Debugging the PHP serial

A lot can go wrong, so lets cover the basics. If your browser keeps loading and nothing happens then your Serial connection is locked up, restart your Pi to release it and see that you close the device in your PHP script.

If it still keeps loading then there might be a problem with the first part of this blog, make user that the user www-data belongs to the dialout group. Use the following command to check if the dialout group has access to the /dev/ttyAMA0 device:

If browser says the message is send but you don’t see anything on your Arduino serial monitor then check for common flaws. Unplugged cables, wrong level converter circuit, baud rate and so on are all possible suspects.

Next time I’ll talk about my ArduinoPi Controller based on the PHP serial class. It’s a web based Arduino implementation using the Raspberry Pi as web host.

7 thoughts on “Using PHP serial class with Arduino and Raspberry Pi

  1. Pingback: Contest: Can You Combine a Raspberry Pi and an Arduino? - ProtoLounge

  2. Pingback: very useful things | Pearltrees

  3. Pingback: php serial connection to arduino uno | digitalitility

  4. Very useful article. I would never come up with the idea of using “whoami”. Funny thing is that even reading this, I almost forgot to restart after changing permissions. By the way, I’m using Apache2 and the user is also www-data. Thank you very much.

  5. Now, I am able to send and receive data serially from arduino to raspberry pi and vice versa.
    one thing I notice is.
    I am sending [Carriage Return (CR) character (0x0D)] [Line Feed (LF) character (0x0A)] after the string as shown in attached pic(serialrawdata.png),
    but it shows [0x0A][0x0A] in serialphp as shown in pic(phpoutput.png)

    I am facing another problem.
    I am sending raw data from arduino to raspberry pi.
    all regular char ( A to Z , a to z , 0 to 9 ) can be received normally in $read = $serial->readPort();
    problem is when arduino sends 0x7F , one char is being deleted from $read.
    I need to store each raw data.
    image link is
    https://plus.google.com/photos/111938137068562649282/albums/5854689735472757361

  6. hi there,

    in case you use selinux, you should set the allow_daemons_use_tty boolean on true via:

    sudo setsebool -P allow_daemons_use_tty 1

    to allow your httpd-daemon to access the port!

    this took me some time… although not on a raspberry pi, but on a ‘ordinary’ fedora 18 system!

    thanks for your nice article.

Leave a Reply