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 ) )
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).
( 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?
A few more snippets.
When I do a verbose compile, this file:
… 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.
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
Thanks for your interest.
Before I send you my 440-line sketch, I will share a test sketch with you.
// Include necessary library files
unsigned long LoopExitTime;
void setup ()
// Launch diagnostic messaging
Serial.println( "" );
Serial.println( F( "Arduino ***** DIAGS RUNNING *****" ));
Serial.println( F( "Beginning One sec wait" ) );
LoopExitTime = millis() + 2000;
while ( millis() < LoopExitTime )
Serial.print( F( "Looping: " ));
Serial.print( F( " : " ));
The diagnostic output is:
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”.