Need help with System events on v0.5.2

I am trying to check out the functionality of system events. I tried the example sketch but i do not get any publish on pressing the button or from calling System.reset() from the cloud.

void setup()
{
System.on(reset,reset_handler);
}

void reset_handler()
{
    Particle.publish("reset");
}

What am i missing out on?

I guess because a publish is a task that’s too long for such an event.
The Particle.publish() only queues the event, but the sending will happen (actually not) after the handler will be finished.

Try to blink D7 instead or hang around in the handler for some seconds to get the event published to the cloud.

1 Like

I did this and tried to run the method by itself. It blinked the LED on for 5 seconds and then off.

void reset_handler() {
    unsigned long startMillis = millis();
    digitalWrite(D7, HIGH);
    while ((millis() - startMillis) < 5000)
    {
        //HANG IN HERE
    }
    digitalWrite(D7, LOW);  
}

When the system resets after a firmware update the code executes but if i call System.reset() from a particle function nothing happens nor does pressing the reset button do anything.

I’m not sure about System.reset() but for the RESET button I’m not surprised as the code hasn’t got any say in preventing the reset once the µC is going down, which is the inevitable, hard wired effect of pulling the RST pin down.

The behaviour with the System.reset() command might be something for @BDub to clarify.

I too had my doubts about the button but i do see the handler go active when i do an OTA firmware update.

I think you are better off setting a flag in the handler (which should be treated as an interrupt), and using that flag to call System.reset() later.

Looking at your code, it appears you are trying to find a way to delay resetting after OTA updating… here’s some code that will do that:

#include "application.h"
SYSTEM_THREAD(ENABLED);
void setup() {
    System.disableReset();
    pinMode(D7, OUTPUT);
}

void loop() {
    digitalWrite(D7, !digitalRead(D7));
    delay(50);
    
    if (System.resetPending()) {
        digitalWrite(D7, HIGH);
        uint32_t startTime = micros();
        while (micros() - startTime < 3000000UL) Particle.process();
        System.enableReset();
    }
}

Actually i wanted the touchscreen to display system is resetting or updating when a call is made to system reset or OTA update is triggered. I am not using system threading. Is that a pre req for using System events?

Ahh, ok that makes sense. If the device wants to reset, you need to delay that so you have time to display your message as per my above example shows. If you want to know you are receiving an update, you can use the system events. Multi-threaded is not required for system events to work.

Thank you! I will use the above example.