NCD1Relay + Particle + Momentary Function

Hi,

I got the a NCD1Relay + Particle and have been using the cloudcontrol code with some modification to the momentary part:

if(command.equalsIgnoreCase("momentary")){
	relayController.turnOffRelay();
	Particle.publish("Status", "OpeningFor10Min");
	commandEntered = "OpeningFor10Min";
	delay(600000);
	relayController.turnOnRelay();
	commandEntered = "ClosingAfter10Min";
	Particle.publish("Status", "ClosingAfter10Min");
	return 1;

This is not ideal as it causes the Photon go offline during this time. Is there a cleaner way to do this? I need it to turn off or on for 10 Minutes when a function is called. Any help greatly appreciated.

Hi,
First thing I would try is something like this:
1- when the momentary command is received, you set a global flag
2- this flag is then catched by the loop() function and you delay(600000) there
3- I would make sure my firmware has this line:
SYSTEM_THREAD(ENABLED);

Second thing I would try (it builds on top of the first one):
I would change previous step 2 so instead of using a delay it would use a timer to turn off the relay.
This will guarantee the processor does not lose wifi connection I believe.

Good luck
Gustavo.

1 Like

To add to Gustavo’s comments, a safer substitute to your Delay() is this line:
for (uint32_t ms = millis(); millis() - ms < 600000; Particle.process());

The Photon won’t do much else during the 10 minutes, but it will stay online and be able to process a “Stop” Function, etc, over the Cloud.
( +1 to using SYSTEM_THREAD(ENABLED); ).

OR:
If you ever want to turn the Relay ON for a given amount of time, You could also try the Momentary Class seen Here
relayController.momentaryRelay(1, RunTimeSecs*1000);
That would automatically turn ON the Relay for the selected RunTime with no delays required, (the opposite of your use-case)

I use this scenario a lot with the NCD relays:

// Pass RunTimeSecs through a Particle Function over the Cloud
  relayController.turnOnRelay(1);   // Relay ON
  previousMillis = millis();        //  previousMillis is a Global unsigned long
  while (  (millis() - previousMillis)  < (  (RunTimeSecs * 1000) - 2000)   )    {   // a "Safe Delay"
  // This will Exit the While Loop() 2 seconds before the Pump Stops- so you can measure the pump's operating pressure, final Amps, etc
    Particle.process();
  }
  // Measure Operating Pressure, etc, here- before turning the Relay OFF. 
 relayController.turnOffRelay(1);   // Relay OFF
1 Like

Thanks @rfontaine @gusgonnet this worked perfectly .

1 Like