turn on/off a with php

hi guys.... can you help me?? i want simply turn on/off a led with PHP.. can you help me? pleaaase! :'(

You're going to need to provide more info; your post seems to indicate you have no experience with the Arduino; but I don't know that for certain...

What platform are you running PHP on - windows or *nix? That will make a difference, as *doze is notorious for making serial port access difficult.

Have you gotten the blink sketch running? Have you built a sketch to control an LED from the serial monitor? Are you familiar with serial communications using PHP?

Are you trying to run before you can walk?

Actually, PHP on Windoze works just fine for writing to the serial port. It's reading from the serial port that Mr. Gates didn't feel was something this his OS needed to support.

Actually, PHP on Windoze works just fine for writing to the serial port. It's reading from the serial port that Mr. Gates didn't feel was something this his OS needed to support.

Bzzzzt! One can read from and write to the serial port in the windows OS. Perhaps you ment to say that PHP can't read from the serial port, just as a batch file or VBscript can't. Use an appropriate application and there is no problem.

PHP can't read from the serial port, just as a batch file or VBscript can't.

And why not? Because Mr. Gates didn't think it was necessary for anything other than proprietary, closed source to be able to.

That you can do so if you acquire Visual Studio is what irritates me. Although not as much as it used to, since they do not offer express versions of Visual Studio for free.

If you limit your self to visual studio, then you may have problems. I made a simple exe using freebasic that when used in the apache cgi-bin folder seemed to be able to read/write the serial port when accessed from a web page. I don't understand all the effort being put into PHP. When it came out I took a light look at it and didn't see anything that made it note worthy in doing I/O with hardware. I thought it looked like a file manipulating application to maybe replace the old work horse PERL.

Well, PHP is great for dynamic web pages, form processing, linking to MySQL, etc. Serial port acess is possible, but not one of it's strong points.

Well, PHP is great for dynamic web pages, form processing, linking to MySQL, etc. Serial port acess is possible, but not one of it's strong points.

Then why keep trying to use it for serial port access? Keep PHP for the processing you mention and move to something else for the serial port.

Well, PHP is great for dynamic web pages, form processing, linking to MySQL, etc. Serial port acess is possible, but not one of it's strong points.

I would say the problem lies mainly with Windows, not PHP. PHP is fairly robust software if you run it on say, BSD or Linux (not that you can't have serial issues there, but it is far more likely that you will be able to correct them fairly quickly with a little man page browsing).

All you have to do is a file open, fopen(), to the device in question (/dev/ttySx or /dev/ttyUSBx, where "x" is the device number) - basically the same as you would in C; as long as your permissions are good for access, and you can get you stty settings proper, you'll be good to go.

@PaulS, @cr0sh

Why do you think Windows serial port programming is so bad? Why do you think it's so much different than on *nix?

For example, the following program works fine receiving data from an Arduino...

int main(int argc, char* argv[])
{
  FILE* f;
  char* rv;
  char Buf[256];

  f = fopen( "COM9", "r" );
  if ( f != NULL )
  {
    rv = fgets( Buf, sizeof(Buf), f );
    fclose( f );
  }
  return( 0 );
}

My guess is the same snippet would work on *nix with one minor change (the "filename").

I gave up on coding for Windows a looong time ago; I would only do it now if I had no other choice.

It isn't that I think it is bad, or radically different; I have heard that it can be fussy to do port stuff under Windows - but it can be fussy under *nix, too.

But at least, at the very worst, I have the source to reference under *nix. I don't have that under Windows, and likely never will. I have been burned one too many times by closed-source issues (not just under Windows, either - this happened to me on my old TRS-80 Color Computer, too) - I won't go back to closed source systems, except under extreme duress.

Right now, the only major portion of my workstation that is closed source is the nvidia drivers I use for 3D - I don't like it, but there really isn't a choice if you want high framerate 3D, unfortunately.

Now, I know none of this has to do with the problem at hand, and it already sounds too preachy and political, so I won't say anything more about it...

My responses to the PHP issue have always been to the "How do I do this with PHP?" or "How do I communicate with the Arduino from a web server?" type questions.

Native C programming on a web server is not easy. Using C# or ASP.net is easy, as long as the web server is running Windoze. The majority of web servers do not.

On a web server that is hosted on *nix, PHP is a great tool for dynamic web page development, including forms processing. Getting the data to the serial port is easy. Getting data from the serial port, and displaying it in the form is easy.

On a web server that is hosted on Windows, PHP is a great tool for dynamic web page development, including forms processing. Getting the data to the serial port is easy. Getting data from the serial port, and displaying it in the form is impossible, given the closed source nature of Windows.

i’m using windows xp…the blink skecth works…everything from the serial monitor works…

these are my codes:

php

<?PHP
if (isset($_GET["t"]))
{
  if ($_GET["t"]==0)
    {
       $fp=fopen("com3", "w");
       fwrite($fp,chr(0));
       fclose($fp);
      }else
      {
       $fp=fopen("com3", "w");
       fwrite($fp,chr(1));
       fclose($fp);
      }
}
echo "<a href=ard.php?t=0>on</a>

";
echo "<a href=ard.php?t=1>off</a>";
?>

and arduino

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

void loop() {
    if (Serial.available() > 0) 
    {
      usbnumber = Serial.read();
    if (usbnumber == 48) {
    digitalWrite(ledPin,HIGH);
   }

   if (usbnumber == 49) {
    digitalWrite(ledPin,LOW);
    }

 usbnumber=0;  
      
    }
}

is a very simple code…but i think that arduino don’t understand what i send with php

I’ve spent 2 hours trying to get PHP to send data using fopen and fwrite.

I attached a serial LCD to another pin, and used NewSoftSerial to send data to the LCD. I echoed to the LCD what was received from the serial port, when fwrite wrote data.

Data IS sent to the Arduino when fwrite is called. However, no matter how much data is written, the Arduino never sees more than one character on the serial port. That one character has no correlation that I could detect to the data that fwrite supposedly wrote.

Conclusion: Using PHP on Windows to write to the serial port is an exercise in frustration.

Have you heard of the anywhere light? It was in the Exhabition a couple of days ago. There is a web app comunicating with processing which sent 1 or 0 via Serial to the Arduino. http://anywherelight.net/

I've spent 2 hours trying to get PHP to send data using fopen and fwrite

Would you like help getting it to work? (bearing in mind my assitance may be rather limited)

In your code snip below, are you sending a "0" character, or are you sending a null byte? I don't know what chr() in php sends, but in some programming it may be the hex value of the character (or byte) desired. You might try something like the bottom lines to see if they work.

fwrite($fp,chr(0));

fwrite($fp,chr(48)); fwrite($fp,chr(49));

@deio:

Opening with “r+” worked for me…

       $fp=fopen("com3", "[glow]r+[/glow]");

It didn’t seem to make any difference but a flush may help…

       fwrite($fp,chr(0));
       [glow]fflush( $fp );[/glow]
       fclose($fp);

You may need to put in a small delay…

       fwrite($fp,chr(0));
       fflush( $fp );
       [glow]sleep( 1 );[/glow]
       fclose($fp);

And zoomkat is correct. You’re sending an ASCII null but checking for an ASCII zero. Try his fwrite’s.

Please report back if any of these suggestions help.

Just as an experiment, you may want to check the following. Copy the below batch file code and paste in notepad. Save on the desk top as on.bat and off.bat. I assume you are using a baude rate of 9600 and com3. Double click the files to run them and see if it turns your LED on and off.

on.bat

@echo off
mode com3:9600,N,8,1 >nul
echo 1 >com3

off.bat

@echo off
mode com3:9600,N,8,1 >nul
echo 0 >com3