Loop not executed when electron is offline

I have a simple loop printing out a string. It works fine until I disconnect the electron’s antenna , goes offline and starts blinking green. The loop seems to not be called when its not connected to cellular network. Anyone encounter this problem?

That’s the intended behavior.

In order to have user application code run independently of having cloud connectivity, you must either use SYSTEM_THREAD(ENABLED):
https://docs.particle.io/reference/firmware/electron/#system-thread

Or use SEMI_AUTOMATIC or MANUAL system mode:
https://docs.particle.io/reference/firmware/electron/#system-modes

The loop seems to keep running with SYSTEM_THREAD(ENABLED) if it loses connection. But it seems like when the device it boots up, it never calls loop() if its not connected. How can I get it to call loop at boot up even if there is not a connection?

That’s strange. I just tested an Electron 3G (U260) and the code below and it starts printing out the dots immediately, while it’s still blinking green after booting.

#include "Particle.h"

SYSTEM_THREAD(ENABLED);

void setup() {
	Serial.begin(9600);
}

void loop() {
	Serial.println(".");
	delay(1000);
}

Tested with system firmware 0.5.1.

Hmmm… your code seems to work but if I try to do some stuff in the setup it never calls loop at boot up. It only happens when it boots up and doesn’t connect. Is there something I shouldn’t do in setup?

It seems to be stuck at a .publish call in my setup code. Docs say it should return false if there is no connection. Why is it stuck at that call? If I remove that call, it works fine.

Having the same issue, my code is being stuck at the .publish call, when the Electron is offline. Can someone please suggest how I can resolve this ?

// Create an array with 5 locations to store values in
int light[5];
int LED_PIN             = D7;

// The variable we'll use to keep track of where we are in the array
int i = 0;

// This is where we'll store the last time a measurement was taken
long lastMeasurement = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {

    digitalWrite(LED_PIN, LOW);
    
    if(millis()-lastMeasurement > 1000){
        // Measure the value on the photoresistor, and put it into the array
        light[i] = analogRead(A0);
        digitalWrite(LED_PIN, HIGH);
        // Keep track of when last measurement was taken
        lastMeasurement = millis();

        // If we've taken 5 measurements (0-4, inclusive) then we should send that data
        if(i == 4){
            
            Particle.publish("L", String::format("%d,%d,%d,%d,%d", light[0],light[1],light[2],light[3],light[4]));
            // Reset index to beginning
            i = 0;
        }
        else {
            // If it wasn't the 5th measurement, increase the index by 1
            i++;
        }
    }
    
}

@Falcon, test for Particle.connected() before tyring to publish! :wink:

1 Like

When you say you have the same issue, you may also try the same solutions already provided in this thread - see second post

Yes, read all posts in this thread and apply the suggestions.

2 Likes

The loop doesn’t seem to execute when the cellular connectivity is lost. (Testing on Electron with firmware version: 0.6.4), can someone please advice how I can resolve this ?

#include "Particle.h"

SYSTEM_THREAD(ENABLED);

int count = 0;

void setup() {
	Serial.begin(9600);
}

void loop() {
    count += 1;
	Serial.println("actualCount: ");
	Serial.println(count);
	delay(1000);
	
	if (Particle.connected()) { // To check if the device is connected
        Particle.publish("f", "1u", PRIVATE, WITH_ACK);
        Serial.println("publishedCount: ");
	    Serial.println(count);
    }
}

The problem is the use of the WITH_ACK flag. If you remove that, the loop will continue to run.

The reason is that with WITH_ACK, Particle.publish will block, waiting for the ACK. The Particle.connected() test before that is helpful, however it doesn’t solve the problem entirely.

The reason is that because the Electron only pings the cloud every 23 minutes, it only discovers that it’s disconnected when you go to publish most of the time. It’s this one that’s causing your loop to block when you use WITH_ACK.

Rick

2 Likes

Thank you. It is working now.

1 Like