Auto restart arduino after 7 days

As the subject like ask. Is there a proffered way to auto reset the arduino after a week?

Problem: I have 2 separate Arduino+Ethernet shields that live in my house, running 24/7 for various purposes. (Purpose not important). I access the Arduino via webpages I have setup on them to do various functions. They can be working fine for weeks, sometimes months with no issues. Then one day I will not be able to load the webpage they are hosting. I still get a response from pings most of the time in this state. Other functions of the Arduino are still working.
I then need to go access the device and push the reset button, and everything is happy again.

Resolution I wish to accomplish: Set a line of code in my arduino so that after every 7 days, the code reboots the arduino to.

Question: Is there any suggested code, or options out there using just code to auto reset after a certain amount of time?

Notes.
-Yes I know about watch dog and it's purpose. And that is already loaded in the code. Watchdog as a limited time out of 8s. And that only works IF the source code glitches.
-No I don't have and won't be adding a RTC to the unit
-Yes I do have a sdcard being used on the Ethernet shield.

IF your software is still running and behaving, you can do this

void(* resetFunc) (void) = 0;//declare reset function at address 0
//...
//inside loop
  if( millis() - startTime >= sevenDayNumber ) resetFunc(); //call reset

Turn the watchdog on every 168 hours, but don’t feed it.

blh64:
IF your software is still running and behaving, you can do this

void(* resetFunc) (void) = 0;//declare reset function at address 0

//...
//inside loop
  if( millis() - startTime >= sevenDayNumber ) resetFunc(); //call reset

Perfect. I even trimmed it down some more in my case as every byte counts. This code uses 484 bytes. (with Serial commented out)

Here is my example working code:

void setup() {
  //Serial.begin(9600);
  //Serial.println("Reset");
}

void(* resetFunc) (void) = 0; //declare reset function at address 0 - MUST BE ABOVE LOOP

void loop() {

  //Serial.println(millis());
  //if ( millis()  >= 1000) resetFunc(); //call reset every 1 second.
  //if ( millis()  >= 60000 ) resetFunc(); //call reset every 60 seconds (1 Minute).
  //if ( millis()  >= 3600000) resetFunc(); //call reset every 60 mins (1 Hour)
  //if ( millis()  >= 86400000) resetFunc(); //call reset every 24 hours (1 Day). .
  if ( millis()  >= 604800000 ) resetFunc(); //call reset every 7 days (1 Week).
  //if ( millis()  >= 2592000000) resetFunc(); //call reset every 30 days (1 Month).
  //if ( millis()  >= 2678400000) resetFunc(); //call reset every 31 days (1 Month).
  //if ( millis()  >= 31536000000 ) resetFunc(); //call reset every 365 days (1 Year).

}

Yes, I do understand that the clock time on the arduino is not perfect, and the milliseconds counted may or may not stay in sync with an actual clock. But this will work for my purpose and needs. Thank you blh64

better

  if ( millis()  >= 604800000UL ) resetFunc(); //call reset every 7 days (1 Week).

and also realize that millis() rolls over every 49.xxx days (max size of unsigned long) so you will never be able to do 1 year.

1 Like

blh64:
better

  if ( millis()  >= 604800000UL ) resetFunc(); //call reset every 7 days (1 Week).

and also realize that millis() rolls over every 49.xxx days (max size of unsigned long) so you will never be able to do 1 year.

But millis will reset every week.
Because…reset.

TheMemberFormerlyKnownAsAWOL:
But millis will reset every week.
Because...reset.

True, but the OPs code had this comments

  //if ( millis()  >= 2678400000) resetFunc(); //call reset every 31 days (1 Month).
  //if ( millis()  >= 31536000000 ) resetFunc(); //call reset every 365 days (1 Year).

And I was pointing out that last statement can never happen.

Are you using the String class? It may be crashing your code. I had an ethernet based web server that ran for well over 6 months unattended, using C strings instead. It actually never crashed, I just had to power it down once to move some power plugs.

aarg:
Are you using the String class? It may be crashing your code. I had an ethernet based web server that ran for well over 6 months unattended, using C strings instead. It actually never crashed, I just had to power it down once to move some power plugs.

Could you elaborate further on the difference between the 'string class' and the 'c string' you mentioned? And perhapse give an example so I can understand what you are talking about please?

Forcing a jump to address 0 only works if your sketch has not locked up before then. A better way is to turn on the watchdog timer which will reset your Arduino if your loop() doesn’t reset the timer at least every 8 seconds. If your code locks up, the watchdog timer will reset your Arduino. After 7 days you can stop resetting the timer to force the watchdog timer to reset the Arduino.

#include <avr/wdt.h>


void setup()
{
  wdt_enable(WDTO_8S);
}


void loop()
{
  const unsigned long ONE_WEEK = 7ul * 24ul * 60ul * 60ul * 1000ul;
  if (millis() < ONE_WEEK)
    wdt_reset();
}

Note: If you have any operations that may take more than 8 seconds, like a DNS lookup for example, you can disable the watchdog timer before the call and re-enable it after.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.