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.
<?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:
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.