Question about DHCP failover

I'm tweaking my network tester, as I noticed that if I don't have Ethernet plugged in, it never starts the program properly. I've attached an old copy of the file. Here's the section in question:

  if (ethernetActive == 0)
  {
    // ethernet isnt active so lets get this show on the road
    lcdPrint(0, 0, "Requesting IP", true);

    if (Ethernet.begin(mac) != 0)
    {
      // ethernet is active now
      ethernetActive = 1;

      // waiting 1 second to let ethernet completely initialize
      // ive seen this in other code so i put it here to be safe
      delay(1000);

      myLocalIp = Ethernet.localIP();
      mySubnetMask = Ethernet.subnetMask();

      // set connection timeout and retry count
      // 0x07D0 == 2000
      // 0x320  == 800
      // 0x1F4  == 500
      W5100.setRetransmissionTime(0x1F4);
      W5100.setRetransmissionCount(1);
    }
    else
    {
      // ethernet failed to initialize, we will keep trying
      lcdPrint(0, 1, "No DHCP");
    }
  }
  else
  {
    // if everything seems good to go, display the main menu
    mainMenu(); 
  }

The thing is, it never gets to the else statement of "No DHCP". What I'd like to have it do is drop into the else statement where I can hard-code an APIPA address of 169.254.x.y to at least get it to run the menu and I could set an IP address manually.

What do I need to change to do this? :slight_smile:

PANTv1dot10.ino (35.4 KB)

You would have to handle the DHCP yourself. The Ethernet library contains the DhcpClass class, which is what does the DHCP work. The Ethernet.begin() calls that class, but just calls the generic version of it's beginWithDHCP() method. There is also a version which allows you to pass timeout values, so if you replicate what the Ethernet's .begin() is doing with regards to DHCP but use the timeout version instead, you should be able to attempt to get an IP address to then pass to the Ethernet library to use, or time out and pass your own IP address instead.

The thing is, it never gets to the else statement of "No DHCP".

Are you sure? It takes at least a minute to determine there is no dhcp server on the localnet. If you don't get that message, then there is no sense putting a static assignment if it will never run. You must determine why the "Ethernet.begin(mac);" (dhcp request call) is not timing out. It does for me.

SurferTim:

The thing is, it never gets to the else statement of "No DHCP".

Are you sure? It takes at least a minute to determine there is no dhcp server on the localnet. If you don't get that message, then there is no sense putting a static assignment if it will never run. You must determine why the "Ethernet.begin(mac);" (dhcp request call) is not timing out. It does for me.

I don't know why it wouldn't time out after a minute. I've left this thing on for 15 minutes and it still just sits there. I've tried it with both Ethernet plugged in and unplugged.

OK, the best place to start is at the beginning. Try this sketch. Don't change anything.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);

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() {
}

Does the serial monitor show 192.168.2.2?

The output is:

Starting w5100
192.168.2.2

OK. That means the SPI and the SPI side of the w5100 is working ok. Next test is the dhcp.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

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

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

  Serial.println("Starting w5100");
  if(!Ethernet.begin(mac)) Serial.println("DHCP fail");
  else Serial.println(Ethernet.localIP());
}

void loop() {
}

Does it show "DHCP fail" or an ip address?

edit: In my haste, I forgot the problem. Or does not show anything after "Starting w5100". It may take a minute or more to timeout and show "DHCP fail".

Sorry about the delay in response. This code works as I expected it to:

Starting w5100
DHCP fail

Now that I know what works, I'm going to try and wedge the working code into my setup. :slight_smile: