Block in the loop when Spark.connect()?

in this post, i know the photon trying to connect to the cloud can be expressed as
if( !Spark.connected() && WiFi.ready() ).

And now i want to made a LED blink quickly as the photon is trying to connect to the cloud, the code is as followed.

void loop() {
    if(!Spark.connected() ){
        
        if(WiFi.connecting())       blink(A6);
        else if(WiFi.ready())      blink(A5);
  
        Serial.println("un connected   ");
        Serial.println(n);
        n++;
    }   
}

However, when the WIFI is connecting i can see the LED blinking quickly however when photon is trying to connect to cloud, it would never blink another LED, and nor any prints out from the Serial. I guess the Spark.connect() API block the loop process, is it ? how can I get around it?

@helxsz, I had this little sketch whiped up for another thread.
As I understand it does something similar to what you want.

SYSTEM_MODE(SEMI_AUTOMATIC);

const uint32_t waitConnect = 5000; // at least 5 sec between reconnect attempts

uint32_t msConnect;
bool firstAttempt;

void setup() 
{
    pinMode(D7, OUTPUT);
    WiFi.connect();
    firstAttempt = false;
}

void loop() 
{
    if (firstAttempt && Spark.connected())
    {
        firstAttempt = false;
    }
    else if(!firstAttempt  && !Spark.connected())
    {
        Spark.connect();
        firstAttempt = true;
        msConnect = millis();
    }

    digitalWrite(D7, (millis() >> 2) & 0x88); // just some LED pattern ;-)
}

Maybe this helps you a bit (I’d hope :sunglasses:)

By first watching the LED pattern stop from time to time and corresond these to the RGB LED, you can get a feeling of what’s going on; where connection blocks your code and where your code is running free.
From this on you can wrap your own conditional code blocks (e.g. WiFi.connecting(), !WiFi.ready(), !Spark.connected(), Spark.connected(), …) around the devices behaviour.

1 Like