Controlling Cell Connection On Electron

I live in an area with very poor cell service and the current project I’m tinkering with made me realize that as written, the Electron fire’s up, tries to connect to the cell network and drains the battery trying to connect. I would like to change this behavior but not 100% sure how.

Since Particle.connect() blocks execution of the code inside the loop, If possible, I think the right approach is to use an interrupt to break out of the blocking after a given amount of time and execute code to put the electron back to sleep. The docs on attachSystemInterrupt() are thin so I’m unsure if this is even possible.

Any points are very much appreciated.

You won’t need system interrupts for that.
For one, in SYSTEM_MODE(MANUAL) Particle.connect() should not be blocking (that may need testing but is what I heard from Particle devs in the past).
Also Software Timers should provide some parallel execution as well as the brilliant SparkIntervalTimer library contributed by @peekay123.

If you want to go the attachSystemInterrupt() function you may also need to delve into the setup of hardware timers - something the SparkIntervalTimer library already takes care of.

2 Likes

@ScruffR - Thank you for suggestion/help.

1 Like

Here’ something I have:

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
stateTime = millis();
}

void loop() {
         if (!Cellular.ready() && millis() - connectTime >= 8000) {
            connectTime = millis();
	        Serial.println("connecting to cellular network...");
            Cellular.connect();
        }
        if (Cellular.ready() && millis() - publishTime >= 8000) {
                publishTime = millis();
                Serial.println("connecting to Particle cloud...");
                Particle.connect();
        }
	    if (Particle.connected()) {
	        Serial.println("connection established!");
	        state = PUBLISH;
	        break;
	        }
	    if (millis() - stateTime >= 90000) {
	        Serial.println("unable to establish a connection"); 
                // do whatever you want here
	        }

This will first call Cellular.connect() every 8 seconds, and then call Particle.connect() every 8 seconds. If after 90 seconds it still hasn’t connected to the Particle Cloud, you can let it do something else (which in my case, would be to writing some data in its EEPROM and going into Sleep mode)

1 Like