WaitFor(Particle.connected, 10000) is not timing out

I have a prototype that has been working well for sometime - last night I just noticed that during a ‘wakeup from standby’ function that calls WaitFor(Particle.connected, 10000) it has got stuck on this function and the timeout has not, well, timed-out. The LED is flashing cyan, the application had gone through checking the wifi connection. Is there a reason for this application blocking/not timing out? Is there a better/more reliable way to wait for a cloud connection for a period then move on?

@armor, showing some code would be helpful. What system mode did you specify? Is system threading enabled?

@peekay123

These are set at the start of the program.

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

Here is the code that stuck on the waitFor.

if (opMode == connected)
{
    if (!Particle.connected()) Particle.connect();
    progressBar(60);
    waitFor(Particle.connected, 10000);
    updateHeader();
    progressBar(100);
    if (Particle.connected()) connectedOnce = true;
}

I know from the progress bar that the code got stuck after the 60% - LED was flashing cyan.

I’ve seen this, as well. I was running solid for 22 days before my last reboot - but not even a day after I reflashed due to another issue, I hit this flashing cyan, and the waitFor timeout never forced a reboot:

  if(wifiCheck)
  {
    wifiCheck = false;
    if (!Particle.connected())
    {
      Particle.disconnect();
      delay(5000);
      Particle.connect();

      if (waitFor(Particle.connected, 10000)) {
        delay(10000);
        //Connected
        Particle.publish("app/reconnect", "Reconnected to the Cloud after a hang!", 3600, PRIVATE);
      } else {
        //Timeout... Lets reset.
        System.reset();
      }
    }
  }

@legoguy Did you manage to work through this? I’ve also got a WaitFor loop which won’t kick out. Not sure what alternatives there are or whether I’ve got something wrong in my code…

if (waitFor(WiFi.ready, 10000)) {               //wait for 10 seconds to confirm whether the photon is connected to the wifi, if not sleep. 
    
        if (waitFor(Particle.connected, 10000)) { //wait for 10 seconds to confirm whether the photon is connected to the cloud, if not sleep. 
        
        voltage = lipo.getVoltage();	        // returns a voltage value (e.g. 3.93)
        soc = lipo.getSOC();                    // returns the estimated state of charge (e.g. 79%)

Looking forward to hearing results.

@neal_tommy, if you are running in SEMI_AUTOMATIC mode, you can simply call Particle.connect(), then do if(waitFor(Particle.connected(), 10000). Using this form with the if() will fail if there is a timeout so do you have an else as well?

@peekay123, I didn’t realise that we had to have the Particle in SEMI_AUTOMATIC mode for this? I changed some code and seem to have a work around in AUTOMATIC mode so will let the Particle run a few days and then see from there.
Thanks for the note.

1 Like

@neal_tommy

I haven’t seen a reply from @jvanier about this other than in 0.5.0 a change was made after his investigation to kick out of the wifi chip stack after 60 seconds. I have stopped using waitfor and instead set a unsigned long variable with mills() and then keep checking check until the required timeout period. This always works. If you are in SEMI_AUTOMATIC mode be aware that you should only call Particle.connect() once - @mdma made a cryptic comment about this a month or so back - it seemed to have solved the issues I had.