Passing input to Arduino through Web Server

Hi guys,
I'm having trouble with my project that requires me not to use any shield.
So, I'm only using USB cable as the only connection to my laptop. I basically start a simple project by creating my own web server in my laptop and a simple PHP script to control an LED in the Arduino (the basic led in pin 13).
Here is my Arduino code:

int Bytein = 0;
int pin = 13; 

void setup() {                

  Serial.begin(9600); 
  pinMode(pin, OUTPUT);     
}

void loop() {

if (Serial.available() > 0) {
Bytein = Serial.read();

if(Bytein=49)
{
digitalWrite(pin, HIGH);   

}


else if(Bytein=48)
{
  digitalWrite(pin, LOW);  
} 

}

}

And this is my php script

<?php

$option = "";

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

$option = $_GET['action'];

switch ($option) {
case "on":
$afile = fopen("com7", "w");
fwrite($afile, chr(1));
fclose($afile);
break;

case "off":
$fp = fopen("com7", "w");
fwrite($afile, chr(0));
fclose($afile);
break;


}
}

?>

<html>
<body>
<center>

<p>
<font size="4">Flick Switch</font>

<a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">On</a>
<a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">Off</a>
</p>

</center>
</body>
</html>

Formost, it can only turn the LED on, and nothing else can happen at the first either on/off button.

Can anyone help me with this problem?
I appreciate it so much... Thank you before

I'm not that familiar with PHP, but you probably could do what you want to do using the apache web server and a batch file (the auto reset feature of the arduino would need to be defeated).

PHP is an interpreted language optimized for web servers (like Apache).

Back to the serial question, there are PHP libraries/classes for interfacing with the serial port, but I think all of them require the Direct IO extension to work.

If you're not comfortable building PECL extensions like Direct IO you'd probably be best to have your PHP script shell out to an external program to do the actual communication. The Interfacing with Software page provides many examples.

It looks as if your PHP page will open and close the serial port for each request. Normally, when you open the serial port this causes the Arduino to reset. You could confirm whether that's happening by writing a sketch which does something obvious when it resets.

Also, it looks to me as if your page is writing ASCII characters 0 (NUL) and 1 (SOH) but your sketch is looking for 48 ('0') and 49 ('1').

PHP is an interpreted language optimized for web servers (like Apache).

If the serving machine is a windows machine, PHP mat be no more functional than a simple batch file. It seems PHP on a windows writes to the serial port using the windows command processor, just like a simple batch file.

Hi guys, I actually did some revision to my project and it works...
Just want to share it with you guys, Correct me if it's just somehow works, but foremost I'm glad that it worked as what I wanted it to be.

On the arduino code

int Bytein = 0;
int pin = 13; 

void setup() {                

  Serial.begin(9600); 
  pinMode(pin, OUTPUT);     
}

void loop() {

if (Serial.available() > 0) {
Bytein = Serial.read();
digitalWrite(pin,HIGH);
if(Bytein == 1)
{
digitalWrite(pin, HIGH);   
Serial.println("1");
}


else if(Bytein == 0)
{
  digitalWrite(pin, LOW);  
  Serial.println("0");
} 

}

}

I didn't change anything in my php script,
I think the magic works in 'chr(1)' and Arduino will still read it as '1'. The ASCII converter works in PHP. Correct me if I'm wrong :slight_smile:

Looking at your earlier code, you had:

if(Bytein=49)
{
digitalWrite(pin, HIGH);   

}

Should be (Bytein == 49) instead of (Bytein = 49). The latter always evaluates to true. You appear to have fixed that bug in your latest revision however.

digitalWrite(pin,HIGH);
if(Bytein == 1)
{
digitalWrite(pin, HIGH);   
Serial.println("1");
}


else if(Bytein == 0)
{
  digitalWrite(pin, LOW);  
  Serial.println("0");
}

Turn the pin on. If the byte was 1, turn the pin on. If the byte was 0, turn the pin off. Hmmm, not exactly the way I would have done it.

What happens if the byte is 32?

I was actually testing this project, therefore I was more concerned on these 2 values. Well I guess to make this project more reliable I'd put 'else' so that it won't turn on/off the light if in any case, other values (such as byte 32) becomes the input. Correct me if I'm wrong :slight_smile:

Well I guess to make this project more reliable I'd put 'else' so that it won't turn on/off the light if in any case, other values (such as byte 32) becomes the input. Correct me if I'm wrong

You should not turn the light on if the value isn't 1 and you shouldn't turn it off if the value isn't 0, if your goal is only to respond to those two values. Most importantly, the first call to turn the light on needs to go.