Web based LED control

So ive been wanting to do a “groduino” type project that would control lights and water pumps etc. so i figured i would start out with a little web based LED control. Little background I have a Ubuntu LAMP server running in virtual box on my windows 7 box. trying to turn 2 LED’s on and off.I have just been reading several tutorials about this and have combined a couple of them to come up with this umm “solution”. I am a total newb at Linux and PHP so I hope it is something simple that I am missing.

I can get the lights on the Arduino (uno) to turn on and off by directly sending values over the serial monitor, but when I try to control it from the web page it only flashes the green led 3 times instead of turning one on or off.

here is the arduino code

int ledPin13 = 13; // the pin that the green LED is attached to
 int ledPin12 = 12; // the pin that the red LED is attached to
 int incomingByte;      // a variable to read incoming serial data into
 
 void setup()
 {
  
   Serial.begin(9600); // initialize serial communication
 
   pinMode(ledPin13, OUTPUT);  // initialize the green LED pin as an output
   pinMode(ledPin12, OUTPUT);  // initialize the red LED pin as an output
 }//end setup
 
 
 void loop() {
   // see if there's incoming serial data:
   if (Serial.available() > 0) {
    
     incomingByte = Serial.read(); // read the oldest byte in the serial buffer
//Preform the code to switch on or off the leds
   
   if (incomingByte == '1')
   {
   digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on
   }
   
   if (incomingByte == '2')
   {
   digitalWrite(ledPin13, LOW); //If the serial data is 1 turn red LED off
   }
 
   if (incomingByte == '3')
   {
   digitalWrite(ledPin12, HIGH); //If the serial data is 2 turn green LED on
   }
  
   if (incomingByte == '4')
   {
   digitalWrite(ledPin12, LOW); //If the serial data is 3 turn green LED off
   }

   }
 }

and the web page code

<?php

$gon = 1;
$goff = 2;
$ron = 3;
$roff = 4;

if (isset($_GET['action']))
{
//open write
$fp =fopen("/dev/ttyACM0", "w");

if ($_GET['action'] == "greenon")
    {
        //to turn the GREEN LED ON, we issue the command 
        fwrite($fp, chr($gon));
    }

if ($_GET['action'] == "greenoff")
    {
        //to turn the GREEN LED OFF, we issue the command 
        fwrite($fp, chr($goff));
    }

if ($_GET['action'] == "redon")
    {
        //to turn the RED LED ON, we issue the command 
        fwrite($fp, chr($ron));
    }

if ($_GET['action'] == "redoff")
    {
        //to turn the RED LED OFF, we issue the command 
        fwrite($fp, chr($roff));
    }

//Close write
fclose($fp);
}

?>
<html>
<head>
<title>Arduino Project 1: Serial LED Control</title> 
</head>
<body>

<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenon" ?>">
Click here to turn the GREEN LED on.</a></p>

<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenoff" ?>">
Click here to turn the GREEN LED off.</a></p>

<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=redon" ?>">
Click here to turn the RED LED on.</a></p>

<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=redoff" ?>">
Click here to turn the RED LED off.</a></p>

</body>
</html>

Every time you open and close the serial monitor, the Arduino resets (reboots). You are then not waiting long enough after the reboot for the Arduino to be ready to receive serial data. Therefore, that serial data is lost. You then reset the Arduino again.

You need to either prevent the Arduino from restarting on serial port open/close (requires hardware modification) or only open/close the serial port once, not every time you send data.

Where would the appropriate place to put the " $fp =fopen("/dev/ttyACM0", "w"); " be?

i tried putting it at the very top of the php block on the web page and giving it its own block, but i still get the same results.

Thanks for the reply

You’ll need to enable sessions in PHP, and open the serial port, saving the handle to the port in the session space. Each time the LED state is to change, use the saved serial port handle.

hmm ok that part is over my head. ive been reading this article http://devzone.zend.com/article/1312 but again most of it is over my head. This is my first time ever trying to use PHP. From the article im not sure if I need to change some settings in the php.ini or just use session_start() at the beginning then session_register($fp) . The "handle" just refers to $fp that im setting to the serial port? so the line fwrite($fp, chr($gon)); is " using the saved serial port handle " ?

you don't need to change the ini file, just call session_start() at the start of your code. session_start() must be called before any PHP output or you will get an error.

i have added the two parts i mentioned to the code and I am still getting the 3 flashes from LED on pin 13

so from my understanding every time I write to the serial port fwrite($fp, chr($gon)); it is re opening the "file"/serial port and resetting the arduino. So how would I write to the serial port without opening the "file" every time / without using fwrite?

i have added the two parts i mentioned to the code and I am still getting the 3 flashes from LED on pin 13

Which means you modified the code, which means you need to post it again, so we can see that you did it right.

so from my understanding every time I write to the serial port fwrite($fp, chr($gon)); it is re opening the "file"/serial port and resetting the arduino.

No. Every time you use fopen() or fclose(), the Arduino resets. Not when you use fwrite().

Just notice the code in your links, you should NOT be using short tags in your code. You also have some PHP syntax errors. You are hoping PHP engine will correctly execute your code. This is a bad idea.

you should use this (the full tag)

<?php echo  "$something"; ?>

Don’t use this

<? echo  "$something"; ?>

So Change this:

<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenon" ?>">
Click here to turn the GREEN LED on.</a></p>

to…

<p><a href="<?php echo ''.$_SERVER['PHP_SELF'].'?action=greenon';?>">
Click here to turn the GREEN LED on.</a></p>

Notice how the syntax highlighter now knows what to do with the code :Dde

ok so here is my code as it stands now. And i really appreciate all the help you guys are giving me. This is my first PHP and arduino project. Im basically trying to modify other code that I have found with out knowing PHP. Any one have a good reccomendation on a book or site so that i can really learn PHP?

<?php
//open write
session_start();

$fp =fopen("/dev/ttyACM0", "w");

session_register($fp);

$gon = 1;
$goff = 2;
$ron = 3;
$roff = 4;



if (isset($_GET['action']))
{



if ($_GET['action'] == "greenon")
        {
        //to turn the GREEN LED ON, we issue the command
        fwrite($fp, chr($gon));
        }

if ($_GET['action'] == "greenoff")
        {
        //to turn the GREEN LED ON, we issue the command
        fwrite($fp, chr($goff));
        }

if ($_GET['action'] == "redon")
        {
        //to turn the GREEN LED ON, we issue the command
        fwrite($fp, chr($ron));
        }

if ($_GET['action'] == "redoff")
        {
        //to turn the GREEN LED ON, we issue the command
        fwrite($fp, chr($roff));
        }


}

?>
<html>
<head>
<title>Arduino Project 1: Serial LED Control</title>
</head>
<body>

<p><a href="<php? echo ''.$_SERVER['PHP_SELF'] . '?action=greenon'; ?>">
Click here to turn the GREEN LED on.</a></p>

<p><a href="<php? echo ''.$_SERVER['PHP_SELF'] . '?action=greenoff'; ?>">
Click here to turn the GREEN LED off.</a></p>

<p><a href="<php? echo ''.$_SERVER['PHP_SELF'] . '?action=redon'; ?>">
Click here to turn the RED LED on.</a></p>

<p><a href="<php? echo ''.$_SERVER['PHP_SELF'] . '?action=redoff'; ?>">
Click here to turn the RED LED off.</a></p>

I am not at home right now to see if the code actually work yet but I will report back.