Xammp - Arduino - Connection works but no data transfer

Hey guys,

first I am a newbee and bought today my first arduino board. My mother tongue isn't english so sorry for my spelling. :blush:

I have already a connection between my Arduino (UNO) and my Xammp. But the problem is that i can't broadcast (or transfer?) my data.

Here my php script:

if($_GET['command']) {

    $fp =fopen("com7", "w+");
    echo "<p>Wrote $i </p>";
    fwrite($fp,1);
    
    fclose($fp);
    }

My Arduino Code:

// Includes

#include <Servo.h>
int ledPin = 8;

int USBnumber = 0;

void setup()
{
   pinMode(ledPin, OUTPUT);
  Serial.begin(9600);   
}

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

    if (USBnumber > 0) {
        if (USBnumber == 1){ 
            digitalWrite(ledPin, HIGH);
        }else if(USBnumber == 2){
            digitalWrite(ledPin, LOW);
        }else{
             
         //"error"-Signal
          for(int i = 0; i < 10; i++) {
            digitalWrite(ledPin, HIGH);
            delay(100);
            digitalWrite(ledPin, LOW);
            delay(100);
          }
                 
        }
        Serial.print(USBnumber);
        USBnumber = 0;
    }
}

Always my arduino makes my error-Signal. I think my USBnumber isn't 1 or 2. So my php script is wrong? But why or where?

In the php I tried stuff like this too, but this wasn't succesful too

  fwrite($fp,chr(1));
//or
  fwrite($fp,chr("1"));

So where is my mistake? Or how can I prof what I send to my arduino? Or what my arduino receive?

I hope u will understand me. I'm thankful for help. :slight_smile:

Hi kuzdu

If you use serial monitor to send the character to the Arduino program (instead of the PHP script), what do you see printed out by this statement?

Serial.print(USBnumber);

Regards

Ray

It's quite strange.

When i open my monitor and execute my php code, I get following message:

Warning: fopen(com7): failed to open stream: Permission denied in C:\xampp\htdocs\arduino\test.php on line 18
Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\arduino\test.php on line 22
Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\arduino\test.php on line 24

I reduced my Arduino Code like in this example: http://arduino.cc/en/Serial/read

So I'm sorry but i can't say, which message is coming with this code:

Serial.print(USBnumber);

Edit: I use in my php file:

chmod("test.php", 0755);

When i open my monitor and execute my php code, I get following message:

So, why do you expect the Arduino to do anything?

    echo "<p>Wrote $i </p>";
    fwrite($fp,1);

I can't imagine the relationship between what the echo statement says and what the code does. Perhaps the echo statement needs to be:

    echo "<p>Mary had a little lamb. It's fleece was white as snow...</p>";

In any case, opening the serial port (as the PHP script is trying to do), resets the Arduino. Closing the serial port, as the PHP script is trying to do, also resets the Arduino. Since you do not wait for the Arduino to reset before sending it data, and you reset it again after sending it data, I can't see that the Arduino is actually going to accomplish anything. Therefore, I can't see the purpose of this exercise.

I can't imagine the relationship between what the echo statement says

 echo "<p>Wrote $i </p>";

fwrite($fp,1);

Yeah ur right, there is no relationship. Originally my code was like this:

 echo "<p>Wrote $i </p>";
    fwrite($fp,$i);  //or    fwrite($fp,chr($i));

In any case, opening the serial port (as the PHP script is trying to do), resets the Arduino. Closing the serial port, as the PHP script is trying to do, also resets the Arduino. Since you do not wait for the Arduino to reset before sending it data, and you reset it again after sending it data, I can't see that the Arduino is actually going to accomplish anything. Therefore, I can't see the purpose of this exercise.

Now I use a sleep. You mean it like this or?

$fp = fopen("com7", "w+") or die("can't open file");
    $i = ($_GET['command'] > 0) ? $_GET['command'] : 0;
  
    echo "<p>Wsrote $i </p>";
    fwrite($fp, chr($i));

  sleep(3);   //Now here is a sleep.. some time for the arduino

    fclose($fp);

But when I open my serial monitor and just then, there is still the error: fopen(com7): failed to open stream: Permission denied

Maybe I use the wrong port? But my IDE says "Arduino UNO on COM7"

Okay it seems to be impossible...

found this: Weather Data Progress – Localhost not working, waiting for Ethernet Shield | Project Blinky

What do u think?

When i open my monitor and execute my php code, I get following message:

Can I check something? You are running the PHP script on your PC? And that PC is connected to the Arduino via USB?

If you try to talk to the Arduino through the IDE serial monitor AND run another program to communicate via the Arduino serial USB, only one of them will work. They can't both have the serial port open at the same time.

If you try to talk to the Arduino through the IDE serial monitor AND run another program to communicate via the Arduino serial USB, only one of them will work. They can't both have the serial port open at the same time.

Hmm that makes sense... But what do you suggest? My plan was to control the arduino via xampp. Do u have any ideas how I can realise that?

My plan was to control the arduino via xampp. Do u have any ideas how I can realise that?

Don't use the Serial Monitor app at the same time.