Try this snippet:
SYSTEM_MODE(SEMI_AUTOMATIC);
uint8_t retry_count = 0;
unsigned long old_time = millis();
void setup(){
WiFi.on();
}
void loop(){
if(millis() - old_time >= 2000){
if(retry_count < 10){
if(!WiFi.ready()){
WiFi.connect();
retry_count++;
}
else if (!Spark.connected()){
Spark.connect();
retry_count++;
}
}
else{
WiFi.off();
retry_count = 0;
WiFi.on();
}
old_time = millis();
}
// place the rest of your code below
}