Arduino with php serial

Hallo !

I try to make an .php page with php serial class and i want to this page show mi the temperature from Arduino Serial port but without refreshing the page. I try this :

The php page:

header('Content-type: application/json');
// Normally we would load the serial port class
// But to use with Titanium we remove this to use with titanium and include it in the index, so its only called once.
// if you want to test in a web browser make sure you have php running on your computer and uncomment this next line
// Initialize the class
$serial = new phpSerial();
//Specify the serial port to use... in this case COM1
// Set the serial port parameters. The documentation says 9600 8-N-1, so
$serial->confBaudRate(9600); //Baud rate: 9600
// leave this alone
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length
// (this is the "8" in "8-N-1")
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1")
// Device does not support flow control of any kind,
// so set it to none.
// Now we "open" the serial port so we can read from it
$read = $serial->readPort();
// trim it down, because we are printing a new line every time and thers a \n at the end of it.
$read = (substr($read, 0, -2));
//We're done, so close the serial port again
/// set your timezone of titanium flips shit
// format we are matching is Sat, 30 Apr 2011 08:51:00 -0400
$time = date("D, d M Y H:i:s O");
$json = '{
"serialdata": [
echo $json;

Arduino :

int led = 13;

void setup() {
  pinMode(led, OUTPUT);

void loop() {
  char comRead =;
  if (comRead == 65) {
    digitalWrite(led, HIGH);
  } else {
    if (comRead == 66) {
      digitalWrite(led, LOW);
  for (int temp=0; temp <= 255; tempp++){
      Serial.println(temp); //Layer temp

Please help me for this or just give me an example for php showing or get data without refreshing the page and when i visit the page to see real time the temperature!!!

You open the serial port, resetting the Arduino. You wait 2 seconds, and then read whatever the Arduino sent.

Meanwhile, the Arduino is resetting, then reading serial data. Why? You never send anything. The Arduino then sends a bunch of strings containing useless information, until the PHP script closes the serial port, resetting the Arduino again.

The Arduino should do nothing until the PHP script asks for information. Then it should provide ONE response and shut up.

The PHP script should NOT be opening and closing the serial port. Your web page needs an "open the port" button, and a "close the port" button. Those buttons should do what the names suggest.

Periodically (NOT continuously), the PHP script should ask the Arduino for data.

The data provided by the Arduino should NOT be the value of the index of a for loop.

This Python demo shows a system for communicating with an Arduino and takes account of the issues @PaulS has mentioned. The principles apply equally to PHP.

Can you create Threads in PHP? In some of my Python projects I have a background Thread running to handle communication with the Arduino. That allows me to get debug information without interfering with the main program.