Arduino 328p + Ethernet shield W5100 can't access external network

Hello,
I have the Arduino 328p + Ethernet shield W5100 connected to the router. The router is connected to internet. My PC and mobile can access the internet through the router.

  1. If I run the WebServer sketch on Arduino, I can access it in the IE on my PC. This is as expected.
  2. If I run the WebClient code on Arduino, and access the web server in the same LAN, it can connect to the web server, and output the http response to the Serial Monitor. This is as expected.
    3.If I run the WebClient code on Arduino, and access the internet web server, it can NOT connect to the web server. This is my problem. The sketch code and output of the Serial Monitor is shown below. I can ping the Arduino on my PC. In the router management page, I can see the Arduino Mac and IP in the ARP page. Does anybody encounter such kind of tricky issue?

I tried the approaches below, but none of them works.
1). Change a different IP, such as 192.168.1.233. And I’m sure the used IP isn’t conflicted to other device.
2). Change a different MAC.
3). Change a different server IP.
4). Reset Arduino serval times when it is connecting to server or the connection fails.
5). Delay a longer time after Ethernet.begin.
6). Configure the router to put the Arduino IP in DMZ.

There is another thread (http://arduino.cc/forum/index.php/topic,52664.0.html) to discuss such kind of issue. But unfortunately no solution.

WebClient sketch

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0x00, 0x0D, 0x0E, 0x0F, 0xF0, 0xE0 };
byte ip[] = { 192,168,1,177 };
//byte server[] = { 119,75,217,56 }; // www.baidu.com
//byte server[] = { 192,168,1,200 }; 
byte server[] = {  64,233,183,147 }; //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):
Client client(server, 80);

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  Serial.println("connecting...");
  delay(2000);
  
  // if you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET / HTTP/1.1");
    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(;;)
      ;
  }
}

Output of the Serial Monitor

connecting...
connection failed

disconnecting.

There are variations on the Ethernet.begin() function call. One of them allows you to specify a subnet mask. I think that this is necessary to get outside of a local area network (aka home network).

Try the below code without any modification and see if it works.

//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 }; // Arduino IP address
byte server[] = { 208, 104, 2, 86 }; // zoomkat's web site

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

I had a quick test with zommkat's code. The output is listed below. It still can't access the web server.

starting simple arduino client test

connecting...
connection failed

disconnecting.
==================================

Hi PaulS, If look into the source code of Ethernet library, the default gateway and subnet for my sketch are 192.168.1.1 and 255.255.255.0 respectively. They are exactly correct for my local network. I still tried your suggestion. Passing them into Ethernet.begin(...) doesn't resolve my issue.

I'm using the TP-LINK router. Its software version is: 3.10.3 Build 091231 Rel.51228n and hardware version is: WR741N/742N v1 00000000. Is there any functionality needs to be configured? Any comments is appreciated.

I don’t know why zoomkat’s code doesn’t work for you, but for the google example you posted, you’re attempting to connect with the HTTP 1.1 protocol, and if you do that I believe you need to also include a HOST line. Try changing this line in your code…

client.println("GET / HTTP/1.1");

…to this…

client.println("GET / HTTP/1.0");

…and see if that helps.