Static v. DHCP IP Address problem

Sorry but I'm new at this and I ran into a problem that has stumped me.

I have a new MEGA 2560 with a Ethernet Shield. Works fine with DHCP. Does not connect using a Static. I have the IP Info displaying in the serial output and it looks like I got everything in the correct place. I did a lot of cut and paste to get this together so I'm guessing I did something wrong. Here is the two outputs:

*** Initializing Ethernet ***
Setting Default IP Address
My IP address: 192.168.1.200.
My Subnet Mask: 255.255.255.0.
My Gateway: 192.168.1.1.
My DNS: 192.168.1.5.

Connecting to External Gateway 192.168.1.1 on port 80
connection failed
Closing Connection

*** Initializing Sump Tank ***

First Sump Tank Reading: 577

*** Initializing Alarm ***

Alarm Silence Set to 2

with boolean useDHCP = true;

*** Initializing Ethernet ***
Attempting to get a DHCP address
My IP address: 192.168.1.70.
My Subnet Mask: 255.255.255.0.
My Gateway: 192.168.1.1.
My DNS: 192.168.1.5.

Connecting to External Gateway 192.168.1.1 on port 80
connected
Closing Connection

*** Initializing Sump Tank ***

First Sump Tank Reading: 570

*** Initializing Alarm ***

Alarm Silence Set to 2

Here is a stripped down version that just has the network part.

  #include <SPI.h>
  #include <Ethernet.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  // assign an IP address for the controller:
  IPAddress ip(192,168,1,200);
  IPAddress gateway(192,168,1,1);	
  IPAddress subnet(255,255,255,0);
  IPAddress dnsserver(192,168,1,5);

  IPAddress externalGateway(192,168,1,1);
  boolean useDHCP = true;

  
  char google[] = "www.google.com";

  EthernetClient client;
  EthernetServer server(80);

void setup()
{

  Serial.begin(9600);
  
// Ethernet Start
  Serial.println("");
  Serial.println("*** Initializing Ethernet ***");

if (useDHCP) {
    Serial.println("Attempting to get a DHCP address");
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      Serial.println("Setting Default IP Address");
      Ethernet.begin(mac, ip, dnsserver, gateway, subnet);
      //Ethernet.begin(mac, ip);
    }
} else {
  Serial.println("Setting Default IP Address");
  Ethernet.begin(mac, ip, dnsserver, gateway, subnet);
  //Ethernet.begin(mac, ip);
}

// give the Ethernet shield a second to initialize:
  delay(1000);

  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(".");
  }
  Serial.println("");
  Serial.print("My Subnet Mask: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.subnetMask()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("My Gateway: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.gatewayIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("My DNS: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.dnsServerIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("");  
  Serial.print("Connecting to External Gateway ");
  Serial.print(externalGateway);
  Serial.println(" on port 80");
  
  // if you get a connection, report back via serial:
  if (client.connect(externalGateway, 80)) {
    Serial.println("connected");
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Closing Connection");
  client.stop();


  Serial.println("");
  Serial.println("");  
  Serial.print("Connecting to Google ");
  Serial.println("on port 80");
  // if you get a connection, report back via serial:
  if (client.connect(google, 80)) {
    Serial.println("connected");
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Closing Connection");
  client.stop();
    


}

void loop()
{


}

Thanks for any help you can provide!!!!

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Does not connect using a Static.

Connect to what? The router? Perhaps you should describe what you are trying to do insrtead of just showing broken code.

The whole business of getting started is tedious.

First you have to tell your router to set aside some addresses, and NOT use them for the DHCP work it does.

Then you need to program your Arduino to use one of the addresses not being used within the DHCP

More at....

... where... for now... you can ignore the stuff about DynDns, and other stuff about getting to a device on your LAN from "outside".

Thanks!!! Done

zoomkat:

Does not connect using a Static.

Connect to what? The router? Perhaps you should describe what you are trying to do insrtead of just showing broken code.

Connect to anything. As a test it is just connecting to the webpage of my router. I have used a few different addresses for the test all with the same result.

What I'm trying to do big picture. I hope to have it:
Monitor my sump pump and tank.
Check for water under my AC and Water Heater.
Verify internet is working through both my router and the Comcast router and DNS off my Windows Server is working. Long Story. Turn off and back on anything that is not working.
Monitor my garage door from web page / iphone. Close if left open.

I'm a LONG way from any of this.

What I'm trying to do at this point and the point of the post is to set the IP address without the use of DHCP, not even reserving an IP address in the DHCP server. Sometime the Windows Server is the problem so I don't want my project to use it.

Thanks!!

IPAddress dnsserver(192,168,1,5);

That doesn't look valid for a DNS server.

That is my Windows 2008 Domain Controller and is a DNS server. Provides DNS for everything in the house. When it pulls DHCP, it is getting the address from that address and it is the DNS provided by the DHCP server.

The connection test is straight to the IP so DNS should not be required. If I try the connection to www.google.com I get the same results. Works with DHCP and 192.168.1.5 for the DNS. Does not work with static.

With more testing I noticed I can ping the sheild on the IP I assign to it. I even changed the IP to make sure. I have attempted to have the Arduino connect to several different devices on my network on a few different ports including port 53 on my Domain Controller (all of which I can get to from my laptop) but I get connection failed on all of them if statically assigning the address, works fine with DHCP.

I also stopped using the ipaddress class and entered all settings as arrays. Did not change anything. I'm thinking there has to be something with the Library. Is anyone else setting a static IP and connecting to something?

Thanks

Yes, I use both static and dhcp on my network, and my Mega/ethernet shield works fine with either one.

The Windows dns server computer is at 192.168.1.5. What is the gateway device at 192.168.1.1? Which is the dhcp server?

Can you assign a computer on that localnet a static ip and connect to the internet?

edit: It works with a condition. My dhcp server does not issue the entire ip subnet. I reserve ip addresses for static ips. If I assign a static ip that is within the dhcp range, it causes problems.

Some simple web page test code that has an assigned IP address for the arduino.

// zoomkat meta refresh server test code
// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

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

int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"2\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test IDE 1.0");
          client.println("
");
                    
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("
");
          client.println("
");
          
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
          
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

SurferTim:
Yes, I use both static and dhcp on my network, and my Mega/ethernet shield works fine with either one.

The Windows dns server computer is at 192.168.1.5. What is the gateway device at 192.168.1.1? Which is the dhcp server?

Can you assign a computer on that localnet a static ip and connect to the internet?

edit: It works with a condition. My dhcp server does not issue the entire ip subnet. I reserve ip addresses for static ips. If I assign a static ip that is within the dhcp range, it causes problems.

Thank you for the reply.

Gateway is 192.168.1.1 Linksys Router.
DHCP is done by my Windows Server. 192.168.1.5. DHCP seems to work fine.
Yes. I just assigned a Static IP to both my AppleTV and a Windows XP box on that subnet using the same setting and it works fine.

zoomkat:
Some simple web page test code that has an assigned IP address for the arduino.

// zoomkat meta refresh server test code

// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

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

int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("");
          client.print("");
          client.print("<meta http-equiv="refresh" content="2">");
          client.print("Zoomkat's meta-refresh test");
          client.print("");
          client.println("");
          client.print("Zoomkat's meta-refresh test IDE 1.0");
          client.println("
");
                   
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("
");
          client.println("
");
         
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
         
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
            }
           break;
          client.println("");
          client.println("");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Thanks!!! Your code works as a server. I added the needed code for doing a client connection out but this time I put it in the loop instead of the setup. It failed the first two times then started working. Maybe my problem is just timing. I will play some more.

Connecting to External Gateway 192.168.1.1 on port 80
connection failed
Closing Connection

Connecting to External Gateway 192.168.1.1 on port 80
connection failed
Closing Connection

Connecting to External Gateway 192.168.1.1 on port 80
connected
Closing Connection

Connecting to External Gateway 192.168.1.1 on port 80
connected
Closing Connection

Connecting to External Gateway 192.168.1.1 on port 80
connected
Closing Connection

Test code that functions both as a server and a client.

//zoomkat 12-08-11, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 5 high/low
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
IPAddress ip(192,168,1,102); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
//IPAddress myserver(208,104,2,86); // zoomkat web page
EthernetServer server(84); //server port
EthernetClient client;
String readString; 
char serverName[] = "web.comporium.net"; // zoomkat's test web page server

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin 5 selected to control
  Ethernet.begin(mac,ip,gateway,gateway,subnet); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("server/client 1.0 test 12/08/11"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client

  /*if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }*/

}

void loop(){
  // check for serial input
  if (Serial.available() > 0) 
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call sendGET function
    }
  }  

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat");
            client.println();
            client.println();  
          }
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<TITLE>Arduino GET test page</TITLE>");
            client.println("</HEAD>");
            client.println("<BODY>");

            client.println("<H1>Zoomkat's simple Arduino 1.0 button</H1>");

            client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>"); 
            client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

            //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
            client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
            client.println("</IFRAME>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
} 

//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}