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();
}