Never mind guys, just found this code in another thread and it did the job:
SYSTEM_MODE(SEMI_AUTOMATIC)
SYSTEM_THREAD(ENABLED)
const uint32_t msRetryDelay = 5*60000; // retry every 5min
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
void setup()
{
Serial.begin(115200);
pinMode(D7, OUTPUT);
Particle.connect();
if (!waitFor(Particle.connected, msRetryTime))
WiFi.off(); // no luck, no need for WiFi
}
void loop()
{
// do your stuff
digitalWrite(D7, !digitalRead(D7));
if (!retryRunning && !Particle.connected())
{ // if we have not already scheduled a retry and are not connected
Serial.println("schedule");
stopTimer.start(); // set timeout for auto-retry by system
retryRunning = true;
retryTimer.start(); // schedula a retry
}
delay(500);
}
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 reconnectino attempt
}
else // if already connected
{
Serial.println("connected");
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();
}