If connection is out more than 30 seconds, reset arduino using WDT

Hello everyone,

I am using Arduino Mega and Wifi shield. I am using CC3000 Adafruit example from the blynk examples and everything works perfect.

Sometimes Arduino gets offline (it simply hangs). If the connection is out - for more than 30 secs- , I need to program Arduino to be able to reset itself.

I knew that one rigid way of doing this is through Watchdog timer. I also read through Arduino forums, how to get the wdt to work for more than 8 seconds. but due to my limited programming capabilities, I don’t know where to integrate this in the blynk example.

If you please could guide me, how to program my Arduino correctly (What am I missing in the code? What to add?), I would be so grateful,

Here is my best code:

` #define BLYNK_PRINT Serial // Comment this out to disable prints and save space

// These are the interrupt and control pins for СС3000
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <BlynkSimpleCC3000.h>
#include <avr/wdt.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "blabla";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "bla", "bla", WLAN_SEC_WPA2);
}

void loop()
{
  if (Blynk.connected()){
  Blynk.run();
}
else {
  myWatchDogEnable(0b100001); //8 seconds
  myWatchDogEnable(0b100001); //8 seconds
  myWatchDogEnable(0b100001); //8 seconds
  myWatchDogEnable(0b100001); //8 seconds
}
}

void myWatchDogEnable(const byte interval){
  MCUSR=0;
  WDTCSR|=0b00011000;                               //set WDCE, WDE
  WDTCSR=0b01000000|interval;                       //set WDIE & delay
  wdt_reset();
  Blynk.run();
}
ISR(WDT_vect){
  wdt_disable();
}`
1 Like

@Costas would you please help?

@hkhalaf does the WDT work on your Mega without Blynk i.e. have you flashed it with Optiboot?

I have flashed Nano’s with the correct bootloader but not the Mega. I believe the Hex file for the correct bootloader for the Mega is available at https://raw.githubusercontent.com/arduino/Arduino-stk500v2-bootloader/master/goodHexFiles/stk500boot_v2_mega2560.hex

Thanks for the reply @Costas

I don’t really know if the problem is with the optiboot (Mega needs to be flashed with optiboot to be able to run WDT) or My naïve programming.

Could you please tell if having the Blynk.run available in both the loop section and under the myWatchDogEnable() correct or should I be modifying the code?

@hkhalaf I think the additional Blynk.run() is ok.
You can test your Mega with the following 32 second lock up simulation sketch.
If it doesn’t reset take a look at Nick Gammon’s stuff at https://github.com/nickgammon/arduino_sketches and http://www.gammon.com.au/bootloader to ‘fix’ the Mega.

#include <SimpleTimer.h>
#include <avr/wdt.h>

SimpleTimer timer;
unsigned int WDTcounter;

void CheckWDT(){
  WDTcounter++;
  Serial.print("WDT Counter = ");
  Serial.println(WDTcounter);
  if(WDTcounter > 7){
    Serial.println("Resetting device with WDT, please wait.......");
    while (true) ;
  }
  else{  // not locked up for more than 32 seconds so reset WDT
    wdt_reset();    
  }
}

void setup()
{
  Serial.begin(115200);
  delay(20);
  Serial.println("Arduino has restarted");
  wdt_enable(WDTO_8S);
  timer.setInterval(4000, CheckWDT); 
}

void loop()
{
  timer.run();
}