PHP Serial Communication

Hey guys!

I've looked at a lot of examples on how to communicate using PHP. However, I'm running into a problem.

I'm trying to set the PWM of an LED by using PHP. For example, I would like to send a value from 0 to 255 and have the Arduino write the value to the LED.

The problem i'm having is that i can't seem to find the appropriate way to send/receive the value to properly set the PWM.

I'm using fwrite() in PHP to send commands but I need help to determine how to process the value. I tried using a byte array and couldn't really get that working correctly. Let me know if you need anything else.

Thanks in advance.

Let me know if you need anything else.

PHP code and sketch code would be nice...

(remember to use the # button when posting!)

:slight_smile:

You can put Bitlash on the Arduino and send commands to it over the serial port to control the Arduino.

Bitlash is an open source interpreter for Arduino, available at http://bitlash.net -- for example in your application you might send:

a5=128

to set the PWM value on pin 5 to 50%.

Happy hacking,

-br

http://entropymouse.com

have you had a look at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264895784/32 and other entries on php and serial comms ?

main issue seems to be autoreset of arduino, which can be hacked by a resistor on the reset pin (or just buy a clone with a switch).

Good luck !

Arduino Code:

int ledPin = 13;
byte usbnumber = 0;
void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0) {
        usbnumber = Serial.read();
        Serial.write(usbnumber);
    }

    if (usbnumber > 0) {
        if (usbnumber == 'a'){
            digitalWrite(ledPin, HIGH);
            delay(300);
            digitalWrite(ledPin, LOW);
            delay(300);
        }else{
            digitalWrite(ledPin, HIGH);
            delay(300);
            digitalWrite(ledPin, LOW);
            delay(300);
            digitalWrite(ledPin, HIGH);
            delay(300);
            digitalWrite(ledPin, LOW);
            delay(300);
        }
        usbnumber = 0;
    }
}

PHP Code:

<?php
$fp =fopen("com9", "w");
fwrite($fp, chr(97));
fclose($fp);
?>

When i run the PHP code, the LED flashes twice.

One thing that happens is that the Arduino resets when the serial port is opened by the PHP script.

The PHP script then immediately sends data, which the Arduino is not yet ready to receive, still being in the process of resetting.

The PHP script then closes the serial port, causing the Arduino to reset again.

Perhaps the LED blinking that you see is simply the Arduino reset occurring (twice), not the reaction from receiving a non-a value on the serial port.

I took that into consideration and put a 120ohm resistor between the RESET and 5V pin. I also flashed another LED in the setup() and it never came on unless i hit the reset button.

I used PortMon to verify that PHP was sending the correct data and it is. The Rx LED on the Arduino flashes when i send the character. If i use the Serial Monitor and send an 'a' it works correctly, just not with PHP.

Is there anything else I could try?

Only other thing I can think of is to change this line:

fwrite($fp, chr(97));

Why go find an ASCII table to look up the ASCII code for the letter a just so you can tell PHP to convert the code back to a letter. Just send an a:

fwrite($fp, "a");

I tried that as well and still no luck. I'm not sure what the problem is.

Are you working on Windows or Linux?

I'm working on Windows 7 running IIS 7 with the latest version of PHP.

That's too bad. PHP on Windows can't read from the serial port. It's a failing of some underlying Microsoft software. Otherwise, you could have the Arduino send serial data back, which would make debugging easier.

I agree. Not sure what else I can do at this point.