WDT keeps resetting

I’ve just started messing around with the WDT - I’ve incorporated into into one of my projects that sends data to pachube using a ethernet module. I’m using the Ethercard libraries.

Before I inserted the WDT code it all worked great - now the code keeps resetting every few seconds. If I comment out these lines then it works:

  if (!ether.dnsLookup(website))
   Serial.println(F("DNS failed"));

Here’s my full code:

#include <avr/wdt.h>
#include <EtherCard.h>
#include <Wire.h>



char website[] PROGMEM = "api.pachube.com";

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 
  0x70,0x60,0x60,0x20,0x30,0x30 };

static uint8_t myip[4] = { 
  10,1,1,20 };          //IP
static uint8_t gwip[4] = { 
  10,1,1,1 };           //Gateway IP
static uint8_t dnsip[4] = { 
  10,1,1,1 };          //DNS IP
byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;


void setup () {

  wdt_enable(WDTO_2S);
  
  Serial.begin(9600);
  Serial.println(F("Setup Network"));


  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println(F("Failed to access Ethernet controller"));

  ether.staticSetup(myip, gwip, dnsip);

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
   Serial.println(F("DNS failed"));


}

void loop () {

  wdt_reset();

}

Any idea where I’m going wrong?

You're telling the WDT to reset the processor if you don't call wdt_reset() at least every 2 seconds, then you call dnsLookup() which may take more than two seconds. Of course your system will reset!

Move the wdt_enable() to AFTER the dnsLookup() or set your wdt interval long enough that dnsLookup() will return in time.

Looks like the DNS lookup (plus the other things you are doing in setup()) are taking more than 2 seconds. This is entirely possible when the Ethernet connection is down.

Doh - as easy as that! I increased it to 8s - problem sorted!

Thanks.