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.
|
1 2 3 4 5 |
<?php echo exec('whoami'); ?> |
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.
|
1 |
usermod -a -G dialout www-data |
Running the command groups www-data give the following result
|
1 2 |
root@raspberrypi:/opt/www# groups www-data www-data : www-data dialout |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); include "php_serial.class.php"; $serial = new phpSerial; $serial->deviceSet("/dev/ttyAMA0"); $serial->confBaudRate(115200); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->deviceOpen(); $serial->sendMessage("Hello from my PHP script, say hi back!"); $serial->deviceClose(); echo "I've sended a message! \n\r"; ?> |
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:
|
1 2 |
root@raspberrypi:/opt/www# ls -l /dev/ttyAMA0 crw-rw---T 1 root dialout 204, 64 Aug 30 20:21 /dev/ttyAMA0 |
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.


Pingback: Contest: Can You Combine a Raspberry Pi and an Arduino? - ProtoLounge
Pingback: very useful things | Pearltrees
Pingback: php serial connection to arduino uno | digitalitility
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.
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
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.
Very helpful blog post, got me out of a problem i was having using a Raspberry Pi and serial based Xbee module to control my central heating controller remotely.