I've been testing various programs and I noticed that the Arduino hangs for about 35 seconds when using the Arduino to load a small status message from an Apache/PHP webserver.
A lot of people are running into problems with the Ethernet shield and/or Ethernet library, here are some topics and possible solutions:
Reducing ethernet traffic by copying new client files to ethernet library directory: http://code.rancidbacon.com/LearningAboutArduinoEthernet
Using client.status() to find out what's wrong:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235991468
Modding the Ethernet shield to fix cold start hardware bug:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1225354009/29#29
Client.stop() not working properly:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235494406
Use Ethernet2 library:
http://code.google.com/p/tinkerit/source/browse/#svn/trunk/Ethernet2%20library/Ethernet2
Ethernet2 library topic:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1233499187
All topics have various solutions for the problems that are discussed, but I think the nasty 35 second hang bug still isn't fixed. Here's the code that prints client.status() in an attempt to find out what causes the Arduino to hang:
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 66 }; // Arduino IP
//byte server[] = { 64, 233, 187, 99 }; // Google
byte server[] = { 192, 168, 1, 17 }; // desktop pc running wampserver 2.0 and a simple php script that echoes whatever value is supplied in the querystring// int receivingData = 0;
int isConnected = 0;
int timer = 0;
Client client(server, 80);void connectMe(){
client.stop();
delay(500);
Serial.println("connecting...");if (client.connect()) {
Serial.println("connected");
isConnected = 1;
client.println("GET /?rfidkey=12345678 HTTP/1.0");
client.println();
Serial.print("client.status = ");
Serial.println(client.status(),DEC);
} else {
Serial.println("connection failed!");
Serial.print("client.status = ");
Serial.println(client.status(),DEC);
}
}void disconnectMe(){
Serial.println();
Serial.println("disconnecting...");
isConnected = 0;
client.flush();
client.stop();
if(!client.connected()){
Serial.println("disconnect error!");
}else{
Serial.println("disconnected");
Serial.print("client.status = ");
Serial.println(client.status(),DEC);
}
}void setup(){
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
connectMe();
}void loop(){
if (client.available() && client.connected() && isConnected == 1) {
char c = client.read();
Serial.print(c);
} else if (!client.connected()){
if (isConnected == 1){
disconnectMe();
}
delay(1000);
Serial.print("client.status = ");
Serial.println(client.status(),DEC);
}
}
And here's a copy of the serial monitor window:
connecting... connected client.status = 28 HTTP/1.1 200 OK Date: Sun, 29 Mar 2009 02:28:55 GMT Server: Apache/2.2.11 (Win32) PHP/5.2.9-1 X-Powered-By: PHP/5.2.9-1 Content-Length: 8 Connection: close Content-Type: text/html12345678
disconnecting...
disconnected
client.status = 24
client.status = 24 <AFTER THIS IT HANGS FOR 35 SEC>client.status = 0 <35 SECONDS LATER: RETURNED TO LOOP>
client.status = 0
client.status = 0
client.status = 0
client.status = 0
client.status = 0connecting... <HANGS FOR 35 SECONDS>
connection failed!client.status = 0
client.status = 0
client.status = 0
client.status = 0connecting...
connection failed!
client.status = 23
The results are pretty random.. sometimes I can do sequential requests within a minute, but most of the time it just cannot connect :-/ I think is has to do with the return value of client.status() but I'm not sure... I've tried this code on an Deumilanove with both a 168 and 328. Running arduino 15 on XP. Tried various webservers, but all run Apache.. finally installed WAMP on my local pc to make sure the problem isn't caused by routers/webhosts...
So, Arduino DEV team or any other Arduino pro's, how can we solve this problem? It would be very helpful to update the Wiki or reference.. to save other people the problems that most users encounter when trying to load something over http... Time to add solutions from various topics to Arduino 0016 ?
Edit: another thing I noticed is that client.connect, client.stop and client.connected aren't color coded in the IDE...
Edit 2: similar problem described here: http://forums.ladyada.net/viewtopic.php?f=31&t=10192