Serial communication with PHP

Still exploring options.

Another quick question:

Could I use an Ethernet Shield in some way to achieve the same results? Remember basically I'd like to build an Arduino project that I can control via the web. Since it seems that using PHP to send serial data is proving to be much more challenging than I initially thought, maybe a shield can save me?


It seems like my Arduino might be resetting whenever PHP opens or closes the serial connection to my COM port. Is there any way I can make it stop? I've heard that you can add a resistor between the 5 or the 3.3 volt pins and the reset pin to make this a reality. Any thoughts?

Does this help...

Ahh, I believe this has finally solved my issue! I'm able now to suppress the reset when the PHP code opens the serial port and it seems to be working now. Thank you all again for your help and advice. I'm working on the code now to finalize the project. I've got my servos in the mail this weekend, and by next weekend (time permitting) I'll have a viewable pan/tilt web cam for you all!

Thanks again. (ps, I'll make a detailed writeup about the whole process so others can learn from my mistakes)

I'll make a detailed writeup about the whole process so others can learn from my mistakes

Thank you! That would be very helpful to the community. At this very moment there are two other folks having trouble getting PHP to work with an Arduino.

I have two requests. Please...

  • Change the Arduino to a serial port above 9 and test. I suspect PHP does not work support serial ports above 9. It would be nice to know for certain.
  • When you do your writeup, include the version of PHP you are using.

Good job with getting this to work with PHP!

I have one question though, have you tried not closing the file descriptor right after you write to it? Something like


$fp =fopen("com7", "w");
fwrite($fp, chr(a));




this way you would have time to read the input from the Arduino before it closes the port and resets. It would be fun to know if this way works but in your case, it isn’t a viable solution because the php script to control the Arduino would be executed via the web.

In cases where PHP isn’t used from the web (running from command line instead of over Apache) and your PHP script uses an infinite loop to receive / send data constantly to the Arduino, this solution could be possible I guess.

soup can you post your code php and arduino?? I have your same problem.. thanks!! i need help!

sorry for my eng

Sure thing. I'm actually almost done with my write-up on this project. When it's all finished I'll post a link to my blog here so you can all check it out. Thanks again to all who helped me make this a reality. Can't wait to get started on my next project!

Alright all, here is the link to my complete writeup for this project. Keep in mind that I'm even less talented in writing than I am in Arduino projects!

Enjoy! And I hope this helps someone achieve that awesome project! Aneal K.


Something minor I noticed about your code; it obviously had nothing to do with your problem, but was something to keep in mind (for this small sketch, it really doesn't matter - but for larger projects, it might matter a lot)...

You declare the variable "inByte" for reading from the serial port as an integer...[stop]

soup - I am not asking the following of you, but if you know, please chime in...

Ok - I just went to the function definition on the Arduino site:

I just noticed that it reads:

"Returns...the first byte of incoming serial data available (or -1 if no data is available) - int"

Why isn't the return value of this function a byte, instead of an int? Aren't integers on the Arduino defined as 2 bytes long?:

So - why does return an int, and why doesn't the documentation match up? Also, what is the return value type for Serial.available() - it isn't mentioned in the documentation, either?


Honesly, you're blowing my mind. I have more experience coding with Visual Basic 6 than I have in any other language/synax. I'm used to using VB, so I declare almost everything as Integers. I've never written any code that was so involved as to worry about size or memory constraints.

On that note, I know I didn't answer your question. I'm not sure why the integer data type worked here or why I didn't use a lesser data type. I just did what I was used to.

I'm open to others' input here as well. Can anyone inform me/us?

Aneal K.

Ok - I just went to the function definition on the Arduino site:
I just noticed that it reads:
“Returns…the first byte of incoming serial data available (or -1 if no data is available) - int”
Why isn’t the return value of this function a byte, instead of an int?
So - why does return an int,

It allows the status and value to be returned in a single function call. It allows a loop like this…

  int ch;

  ch =;

  while ( ch != -1 )
    if ( (char) ch == 'x' )
    ch =;

Aren’t integers on the Arduino defined as 2 bytes long?


and why doesn’t the documentation match up?

I can’t help with that one.

Also, what is the return value type for Serial.available() - it isn’t mentioned in the documentation, either?

uint8_t (aka byte).

Sweet : I have a seeeduino mega, which has a toggle switch for the reset function : auto or manual, so that issue isn’t one here.

I downloaded the php serial class mostly because I am interested in reading values from the arduino into a mysql database. My first trial was with the example.php that comes with the class. I made some really small changes.

  • comment out the write line
  • check arduino interface for port name, was /dev/ttyUSB0, so change portname to that string
  • add html header and footer
  • add : echo $serial; after tthe readport() line, this gives me instant feedback, once this works it should easy to direct the output to mysql and then schedule a cron job that calls this php script every 15 minutes or so.

On the arduino, I ran the serial print demo sketch (that prints values in all formats). I verified arduino functionality through serial monitor, than closed that. As you close that, the red tx led on the arduino goes dark.

The first run of the php script failed, the arduino led never gave any sign of life.
I needed to chmod 777 /dev/ttyUSB0, and then I started to get gibberish in my php page, which means success !

Next up : make the arduino respond to a specific call with readings in a specified format. Or maybe I will do one value depending on which call it gets : pc sends A, arduino response is temperature value, pc sends B, response is hunidity etc.

pc code

<?php include "php_serial.class.php"; echo " Test MySQL "; // Let's start the class $serial = new phpSerial; // First we must specify the device. This works on both linux and windows (if // your linux serial device is /dev/ttyS0 for COM1, etc) $serial->deviceSet("/dev/ttyUSB0"); $serial->confBaudRate(9600); // Then we need to open it $serial->deviceOpen(); // To write into $serial->sendMessage("Hello !"); // Or to read from $read = $serial->readPort(); echo "here it is"; echo $read; echo "here it was"; sleep(1); // If you want to change the configuration, the device must be closed $serial->deviceClose(); // We can change the baud rate //$serial->confBaudRate(2400); // etc... ?>

arduino code =

Keep in mind that opening a serial connection resets the Arduino, as does closing the connection, unless you have modified the Arduino.

You should not open and close the connection before and after every send/receive of data.

Also, you need to allow time for the Arduino to reset before you send it data, or that data will be lost.

@Paul : as mentioned, seeeduino clones (like my mega) come with a switch that enables you to toggle between autoreset and manual reset. I guess this is a cheap but nice feature (even a jumper would do) that all arduinos should have, given that communications seem so much easier with this option. Anyone speak italian ? :-)

Thanks for the hints on the open/close and reset. The open/close : I think I would prefer to open/close since I intend to use an xbee (or some other wireless system) to get readings only every 15 minutes or so. Unless I am overlooking anything ?