Ethernet shield works... sometimes....

I have big problems with my Ethernet-shield. i have made this vanilla setup that is based on one of the examples. the only thing it does is to ask for a webpage and then print the result. But, it does not work all the time, it often hangs up for several seconds, not getting a connection and so on.

I have tried the examples and I get the same kind of problems.

Is there a general problem with the Ethernet shields? are there any workarounds?

in the end, the thing i want to do is to collect "likes" from a Facebook page. I found an example of this too, works as badly for me.. some times.. sometimes not at all... Writing something myself is more fun thou, but then I need to know if the technique works as it should first...

Thank you.

No one can tell anything by guessing what your sketch looks like.

Did you try this code? It works well for me. http://playground.arduino.cc/Code/WebClient

Otherwise, as oric-dan suggests, post your code.

my code looks like this:
http://www.asgard.se/test.asp is a testfile that should only show some digits(as string naturally)

but it fails most of the time, but sometimes it collects “

Bad Request (Invalid Hostname)

” as webpage.

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xAF, 0xAE, 0xCD };
char server[] = "www.asgard.se";    // name address 

EthernetClient client;

void setup() {
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);  
  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 /test.asp");
    client.println("Host: www.asgard.se");
    client.println("Connection: close");
  } 
  else {
    Serial.println("connection failed");
  }
}

void loop()
{
  do {
      char c = client.read();
      Serial.print(c);
  } while (client.available());
  
  if (!client.connected()) {
    Serial.println("disconnecting.");
    client.stop();
    // do nothing forevermore:
    while(true);
  }
}

the example seems to work for me, i will try to build out of that and see where I end up. But if someone knows whatswrong with the code i have made, i would be glad to hear whats wrong. .

I added the HTTP/1.1 and the double cr/lf (blank line) to your request. This should work.

  if (client.connect(server, 80)) {
    Serial.println("connected");
    // included HTTP/1.1 here
    client.println("GET /test.asp HTTP/1.1");
    client.println("Host: www.asgard.se");
    // added another \r\n here (blank line)
    client.println("Connection: close\r\n");
  }

My code has all the fault tolerance stuff that will keep it working for more than a couple hours.

Yes! your code works like a charm.. I have messed around with it today trying to do yet another thing, check information from a facebook page.

for an example, i want the data from https://graph.facebook.com/229390667073524 this page(shows information about one of my webpages.

char serverName[] = "graph.facebook.com"; char pageName[] = "/229390667073524 HTTP/1.1";

looked like the obvious choice, but it does not work at all, only get "connected... disconnecting pass1" or sometimes i get a HTTP/1.1 400 Bad Request

is there some way to get the graph information via your code? yours is much better to work from than the examples i have found!!

No protocol (HTTP/1.1) in the page name. That will be inserted in the getPage() call in my code. Just this part.

char pageName[] = "/229390667073524";

hmm. still does not work, i get the following ansver:

HTTP/1.1 403 Forbidden Access-Control-Allow-Origin: * Cache-Control: no-store Content-Type: text/javascript; charset=UTF-8 Expires: Sat, 01 Jan 2000 00:00:00 GMT Pragma: no-cache WWW-Authenticate: OAuth "Facebook Platform" "insufficient_scope" "(#210) Subject must be a page." X-FB-Rev: 884423 X-FB-Debug: DZOCQInSB0OalT2U/YPkl3LZhPUtWjVHfFp8nnzr2Y0= Date: Tue, 23 Jul 2013 09:25:16 GMT Connection: close Content-Length: 89 {"error":{"message":"(#210) Subject must be a page.","type":"OAuthException","code":210}} disconnecting. Pass 1

if you surf http://graph.facebook.com/229390667073524 from a reglular webbrowser you will see that its not the same... Im thinking that 229390667073524 might only be the querystring, and that the real pagename is not revealed in the adress? could that be the case?

This worked for me. I added the slash at the end of that directory name

  if (client.connect(server, 80)) {
    Serial.println("connected");
    // added slash to directory 
    client.println("GET /229390667073524/ HTTP/1.1");
    client.println("Host: graph.facebook.com");
    client.println("Connection: close\r\n");
  }

so char pageName[] = "/229390667073524/"; should work in your example? I still get the same errormessage:(

the code looks like this now, and it does give the same error message as before.
any ideas?

/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Uses POST method.
   Posted November 2012 by SurferTim
*/

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  
  0xFE, 0xAD, 0xCE, 0xAF, 0xAE, 0xED };

//Change to your server domain
char serverName[] = "graph.facebook.com";


// change to your server's port
int serverPort = 80;

// change to the page on that server
char pageName[] = "/229390667073524/";

EthernetClient client;
int totalCount = 0; 
int loopCount = 0;
// insure params is big enough to hold your variables
char params[32];

void setup() {
  Serial.begin(9600);


  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop()
{
  if(loopCount < 30)
  {
    delay(1000);
  }
  else
  {
    loopCount = 0;
    // params must be url encoded.
    sprintf(params,"temp1=%i",totalCount);     
    if(!postPage(serverName,serverPort,pageName,params)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}


byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
  int inChar;
  char outBuf[64];

  Serial.print(F("connecting..."));

  if(client.connect(domainBuffer,thisPort))
  {
    Serial.println(F("connected"));

    // send the header
    sprintf(outBuf,"POST %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",domainBuffer);
    client.println(outBuf);
    client.println(F("Connection: close\r\nContent-Type: application/x-www-form-urlencoded"));
    sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
    client.println(outBuf);

    // send the body (variables)
    client.print(thisData);
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      connectLoop = 0;
    }

    delay(1);
    connectLoop++;
    if(connectLoop > 10000)
    {
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
  }

  Serial.println();
  Serial.println(F("disconnecting."));
  client.stop();
  return 1;
}

It looks like it is telling you it needs a page name if you want to use the POST method. What page are you trying to send that POST request to?

If I use the same request using GET, it works.

Thank you again SurferTim! It works now and you have my gratitude!!

SurferTim: im using your code now and it sort of works, but i get alot of connection failed before it connects as it should every time... im sitting on a 100/100 internetline so it should not be a problem to connect to a server and fetch one simple page, but still it seems like it taking forever.

the ethernet possibilities with an arduino feels limited due to this. Is there a solution to this? I hurd rumours about a new ethernet library that works better?

im using your code now and it sort of works, but i get alot of connection failed before it connects as it should every time... im sitting on a 100/100 internetline so it should not be a problem to connect to a server and fetch one simple page, but still it seems like it taking forever.

That depends on the server and the connection to it. Mine is blazing fast, but it is localnet. Even Google home page is fast if I boost the serial port speed. Don't know what to tell you.

edit: If you have a 100M internet connection, you have a lot more bandwidth than I do. And compared to 15 seconds, 63 seconds is forever. What does forever mean? I haven't been alive that long.

But my friends don't agree. Moses and I were sitting around the burning bush, talking about the parting of the Red Sea, and God showed up with the same "I think my laptop has a virus" complaint...

im sitting on a 100/100 internetline so it should not be a problem to connect to a server and fetch one simple page, but still it seems like it taking forever.

Have you actually tried using another web server with a simple file on it? Your problem could be an arduino/Ethernet shield issue, or issues "out there" with the site that you are trying to connect to.