[Solved]TP Link, Mega 2560, Ethernet Shield, network configuration problem

Goal: To use an ethernet shield with a Nano Router as a Wi-Fi alternative.
link: Arduino Forum
Problem: When I enter IP address 192.168.1.65 into a browser I should get a web page, however, I get the message "cannot be found".

My first attempt at using Macbook wirelessly (Airport ) to monitor an analog signal from a Mega2560 board with an Arduino Ethernet Shield via a TP Link (TL_WR702N) nano router through my home router.
I have configured the TP-Link wireless nano router in Client mode.

I have configured the Macbook to set Configure IPv4 as "using DHCP".
IPv4 Address: 192.168.1.65
Subnet mask: 255.255.255.0
Router: 192.168.1.254

An ethernet cable from the shield is connected to the TP Link TL-WR702N.
The arduino is powered by an external 5 Volt supply.
The TP Link has been configured as a Client.
I have loaded Tom Igoe's sketch (Pde 1.03) from Making Things Talk 2nd edition page 124.
Here it is.

/*
  RGB Web  Server
 Context: Arduino
 
 */

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

EthernetServer server(80);

byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress gateway(192,168,1,254);// same as router address
IPAddress subnet(255,255,255,0);
IPAddress ip(192,168,1,65);


// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):


void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.begin(9600);
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Got a client");
    int lineLength = 0;    // length of the incoming text line

    while (client.connected()) {
      if (client.available()) {
        // read in a byte and send it serially:
        char thisChar = client.read();
        Serial.write(thisChar);
        // if you get a linefeed and the request line is blank,
        // then the request is over:
        if (thisChar == '\n' && lineLength < 1) {
          // send a standard http response header
          makeResponse(client);
          break;
        }
        //if you get a newline or carriage return,
        // you're at the end of a line:
        if (thisChar == '\n' || thisChar == '\r') {
          lineLength = 0;
        } 
        else {
          // for any other character, increment the line length:
          lineLength++;
        }
      }    
    }
    Serial.println("Breaking");
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}


void makeResponse(EthernetClient thisClient) {
  thisClient.print("HTTP/1.1 200 OK\n");
  thisClient.print("Content-Type: text/html\n\n");
  thisClient.print("<html><head><meta http-equiv=\"refresh\" content=\"3\">");
  thisClient.print("<title>Hello from Arduino</title></head>");
  // set up the body background color tag:
  thisClient.print("<body bgcolor=#");
  // read and the three analog sensors:
  int red = analogRead(A0)/4;
  int green = analogRead(A1)/4;
  int blue = analogRead(A2)/4;
  // print them as one hexadecimal string:
  thisClient.print(red, HEX);
  thisClient.print(green, HEX);
  thisClient.print(blue, HEX);
  // close the tag:
  thisClient.print(">");
  // now print the color in the body of the HTML page:
  thisClient.print("The color of the light on the Arduino is #");
  thisClient.print(red, HEX);
  thisClient.print(green, HEX);
  thisClient.println(blue, HEX);
  // close the page:
  thisClient.println("</body></html>\n");
}

I have changed the IP gateway to the following:

byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress gateway(192,168,1,254);// same as home router address
IPAddress subnet(255,255,255,0);
IPAddress ip(192,168,1,65);

Observations: With a sketch loaded and the shield connected the green led and yellow led periodically turns on and off. The blue LED on the nano router stays permanently on.

Airport connected: Status connected: Airport is connected to BT Hub and has the IP address 192.168.1.65 ( same address as in sketch)

Any pointers gratefully received.

That link to the TP Link setup shows the ethernet LAN addresses as 192.168.0.x and the gateway is 192.168.0.254. Maybe you should check the setup in the router to make sure you have all that correct.

edit: In that case, this would be incorrect and would cause a connection fail.

  Ethernet.begin(mac, ip, gateway, subnet);
  // it should be
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

Thanks for your prompt reply TIM.

I rechecked the settings for my home router and found under

Configure IPv4: Using DHCP

IPv4 Address:192.168.1.65
Subnet Mask: 255.255.255.0
Router:192.168.1.254

This agrees with the code:

byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress gateway(192,168,1,254);// same as router address
IPAddress subnet(255,255,255,0);
IPAddress ip(192,168,1,65);

I have changed the code as you suggested to

 Ethernet.begin(mac, ip, gateway, subnet);
  // it should be
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

When I use http://192.168.1.65/ as the url the following appears in the browser:
"It Works!" I do not understand this as the html code for "It Works!" does not appear in the script.

"It Works!" is normally the response from a Windows web server before you change the default page. That is not the Arduino.

Where did you get this IP address? This is not the router address. Are you sure that is not another computer?
IPv4 Address:192.168.1.65
Subnet Mask: 255.255.255.0
Router:192.168.1.254

edit: That TP Link router uses 192.168.0.x/24 on the ethernet. Read here about setting it up:

Open a Web Browser > Type 192.168.0.254 in the address bar > Press Enter

Where did you get this IP address? This is not the router address.

I will have to start at the beginning since the DHCP AddressPrinter sketch gives :

Serial.println("Failed to configure Ethernet using DHCP");

Once I get that sorted I will come back.
I could be sometime.

Thanks.

Using various test sketches by ZoomKat and Tom Igoe I solved the problem. Like SurferTim suggested I checked all my settings and found I had an incorrect IP.

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

/*
I use this to test the SPI connection.
If the serial monitor shows 192.168.1.75, then it is ok. 
If it shows anything else, like 0.0.0.0, then it failed.

*/

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,75);// BT Home Hub 192.168.1.76

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

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip);

  Serial.println(Ethernet.localIP());
}

void loop() {
}
/*
  DHCP-based IP printer
 
 This sketch uses the DHCP extensions to the Ethernet library
 to get an IP address via DHCP and print the address obtained.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 12 April 2011
 modified 9 Apr 2012
 by Tom Igoe
 
 */

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };// HOBBY components 

// 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);
  Serial.println("Starting");
  // this check is only needed on the Leonardo:
   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");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); // 192.168.1.76
  }
  Serial.println();
}

void loop() {

}

Replaced the original code with one which allows me to turn LED's on and off from a web page.
Since the code was taken from a book I think I need permission to publish it. Have emailed the author. I now need to implement the sketch by ZoomKat to prevent dropping of connection.

Below is the code for controlling an LED remotely via a web page.
It is taken from the book Programming Arduino - Getting started by Simon Monk.
Just add a series resistor to an LED connected between ground and any pin listed.

// sketch 10-02 Internet Pins
// Source: Programming Arduino - Getting Started by Simon Monk
#include <SPI.h>
#include <Ethernet.h>

// MAC address just has to be unique. This should work
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address will be dependent on your local network:
byte ip[] = { 
  192, 168, 1, 30 };
EthernetServer server(80);

int numPins = 5;
int pins[] = {
  3, 4, 5, 6, 7};
int pinState[] = {
  0, 0, 0, 0, 0};
char line1[100];

void setup()
{
  for (int i = 0; i < numPins; i++)
  {
    pinMode(pins[i], OUTPUT);
  }
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      readHeader(client);
      if (! pageNameIs("/"))
      {
        client.stop();  
        return;
      }
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();

      // send the body
      client.println("<html><body>");
      client.println("<h1>Output Pins</h1>");
      client.println("<form method='GET'>");  
      setValuesFromParams();
      setPinStates();
      for (int i = 0; i < numPins; i++)
      {
        writeHTMLforPin(client, i);
      }
      client.println("<input type='submit' value='Update'/>");
      client.println("</form>");
      client.println("</body></html>");

      client.stop();            
    }
  }
}

void writeHTMLforPin(EthernetClient client, int i)
{
  client.print("<p>Pin ");
  client.print(pins[i]);  
  client.print("<select name='");
  client.print(i);
  client.println("'>");
  client.print("<option value='0'");
  if (pinState[i] == 0)
  {
    client.print(" selected");
  }
  client.println(">Off</option>");
  client.print("<option value='1'");
  if (pinState[i] == 1)
  {
    client.print(" selected");
  }
  client.println(">On</option>");
  client.println("</select></p>");  
}

void setPinStates()
{
  for (int i = 0; i < numPins; i++)
  {
    digitalWrite(pins[i], pinState[i]);
  }
}

void setValuesFromParams()
{
  for (int i = 0; i < numPins; i++)
  {
    pinState[i] = valueOfParam(i + '0');
  }
}

void readHeader(EthernetClient client)
{
  // read first line of header
  char ch;
  int i = 0;
  while (ch != '\n')
  {
    if (client.available())
    {
      ch = client.read();
      line1[i] = ch;
      i ++;
    }
  }
  line1[i] = '\0'; 
  Serial.println(line1);
}

boolean pageNameIs(char* name)
{
  // page name starts at char pos 4
  // ends with space
  int i = 4;
  char ch = line1[i];
  while (ch != ' ' && ch != '\n' && ch != '?')
  {
    if (name[i-4] != line1[i])
    {
      return false;
    }
    i++;
    ch = line1[i];
  }
  return true;
}

int valueOfParam(char param)
{
  for (int i = 0; i < strlen(line1); i++)
  {
    if (line1[i] == param && line1[i+1] == '=')
    {
      return (line1[i+2] - '0');
    }
  }
  return 0;
}