Activate LED from PHP

Hello,

I just started working with the Arduino Leonardo and I am trying to learn a few things. I did a few of the tutorials but then decided that I want to be able to control the led using php. The code from the Arduino IDE appears correct because I can toggle the led using the serial monitor and through python. But when I try to use php, the led will not activate. I will post the code below. Any ideas why this wouldn’t work?

Arduino Code (Original idea from http://www.instructables.com/id/Control-an-Arduino-with-PHP/)

int led = 13; // used for an led to test stuff
int usbnumber = 0; //this variable holds what we are currently reading from serial


void setup() { //call this once at the beginning
    pinMode(led, OUTPUT);
    Serial.begin(9600); //start up serial port
}

void loop() { //main loop
    while (!Serial);
    usbnumber = Serial.read(); //store it in the usbnumber variable
    if (usbnumber > 0) {
      Serial.println(usbnumber);
    }
    delay(1);
    if (usbnumber == 49) { //if we read something
      digitalWrite(led, HIGH);
    }
    if (usbnumber == 48) { //if we do not read something
      digitalWrite(led, LOW);
    }
        usbnumber = 0; //reset
}

Python Code (Using Idle)

import serial
usb = serial.Serial("/dev/ttyACM0")

#ON
usb.write("1")

#OFF
usb.write("0")

Finally, PHP Code (Currently not working?)

<?php

$verz="1.0";
$comPort = "/dev/ttyACM0"; /*change to correct com port */

?>

<?php
	if (isset($_POST["rcmd"])) {
		$rcmd = $_POST["rcmd"];
		switch ($rcmd) {
			case ON:
				echo "ON";
				$fp =fopen($comPort, "w");
				fwrite($fp, "1");
				fclose($fp);
				break;
			case OFF:
				echo "OFF";
				$fp =fopen($comPort, "w");
				fwrite($fp, "0");
				fclose($fp);
				break;
		}
	}
?>
<html>
<body>
	
<center><h1>Arduino from PHP Example</h1><b>Version <?php echo $verz; ?></b></center>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="submit" value="ON" name="rcmd">
<input type="submit" value="OFF" name="rcmd">
</form>
</body>
</html>

I am using Ubuntu 12.04 to edit the Arduino Code.
Any help will be appreciated.
Thanks in advance,

    while (!Serial);

This does not belong in loop().

Opening and closing the serial port resets the Arduino. Your PHP script is not allowing time for the Arduino to reset.

If I understand correctly, I need to "Pause" the php script before writing to the serial port?

If I understand correctly, I need to "Pause" the php script before writing to the serial port?

Yes, but as soon as you close the serial port, the LED is going to go out.

For testing purposes, is there a way to force the port to stay open in php?

thanks for the help so far!

For testing purposes, is there a way to force the port to stay open in php?

Stick in a while loop that never ends. Close the port after the while loop ends.

Got it working... sort of. I found the apache was returning permissions errors from the php. I ran chmod 777 on the port and I can now turn the light on or off. The new problem is that the permissions reset if I re-upload or reset or unplug the Arduino. Is there a way to permanently force the permissions?

SOLUTION!

http://forums.phpfreaks.com/topic/283198-php-fopen-serial-port-access-denied/#entry1454998

Just in case the site is down for some reason:

http://forums.phpfreaks.com/topic/283198-php-fopen-serial-port-access-denied/#entry1454998

kicken

Note that if you are running this via apache which your error message indicates, you need to consider the user that apache is executing the script as, not what your login user is when determining the permissions and groups. For example, you'd need to add apache's user to the dialup / uucp groups, not yourself.

THANK YOU!!!!

It was so simple but I over looked it.

I had tried adding the user apache to the group but was getting a user error. Then I ran ps aux | egrep '(apache|httpd)' which showed me that the user was www-data. Then I added www-data to the group dialout using sudo usermod -a -G dialout www-data and after I logged out and back in it works!