Issue with Watch Dog Timer

I am having trouble getting the watch dog timer to work. I have read multiple posts on various forums and what I have done looks standard but nothing happens.

This is one where I want to reboot if I don’t get a tcp connection within 60 seconds

  // Start TCP Client
  // Reboot if we don't get TCP Connection within 60 seconds
  LoopExitTime = millis() + 60000;
  while ( ( !myTcpClient.connect( SrvrIP, Port ) ) && ( millis() < LoopExitTime ) )

  // Reboot
  if ( millis() > LoopExitTime ) 
    Serial.println( F( "Failed to open TCP connection" ) );

  Serial.println( F( "TCP running" ) );

In this one, I have a function which waits 70 seconds for input from a serial device (Dylos dust monitor).

void Check_mySerial_Avail() 
      ( mySerial.available() == 0 ) && 
      ( millis() < lDylosFailureTime )

  if ( millis() > lDylosFailureTime ) 
    Send2Srvr( "Ctrl", "Restart - No Dylos message for 70 seconds", true );


When I set thinhs up so that failures occur - stop tcp listener in first example, turn power off to Dylos in second - nothing happens. I get the diagnostic message on the serial monitor showing the logic clearly arrives in that branch but the sketch logic does not restart.

Am I missing something?

1 Like

A few more snippets.

When I do a verbose compile, this file:

#include <avr/wdt.h>

… does not appear in the output list along with all the other “included” files.

I thought the WDT stuff may be native so I commented out this include statement and tried to compile, I got error messages that both ‘WDTO_15MS’ and ‘wdt_enable’ were not declared in this scope. I found all this very confusing.

1 Like

Hey Brian,

I haven’t worked with the watchdog before, but it seems that you’re definitely using the best method you can. Have you got a bit of serial printing that triggers when your code starts so you can tell when it restarts?

From the code you’ve posted so far, I can’t see any errors, but could you post the rest as well?

I’ll do some experimentation on my end to try and get a watchdog to work in my instance

1 Like

Hi James.

Thanks for your interest.

Before I send you my 440-line sketch, I will share a test sketch with you.

// Include necessary library files
#include <avr/io.h>
#include <avr/wdt.h>
unsigned long LoopExitTime;

void setup () 

  // Launch diagnostic messaging
  while (!Serial);
  Serial.println( "" );
  Serial.println( F( "Arduino ***** DIAGS RUNNING *****" ));

  Serial.println( F( "Beginning One sec wait" ) );


void loop()

  LoopExitTime = millis() + 2000;
  while ( millis() < LoopExitTime ) 
  Serial.print( F( "Looping: " ));
  Serial.print( LoopExitTime);
  Serial.print( F( " : " ));
  Serial.println( millis());


The diagnostic output is:

16:40:25.278 -> 
16:40:25.278 -> Arduino ***** DIAGS RUNNING *****
16:40:25.278 -> Beginning One sec wait
16:40:25.332 -> Looping: 2000 : 11
16:40:25.479 -> Looping: 2000 : 216
16:40:25.679 -> Looping: 2000 : 415
16:40:25.880 -> Looping: 2000 : 616
16:40:26.081 -> Looping: 2000 : 817
16:40:26.281 -> Looping: 2000 : 1016
16:40:26.481 -> Looping: 2000 : 1217
16:40:26.681 -> Looping: 2000 : 1418

I would have thought the WDT (enabled at WDTO_1S ) would have kicked in before the 16:40:26.281 line.

While working out what to say here, I continued searching and I think I have found the (an?) answer. I was going to pull the pin but then I thought you may be interested in what I found.

At this address: Watchdog Timer in Arduino | Arduino
I found an example that is very close to mine above except that it has a delay after the wdt_disable statement. With that in place it works perfectly. I tried the 3000ms in the example but cut that back to 500 and it worked fine.

I now need to work out whether I need to do this just once or in multiple logic paths.

Still happy to share my full sketch with you. I plan to do so soon anyway when I have got it fully “productionised”.

1 Like