Electron fails to publish events after a few minutes

I have a very simple application running on two 2G global electrons, one has a 3rd party sim and the other a particle sim. The firmware sleeps and is woken on a rising edge on D1, it should then publish an event, blink the onboard led and then go back to sleep. (code is below, keep alive and cellular credentials setup are obviously omitted from the device with the particle sim)

Directly after reset everything functions perfectly but after being left for a minute the device wakes, blinks the onboard led but doesn’t publish an event. The onboard RGB LED breathes cyan for approximately 30 seconds before the device goes back to sleep without publishing the event. Seems like it is timing out and giving up.

I can’t get my head around how why it would start being unable to publish over time with no change to the firmware. It’s also worth noting that the same firmware previously worked perfectly for weeks so must be a new issue.

If I hit reset on the device it publishes the events perfectly again, but only for a minute before the problem reappears.

Device is running whatever the most recent firmware is. (updated with the cli but it doesn’t give you a version number)

#include "Particle.h"

int boardLed = D7;
int sensor = D1; 

STARTUP(cellular_credentials_set("vodafone", "", "", NULL));


void setup() {
    
	Particle.keepAlive(120);
	
	pinMode(boardLed,OUTPUT); 
	pinMode(sensor,INPUT_PULLDOWN);

}


void loop() {
	
	System.sleep(D1,RISING,SLEEP_NETWORK_STANDBY);

	if (digitalRead(sensor)==HIGH) {

               String status = "PinHigh";
                Particle.connect();
		Particle.publish("googledocs", "{\"status\":\"" + status +"\"}", PRIVATE);
		digitalWrite(boardLed,HIGH);
		delay(100);
		digitalWrite(boardLed,LOW);
		delay(100);
	}

	

} 

Since you are running default SYSTEM_MODE(AUTOMATIC) there is no need to call Particle.connect() (it even may have a negative impact) and you may want to consider adding this instead

  ...
  if (waitFor(Particle.connected, 30000)) {
    Particle.publish(....);
    ...
  }
  else {
    Serial.println("No connection - going back to sleep without publish");
  }

Just to make sure, your sensor pin actually stays HIGH all the time, right?
And you may want to use sensor instead of D1 in your sleep call.

I’m also not sure why you need a String variable for a text that will never be anything else than "PinHigh" - just hardcode the entire publish payload.
If it will eventually become dynamic, consider using C-strings (aka character arrays) with snprintf() instead of String.

Thanks for your suggestions and code review.

I have implemented the changes you suggested above but still have the same behavior. Interestingly the else {} is never executed, even if the event isn’t published. I have also noticed that it seems to be the length of sleep which triggers this behavior, if I keep constantly waking the device every 30 seconds or so it will continue to publish normally.
Another strange behavior I have noticed is that the device will sometimes perform a full reset when woken.

Would I have more stable behavior if I use the Manual system mode?

Is this true for on the Particle SIM device too?
Can you try this with device OS 1.5.0-rc.2?

Is 30sec the limit or have you tried increasing the sleep period to find the breaking-point?

I'd rather try SYSTEM_THREAD(ENABLED) (with some slight code changes to account for the asychronisity).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.