Electron halts after issuing particle.disconnect command

Hi

I am using particle in Semi Automatic mode. I issue particle.connect() during the setup process and wait for about 40 seconds (using systemIntervalTimer Library to generate interrupt after 40s). if particle is not connected to cloud in 40s then in ISR, I use particle.disconnect() command to continue application without connecting to cloud.

but after particle.disconnect(), electron never returns to application.

This happens everytime I remove the power to electron and reapply power. It seems that after every power cycle, electron requires long time to connect to cloud and hence particle.disconnect command executes.

On the other side, If don’t remove the power but press the reset key, then particle connects to cloud immediately.

I don’t understand why electron takes long time to connect cloud if power is taken off and reapplied

Please suggest if I am doing anything wrong

Post your code.

1 Like
SYSTEM_MODE(SEMI_AUTOMATIC); 

#include "SparkIntervalTimer.h"
IntervalTimer CloudTimer;
bool FirstRun = 0;
int attempt = 0;

void setup() {
  Serial.begin(9600); 
  CloudTimer.begin(ParticleDisconnect, 40000, hmSec);
  attempt++;
  Particle.connect();
  delay(1000); // To avoid entering in the loop
  Serial.println("SET UP COMPLETE");
}

void loop() {

 if (FirstRun == 0) {
    Serial.println("IN THE LOOP");
    delay(1000);
    FirstRun = 1;
    Serial.println("First Run");
    if (Particle.connected() == true) {
      Serial.println("CONNECTED");
      CloudTimer.end();
      delay(1000);
    }
    else {
        Serial.println("NOT CONNECTED");
    }
  }

 //APPLLICATION CODE
}


void ParticleDisconnect(void) {

  if (attempt > 0 && Particle.connected() == false) {
    Particle.disconnect();
    delay(1000);
    CloudTimer.end();
    Serial.println("Disconnecting");//Electron halts at this line and never go to execute application code
  }
  
}

@pshah, you may want to read about interrupt service routines. They need to be small, fast and non-blocking. In your case, non of these are satisfied. More specifically, making system calls (Particle.connect) is not good and the delay(1000) is absolutely not a good idea, thus the reason for the problems you are having.

To achieve what you need, here are two (of many) approaches. First, use a Software Timer instead of SparkIntervalTimer. However, in the timer callback, set a flag that you can read in loop(). Then in loop() do what your ISR is presently doing. Perhaps more effective would be using waitFor(Particle.connected, 40000); which will wait for a connection to occur or 40 seconds, whichever comes first. The call will return false if the timeout occurred first.

It is important to note that the recommended connection wait time for the Electron is 5 minutes, not 40 seconds.

2 Likes

Thanks. I will look into ISR.

However, why there is difference in connection time to cloud in different situation.

  1. After power on, it takes long time to connect to cloud
  2. after one cycle of cloud connect and disconnect, it takes very less time to connect to cloud again. (even in case of first failed attempt to connect to cloud, second attempt takes very less time to connect to cloud).

Further, I used sparkinterval library as they were based on hardware timer interrupt. After issuing particle.connect only hardware interrupt is serviced and no other commands are executed. am I right? Hence, I didn’t use software timer. I will try with software timer too in case it works. but not sure.

I also want to do some activity, when particle is trying to connect to cloud, which I do in another ISR.

However, I will make sure that they will be small and fast too.

I will post the results after I test it.

Actually not. Even loop() continues to run after that, even more so the dedicated FreeRTOS thread servicing Software Timer :wink:

For that I'd suggest you give SYSTEM_THREAD(ENABLED) a try before pulling out the big guns like ISR :sunglasses:

1 Like

Sorry I meant that when it is attempting to connect to cloud. Once it is connected, the application code resumes. I thought that from the time issuing the particle.connect command to the time electron is connected to cloud, only interrupt can execute.

SYSTEM_THREAD(ENABLED) seems to be better choice.

Thanks.

I will keep the result posted.