Go Down

Topic: WebClient not working (Read 653 times) previous topic - next topic

MrGlasspoole

I'm trying to get the WebClient example running but always get "connection failed".

The google ip is ok and 173.194.35.8/search?q=arduino works in Firefox.
I also have Arduino running as server without problems so my network settings are ok.
It's the official Ethernet Shield.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

/**************************************************************
*                      Ethernet Settings                      *
**************************************************************/
// Ethernet Card Mac Address
byte mac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
// IPv4 address
byte ip[] = {192, 168, 0, 60};
// Subnet mask
byte subnet[] = {255, 255, 255, 0};
// Default gateway
byte gateway[] = {192, 168, 0, 1};
// Preferred DNS sever
// byte dns[] = {192, 168, 0, 1};

IPAddress server(173,194,35,8); // Google

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

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

  Ethernet.begin(mac, ip, subnet, gateway);
  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 /search?q=arduino HTTP/1.0");
    client.println();
  }
  else {
    // kf 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:
  if (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(;;)
      ;
  }
}

SurferTim

#1
Dec 15, 2012, 02:02 pm Last Edit: Dec 15, 2012, 02:04 pm by SurferTim Reason: 1
Try this code example from the wiki (playground). It has been thoroughly tested.
http://playground.arduino.cc/Code/WebClient
If you have problems with it, post here and I will try to help.

edit: Hey!! How about that? I'm using the wiki instead of posting all that code again!!  :D

MrGlasspoole

Ok, this one works - thanks. But looks like it's not a good idea to make a
google search. To much html, javascript and css for the Mega...

But hey you are the FTP guy.
I will write in that FTP thread in a minute :)

SurferTim

Not really too much html. The part that slows the whole process up is the Serial output at 9600 baud. Kick that up to a reasonable rate, or use it for debugging only, and it will really smoke!  :)

PaulS

Hey, Tim. I tried that code, too. It eventually works, but, I'm wondering why you have a wait, do something, wait, do something, methodology, instead of a do something, wait, do something, wait, methodology.

Changing the initial value of loopCount to 30 makes it do something immediately, and then wait 30 seconds.

SurferTim

Hi Paul! My routers have a time delay between the time the ip/mac address is available on the network until the router puts that ip/mac in the ARP table. If you can guarantee your router will get the ARP table update in less than 2 seconds, go for it!

PaulS

It's not the delay() in setup() that is the issue. It is that once loop() is called, nothing happens for 30 seconds. By the time loop() is called, the Ethernet shield/class should be ready to do stuff. It should, then, in my opinion, get busy doing something, and then wait so as not to overwhelm the server.

SurferTim

That is correct. My router SOMETIMES takes longer than that 2 seconds to put the ip/mac in the ARP table. So I use the additional 30 seconds to allow the router time to get all in order.

I am testing on a really busy network. I have 300 users in the network database, and a lot of them use Netflix.

Like I said, if you can guarantee...

PaulS

Quote
Like I said, if you can guarantee...

I was asking why. Your first response seemed to address the delay in setup(). Now, it appears that you do have a good reason for the wait/do/wait/do methodology, rather than the do/wait/do/wait methodology. I'm fine with that.

SurferTim

I'm glad you brought it up. If you are on a private network with 2 computers and the Arduino, the 30 second delay is probably not necessary. You could very well switch the 30 second delay after the first request.

Go Up