Hello everybody. I have a very simple program that call a php file.
the .ino is this:
"declaration...."
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(2000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /myFile.php HTTP/1.1");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
client.println("Connection: close");
client.println();
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;)
;
}
}
the php code only write "php correctly called" once it's called.
If I run the code many time resetting arduino this is what I obtain:
connecting...
connected
php correctly called
disconnecting.
connecting...
connection failed
disconnecting.
connecting...
connected
php correctly called
disconnecting.
connecting...
connected
php correctly called
disconnecting.
So, sometimes but not always the "client.connect(server, 80)" return false...
If i call many time the php script from a browser I have no problem so I suppose it is not a server problem because apache always answer correctly.
Do you have any idea of why sometimes the client connect fails?
Thanks in advance for your help.