Pages: [1]   Go Down
Author Topic: Question about DHCP failover  (Read 447 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
  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? smiley

* PANTv1dot10.ino (35.39 KB - downloaded 3 times.)
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.


Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, the best place to start is at the beginning. Try this sketch. Don't change anything.
Code:
#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?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The output is:
Code:
Starting w5100
192.168.2.2
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. That means the SPI and the SPI side of the w5100 is working ok. Next test is the dhcp.
Code:
#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".
« Last Edit: July 10, 2013, 01:58:10 pm by SurferTim » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
Starting w5100
DHCP fail

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

Pages: [1]   Go Up
Jump to: