Timeout problem with a blocking Particle.connect()

I am working on a small project where I publish data from a sensor module over the cloud. The program checks whether the data has been published, and if not, saves the data onto the EEPROM. While I run the Photon in Semi-automatic mode, according to the documentation, waitFor() is used to timeout Particle.connect() and stop the device from flashing green (i.e. trying to connect to the Cloud) and continue on with the remaining portion of the code. However, once I step out of the WiFi’s range, the device is stuck trying to connect to the Cloud and Photon does not take not store any data from my sensors.

This is the code within my loop. I have written SYSTEM_MODE(SEMI_AUTOMATIC) in the initialization.

loop(){
 if(!Particle.connected())
{
  Particle.connect();
  waitFor(Particle.connected, 2000);
}  
  sprintf(toPublish_m, "%f - Value", num_m);
  Serial.printf("%f - Main\r\n", num_m);
  boolean sent = Particle.publish("main", toPublish_m);
  delay(1000);

  if(sent)
  {        
    process();
  }
  
  if(!sent)
  {
    Particle.disconnect();
    Serial.printf("Enter store condition\r\n");  
    store(num_m);
  }
    num_m++;
 }

How do i create a timeout (without using interrupts) to unblock the system from Particle.connect()?

I may have misunderstood how your code is supposed to work, but I think I would structure the code differently:

Put the Particle.connect() in setup().

In loop(), do something more like:

loop() {

    if(Particle.connected()) {
        sprintf(toPublish_m, "%f - Value", num_m);
        Serial.printf("%f - Main\r\n", num_m);
        boolean sent = Particle.publish("main", toPublish_m);
        if(sent)
        {
            process();
        }
    }

    if(!sent)
    {
        Serial.printf("Enter store condition\r\n");
        store(num_m);
    }
    num_m++;
}

@sujeeth19093, first I recommend you also add SYSTEM_THREAD(ENABLED); to run the user app in its own thread. Second, you have already specified a timeout of 2 seconds in:

waitFor(Particle.connected, 2000);

Note that 2 seconds is not long enough for the Photon to connect so you may want to stretch that to 10 seconds. By testing the return from waitFor(), you can see if it returned as a result of a timeout or a successful connection:

if (!waitFor(Particle.connected, 2000)) {
  // the connection timed out
}
else {  // use an else or simply flow through
  // connection was successful
}

:smiley:

And another misconception

waitFor() only waits for the action to succeede, but if it doesn't in time just carries on with your code but does nothing to the pending action.
If you want to stop a running action, you have to do that - e.g. via Particle.disconnect().

1 Like

@peekay123 and @rickkas7…thank you both for the help. I got it to work now. I did shift the Particle.connect() to setup and increased the length of the timeout for waitFor to 10 seconds and that seemed to do the trick.

I see. Thanks for clarifying that @ScruffR