Can't call Particle.function() more than once (per keepAlive ping)

With the Electron kept at the default keep alive settings of 23mins, sending command to the Electron is a pain. It only accepts command about every 23mins, that is when it shows “device came online” on the dashboard. It does not accept command at any other time.

I reduced the keep alive time to 60s, but still can only receive command every 60s. Is this normal? I need to use this for a real time control application, but the Electron doesn’t seems to be built for this. This kind of behavior is fine for reading sensors, but not good for control.

Is this done to save on data? What can I do to make the Electron respond to every of my command?

Then you must do something wrong, since most people (including myself) don’t experience that at all.

What does your code look like?

I bet you have an endless loop of code hogging up the electrons resources. I experienced similar behavior before dumping my old code and starting fresh. I started adding back one thing at a time and testing the code until everything was working. Not an ideal way to solve my issue because I still don’t know what the problem was. My code looks much better now though and runs on both photon and electron now.

@ScruffR and @superjuice, for now, I’m only using the stock Library on the Particle IDE. The only thing I added is the code for 3rd party SIM, and reduced to keep alive to 60s. I have had the issue with about three sketches I ran, find below my present sketch:

/* Includes ------------------------------------------------------------------*/
#include "NCD8Relay/NCD8Relay.h"
#include "spark_wiring_print.h"

SYSTEM_MODE(AUTOMATIC);
NCD8Relay relayController;
int triggerRelay(String command);

#include "cellular_hal.h",
STARTUP(cellular_credentials_set("internet.ng.airtel.com", "", "", NULL));



/* This function is called once at start up ----------------------------------*/
void setup()
{
	Serial.begin(115200);
	relayController.setAddress(0,0,0);
	Particle.function("controlRelay", triggerRelay);
	Particle.keepAlive(60);
}

/* This function loops forever --------------------------------------------*/
void loop()
{
}

int triggerRelay(String command){
	if(command.equalsIgnoreCase("turnonallrelays")){
		relayController.turnOnAllRelays();
		return 1;
	}
	if(command.equalsIgnoreCase("turnoffallrelays")){
		relayController.turnOffAllRelays();
		return 1;
	}
	if(command.startsWith("setBankStatus:")){
		int status = command.substring(14).toInt();
		if(status < 0 || status > 255){
			return 0;
		}
		Serial.print("Setting bank status to: ");
		Serial.println(status);
		relayController.setBankStatus(status);
		Serial.println("done");
		return 1;
	}
	//Relay Specific Command
	int relayNumber = command.substring(0,1).toInt();
	Serial.print("relayNumber: ");
	Serial.println(relayNumber);
	String relayCommand = command.substring(1);
	Serial.print("relayCommand:");
	Serial.print(relayCommand);
	Serial.println(".");
	if(relayCommand.equalsIgnoreCase("on")){
		Serial.println("Turning on relay");
		relayController.turnOnRelay(relayNumber);
		Serial.println("returning");
		return 1;
	}
	if(relayCommand.equalsIgnoreCase("off")){
		relayController.turnOffRelay(relayNumber);
		return 1;
	}
	if(relayCommand.equalsIgnoreCase("toggle")){
		relayController.toggleRelay(relayNumber);
		return 1;
	}
	if(relayCommand.equalsIgnoreCase("momentary")){
		relayController.turnOnRelay(relayNumber);
		delay(300);
		relayController.turnOffRelay(relayNumber);
		return 1;
	}
	return 0;
}

Maybe you go back to the Particle SIM and Tinker and see if you can control the D7 LED via the Tinker app more reliably.


(I’ve split your issue out of the original thread since it hasn’t got anything to do with that really.)

@ScruffR, I played around with the keep alive value, 55s and below seems to be the value that keeps the connection steady with my 3rd Party SIM. Also tried it with the Tinker app, the situation is the same. This means the Particle will require over 5Mbps per month for Keep Alive alone. Is this a common Keep Alive time for most 3rd Party SIM?

I have heard a common keep alive rate for 3rd part SIM’s is 30 seconds sometimes which would eat up 1mb every 3 days of your Electron was always on!

Yup, that's probably right, hence the Particle SIM isn't a bad deal after all. You cut down on the "dead" data consumption with the longer keepAlive and make the 1MB go further than with 3rd Party SIMs.
You need to find the sweet spot for your application for each respective SIM and compare them.

One thing to consider is that active communication (within that keep alive periode) will prevent the need for a keep alive ping and the system will not consume the "dead" data since your "value" data has already done the job.

Unfortunately, Particle SIM is very expensive compared to local 3rd party SIM, though I understand a lot of roaming agreement goes into this and the global roaming is priceless for those who will need that. In my country, for 3rd Party local SIM, I can get 10MB M2M data pack for 25cents monthly for single and low volume pack, and 5MB costs 2.5cents for high volume pack.

So the KeepAlive data consumption is a sacrifice I will be willing to make considering that Particle SIM costs $3.99 monthly (includes only 1MegaByte) in my country. Even though I’m yet to sign up for the M2M data pack, the 1.5GB data I’m using presently costs me an equivalent of $2.5 compared to about $16 I have to pay for 5MB on Particle SIM. For commercial rollout, I will only need to move up to 10MB data pack instead of 5MB data pack I was earlier planning to use which will only cost 5 cents for high volume pack.

1 Like