Ethernet library : More explanations

Hi Everybody,

First of all I thank people who have worked to build the Arduino Ethernet Shield and develop the Ethernet library.

Then I have 2 requests :

  • Could it be possible to have a more detailed documentation for the Ethernet library
  • Could it be possible to have a client example (quite the same as Client example) which request periodically the same page. I tried to modify Client example to do that but it doesn't work very well (half time connection fails).

Thank you for your support.

What details are you looking for?

If you post the code, we might be able to offer ideas about how to fix it.

Purpose of this sketch is to request the same web page every 10 seconds.

Here is Arduino Code:

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,101 };
byte server[] = {xxx,xxx,xxx,xxx};


Client client(server, 80);

enum CLIENT_STATE
{  
      STEP1, STEP2, STEP3, STEP4
};

static CLIENT_STATE client_state;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  delay(1000);
}

void loop()
{
  
  if (client_state==STEP1)
  {
    Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    
    client.println("GET /ethershield_log/save.php?pwd=secret&client=192.1.1.101&status=online HTTP/1.0");
    client.println("Host: www.mywebsite.fr");
    client.println("User-Agent: AVR ethernet\r\n");
    client.println("Accept: text/html");

    client.println();
  

    client_state=STEP2;
    delay(3000);
  } else {
    Serial.println("connection failed");
    delay(1000);
    client_state=STEP1;
  }
  }
  
    if (client_state==STEP2)
  {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
    client_state=STEP2;
  }
  else
  {    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    delay(7000);
      client_state=STEP1;
    }

  }
}

Here is Serial Monitor log :

connecting...
connected
HTTP/1.1 200 OK
Date: Mon, 17 Nov 2008 07:03:53 GMT
Server: Apache/2.2.X (OVH)
X-Powered-By: PHP/4.4.9
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html


disconnecting.
connecting...
connected

disconnecting.
connecting...
connection failed
connecting...
connected

disconnecting.
connecting...
connected
HTTP/1.1 200 OK
Date: Mon, 17 Nov 2008 07:04:25 GMT
Server: Apache/2.2.X (OVH)
X-Powered-By: PHP/4.4.9
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html


disconnecting.

Thanks

Ok just to make it clear that I don't have any Arduino Ethernet shields but I will try and offer my assistance.

Briefly looking at your code, I can see a couple of errors that could be contributing to the unexpected events you are encountering.

  1. The line:
client.println("User-Agent: AVR ethernet\r\n");

Should be:

client.println("User-Agent: AVR ethernet");

The extra carriage return and newline characters you have would cause the server to think you have completed your HTTP GET request.

  1. You are disconnecting the Client at the wrong time. You should change your "if (client_state==STEP2)" block to be this (the yellow highlighted line is the line I changed):
    if (client_state==STEP2)
  {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
    client_state=STEP2;
  }
  [glow]else if (!client.connected())[/glow]
  {    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    delay(7000);
      client_state=STEP1;
    }

  }
}

Hope this helps.

Thank you for these suggestions. It seems to work.

I've also discovered that it didn't work very well on one of my personal website but very well on another one.
It seems that my first personal website doesn't answer to each GET HTTP request. I don't know why ?
With my second personal website, I have 100% of success.