Watchdog running on Photon, help needed

I am having some trouble on a product that needs to run loop whether connected to wifi/cloud or not.
Got that part working fine by recreating some scenarios with flaky internet connections or no internet at all.
Sometimes it would reconnect just fine and loop was running whether connected to cloud or not.
In case of anything going wrong out in the field I needed to use watchdog to reset the photon if it got stuck, example solid green light etc.
I must be doing something wrong in my code after using watchdog and just trying to understand how and when the watchdog resets the photon.
If I power on, and there is no internet connection, the watchdog resets the photon. Cant have that. Need the watchdog to reset the photon only if loop is stuck and not running.
It’s not the entire code, just to make things simple.
Your assistance would be greatly appreciated.
Here is the code.

#include <SparkCorePolledTimer.h>
#include <photon-wdgs.h>
#include <SparkIntervalTimer.h>
#include <blynk.h>

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

SparkCorePolledTimer updateTimer(500);

const uint32_t msRetryDelay = 60000; // retry every 1min
const uint32_t msRetryTime  = 30000; // stop trying after 30sec

bool   retryRunning = false;
Timer retryTimer(msRetryDelay, retryConnect);  // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect);     // timer to stop a long running try
//===========================================================================

#define RESET V10              // System Reset
#define Vtimer V18             // Timer Widget
#define QuietTimer V30         // Quiet Time Timer Widget

#define killRelay D3            // Kill switch relay
#define startRelay D7           // Starter relay
#define primeRelay D1           // Primer relay

#define BLYNK_PRINT Serial

char auth[] = "xxxxxxxxx";     //V2

unsigned long lastmsg=millis();


STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

void setup()
{
  Serial.begin(115200);  
  updateTimer.SetCallback(OnTimer);  
  Blynk.config(auth);  
  PhotonWdgs::begin(true,true,10000,TIMER7);
  
  pinMode(killRelay, OUTPUT);
  pinMode(startRelay, OUTPUT);
  pinMode(primeRelay, OUTPUT);
  pinMode(Vtimer, OUTPUT);
  pinMode(QuietTimer, OUTPUT);
  
  digitalWrite(killRelay, LOW);                                       
  digitalWrite(startRelay, LOW);
  digitalWrite(primeRelay, LOW);
  digitalWrite(Vtimer, LOW);
  digitalWrite(QuietTimer, LOW);
  
  Particle.connect();
  
  Blynk.connect();  
  if (!waitFor(Particle.connected, msRetryTime))
  WiFi.off();                // no luck, no need for WiFi
  Particle.publish("watchdog_test", "v2 Online");
}



BLYNK_WRITE(RESET)
{
  System.reset();
}
 

void loop()
{
  static uint32_t ms500 = 0;
  
  updateTimer.Update();
  if (millis() - ms500 < 500) return;
  ms500 = millis(); 
    
  Blynk.run();
  
    PhotonWdgs::tickle();
    if(lastmsg + 3000UL < millis()){
        Particle.publish("watchdog_test", "v2 ALIVE!");
        lastmsg = millis();
    }
  // Do more things in loop that need to run, whether connected to wifi/cloud OR not.
  
}

//============================================================================================================================================
void OnTimer(void) 
{ 
    //Handler for the timer, will be called automatically
}
void retryConnect()
{
  if (!Particle.connected())   // if not connected to cloud
  {
    Serial.println("reconnect");
    stopTimer.start();         // set of the timout time
    WiFi.on();
    Particle.connect();        // start a reconnectin attempt
  }
  else                         // if already connected
  {
    Serial.println("connected");
    Blynk.connect();
    Blynk.run();
    retryTimer.stop();         // no further attempts required
    retryRunning = false;
  }
}
void stopConnect()
{
    Serial.println("stopped");

    if (!Particle.connected()) // if after retryTime no connection
    WiFi.off();              // stop trying and swith off WiFi
    stopTimer.stop();
}

Since you are using SYSTEM_THREAD(ENABLED) you can go with SYSTEM_MODE(MANUAL) which should make Particle.connect() non-blocking.

You also can’t use waitFor() since during that time, you won’t be able to tickle the WDG, rather use a loop in which you keep tickling it.

BTW, what’s the reason behind SparkCorePolledTimer in that project?

I made these changes as you suggested but watchdog keeps resetting while trying to find wifi.

#include <SparkCorePolledTimer.h>
#include <photon-wdgs.h>
#include <SparkIntervalTimer.h>
#include <blynk.h>

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

SparkCorePolledTimer updateTimer(500);

const uint32_t msRetryDelay = 60000; // retry every 1min
const uint32_t msRetryTime  = 30000; // stop trying after 30sec

bool   retryRunning = false;
Timer retryTimer(msRetryDelay, retryConnect);  // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect);     // timer to stop a long running try
//===========================================================================

#define RESET V10              // System Reset
#define Vtimer V18             // Timer Widget
#define QuietTimer V30         // Quiet Time Timer Widget

#define killRelay D3            // Kill switch relay
#define startRelay D7           // Starter relay
#define primeRelay D1           // Primer relay

#define BLYNK_PRINT Serial

char auth[] = "xxxxxxxxx";     //V2

unsigned long lastmsg=millis();


STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

void setup()
{
  Serial.begin(115200);  
  updateTimer.SetCallback(OnTimer);  
  Blynk.config(auth);  
  PhotonWdgs::begin(true,true,10000,TIMER7);
  
  pinMode(killRelay, OUTPUT);
  pinMode(startRelay, OUTPUT);
  pinMode(primeRelay, OUTPUT);
  pinMode(Vtimer, OUTPUT);
  pinMode(QuietTimer, OUTPUT);
  
  digitalWrite(killRelay, LOW);                                       
  digitalWrite(startRelay, LOW);
  digitalWrite(primeRelay, LOW);
  digitalWrite(Vtimer, LOW);
  digitalWrite(QuietTimer, LOW);
  
  Particle.connect();
  
  Blynk.connect();  
//  if (!waitFor(Particle.connected, msRetryTime))
//  WiFi.off();                // no luck, no need for WiFi
  Particle.publish("watchdog_test", "v2 Online");
}



BLYNK_WRITE(RESET)
{
  System.reset();
}
 

void loop()
{
  static uint32_t ms500 = 0;
  
  updateTimer.Update();
  if (millis() - ms500 < 500) return;
  ms500 = millis(); 
    
  Blynk.run();
  
    PhotonWdgs::tickle();
    if(lastmsg + 3000UL < millis()){
        Particle.publish("watchdog_test", "v2 ALIVE!");
        lastmsg = millis();
    }
  // Do more things in loop that need to run, whether connected to wifi/cloud OR not.
  
}

//============================================================================================================================================
void OnTimer(void) 
{ 
    //Handler for the timer, will be called automatically
}
void retryConnect()
{
  if (!Particle.connected())   // if not connected to cloud
  {
    Serial.println("reconnect");
    stopTimer.start();         // set of the timout time
    WiFi.on();
    Particle.connect();        // start a reconnectin attempt
  }
  else                         // if already connected
  {
    Serial.println("connected");
    Blynk.connect();
    Blynk.run();
    retryTimer.stop();         // no further attempts required
    retryRunning = false;
  }
}
void stopConnect()
{
    Serial.println("stopped");

    if (!Particle.connected()) // if after retryTime no connection
    WiFi.off();              // stop trying and swith off WiFi
    stopTimer.stop();
}

Maybe adding some Serial.print() statements to find out where the blocking occures might provide some clues.
Also what Device OS version are you targeting? Try 0.8.0-rc.10

OS Version 0.7.0
Never thought of running a pre release.
Watchdog resets before running anything in loop.
Didn’t want to show entire code as it might give you a headache :stuck_out_tongue:

If this is in connection with my question about SparkCorePolledTimer, is it that you do some stuff that wouldn’t be possible on a Software Timer callback, or any other reason?
It’s just that removing any not really needed libraries might help stability.

Is it ok to PM you the entire project?
Prefer not to show it publicly.

Sure.
If you have the project in Web IDE, you can send my a SHARE THIS REVISION link.

Thanks, sending to you now.