Connecting PHP with Arduino via serial port on Linux

Arduino changed the word. Everybody can make an astonishing project without a degree in electronic. If you are a programmer soon or later you will get an idea of connecting it to a computer. If you are a PHP programmer you will most likely want to use PHP to talk to the Arduino.

Serial communication in Linux is very simple. First connect you Arduino and setup serial port.

$ stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig
-icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

You can connect immediately with screen.

$ screen /dev/ttyACM0 9600

To avoid confusion and time wasting remember Arduino will restart on every connection to /dev/ttyACM0.

If you do something like this:

$fp =fopen("/dev/ttyACM0", "w+");
if( !$fp) {
        echo "error";d

fwrite($fp, “Hello!”);

There is a big chance it will not work. It takes some time for Arduino to boot. If you write instantly after opening the file transmission will be ignored. Arduino didn’t have enough time to run serial listening after restart. Start with fread() and wait for micro controller to send something first. Then talk to it. You can also experiment with sleep() function to give it enough time.

If you don’t want Arduino to reboot on every connection you can insert 10uF capacitor from RST pin to GRD.

One thought on “Connecting PHP with Arduino via serial port on Linux

  1. That it right only if connected by, let say, the standard serial port. If connected directly at Rx / Tx pins with no flow control there is no reset during the initiation of serial communication. The problem is not on the Arduino side but the php script that only is able to send but not to receive what Arduino sends.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s