Resetting via Firmware System.reset()

I’m working on my first project and I wanted to be able to reset the Electron (or photon) remotely. In development, I’ve been using the command line tools on a mac. Here’s the sequence:

System.reset();
return 1;

When issuing the command to do the reset, I noticed that the command line client on the mac (particle) times out because its waiting for a return value. However, none is ever sent because the device resets before executing the return instruction. Swapping the two command’s position causes the function to return the value, in this case 1, but never executes the system.reset(); (and that’s what I expected).

My question - Is there a more elegant way to reset the electron/photon remotely without causing the timeout event to occur in the client?

1 Like

Set a flag in the function, return whatever you need to return, then check that flag in the loop and act accordingly?

2 Likes

@Moors7’s suggestion is actually a great suggestion for any Particle.function call. As little code as possible should be in the actual cloud function call. Ideally you’d just set a flag and then move on, going back to a return as soon as possible. Some pseudocode:


bool resetFlag = false;
int cloudResetFunction(string Command);

void setup()
{
  Particle.function("reset",cloudResetFunction);
  ...your code...
}

void loop()
{
  if (resetFlag)
     System.reset()
  ...your other code...
}

int cloudResetFunction(String command)
{
  resetFlag = true;
  return 1;
}
1 Like

That’s a really good idea. Thank you!

1 Like

I tried it out and it almost worked - here’s what I think happened:

Implemented the flag method as described above. Yet, the reset happened faster than then device had time to complete the cloud function and send the return code.

Learning from some other code I’ve seen, I programmed in a delay in the reset code that resides in the main and that let the cloud function complete before rebooting. I set the delay for 15 seconds initially but it could go be shorter.

Here’s the code I’m using. Any short comings anyone can see?

#define DELAY_BEFORE_REBOOT (15 * 1000)

unsigned int rebootDelayMillis = DELAY_BEFORE_REBOOT;
unsigned long rebootSync = millis();

void loop() {
    if ((resetFlag) && (millis() - rebootSync >=  rebootDelayMillis)) {
        // do things here  before reset and then push the button
        Particle.publish("Debug","Remote Reset Initiated",300,PRIVATE);
        System.reset();
    }
}

int cloudResetFunction(String command) {
       resetFlag = true;
       rebootSync=millis();
       return 0;            
}
1 Like

Ah, good catch! Looks good. A countdown till reset would be kinda cool.

1 Like