Data sending and receiving through serial port

Hi guys, I’m working with triggering stuff through PHP and the serial class. It’s working well but I’m wonder how I can return data back to the php script. So basically i’d like to write a letter to arduino, through serial to the script to trigger a something then have the arduino return data back. is this possible? I’d also like to be able to turn the switch off and first check the status of that before sending the signal to fire it. I just need to know how to retrieve data with PHP from the arduino. Thanks.

I'm not sure what you write.
You use a PHP webpage, and you use the Serial library for commands and so ?

I'm new to this, but you can request (a GET request) a file from the Arduino webserver. For example request data.txt.
Instead of retrieving that from the SD card, the Arduino detects "data.txt" and generates on the fly that file (or actually a fake file) and fills it with data.
In the PHP script you have to extract the data from the file.

There must be other ways, since I'm just learning this.
I was trying to make an HTML5 live update code of Arduino webserver, but it is not working yet.

So basically i'd like to write a letter to arduino, through serial to the script to trigger a something then have the arduino return data back. is this possible?

Depends on what operating system you are using. Linux or a variation? Yes. Windows? No.

The Arduino can write data to the serial port. The PHP serial class, due to Microsoft's bungling, can not read from the serial port on Windows. Linus Torvold and company didn't make the same mistakes.

Krodal, I’d love to see what you get working. Basically what I’d like to do is create a php function that would essentially ping the arduino to check the state of a toggle switch before continuing. I can’t seem to find any guidance on how to retrieve the information from the arduino. And yes using MAC and OSX

I can't seem to find any guidance on how to retrieve the information from the arduino.

What have you tried?

http://code.google.com/p/php-serial/

// To write into
$serial->sendMessage("Hello !");

// Or to read from
$read = $serial->readPort();

Paul, when I use readPort I get nothing in return. It will write to the serial monitor of the arduino software but will not return anything to my PHP script.

but will not return anything to my PHP script.

Which, of course, we haven’t seen. Nor, have we seen the Arduino code. Can’t do much more, in this case, than offer encouragement.

Arduino Code

int LED = 14; 
byte inbyte = 0;

void setup() {
  Serial.begin(9600); //open the serial port
  pinMode(LED, OUTPUT); 
  Serial.println("Type f to fire");
  Serial.print(">"); //simulate prompt
}

void loop() {
  inbyte = Serial.read(); //Read one byte (one character) from serial port.
  if (inbyte == 'f') { 
    digitalWrite(LED, HIGH);
    delay(200);
    Serial.print('P');
     digitalWrite(LED, LOW);
     delay(1000);
  }
}

php code

<?php
include "phpserial.php";

// 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/tty.usbmodemfa131");

// Then we need to open it
$serial->deviceOpen();

// To write into
$serial->sendMessage("f");
$serial->sendMessage("s");

// Or to read from
sleep(1);
$read = $serial->readPort();
print_r($read);

// If you want to change the configuration, the device must be closed
$serial->deviceClose();

// We can change the baud rate
$serial->confBaudRate(9600);

// etc...
?>

Have you modified your Arduino to prevent resetting when the serial port is opened and closed?

  inbyte = Serial.read(); //Read one byte (one character) from serial port.

You should not be calling Serial.read() unless you have called Serial.available() and determined that there is at least one byte to read.

     delay(1000);

Why? Why? Why?

PaulS:
Have you modified your Arduino to prevent resetting when the serial port is opened and closed?

  inbyte = Serial.read(); //Read one byte (one character) from serial port.

You should not be calling Serial.read() unless you have called Serial.available() and determined that there is at least one byte to read.

     delay(1000);

Why? Why? Why?

Thanks for your help paul. yes, there is a 220ohm resistor acting as a jumper from 5v to "reset" pin. But looking around it seems that might not necessarily fix it. How would I know that the board is resetting?

Secondly, I was following this tutorial for the serial communication. I guess when I tried to modify it for my need I lost that bit of code (Serial.available).

Lastly, the delay was to prevent repetitive button pushing that would trigger the event.

How would I know that the board is resetting?

Add an LED, with current limiting resistor, to a digital pin. In setup(), flash that LED a couple of times.

If the LED flashes when PHP opens the serial port, or when it closes it, then the resistor is not doing its thing correctly.

Lastly, the delay was to prevent repetitive button pushing that would trigger the event.

Unless you are incredibly slow, the first 1/5 second delay would have been plenty.

But, take this as an opportunity to learn how to detect switch transitions, instead of just detecting whether the switch is pressed or released. Only send the data on the correct transition.

Hint: You need to record the previous state, and compare the current state to the stored state. If different, a transition has occurred. The current state then tells you whether it was to pressed or to released. Update the previous state with the current state at the end of loop().

I got a 10uf capacitor and the board isn't reseting with PHP call. I also removed the delay. Does my arduino/php look configured correctly to receive the serial data from the arduino.

Does my arduino/php look configured correctly to receive the serial data from the arduino.

Look at the order of operations. Open the port, send some data, read some data, close the port, set the baud rate. Looks to me like the order could be improved a little bit.

Paul, Thanks so much, got it all figured out!