Startup() macro not running when waking Electron from sleep()

I got a very helpful response from @Moors7 yesterday to this post about turning on LED upon waking my Electron. He pointed me towards the Startup() macro which was exactly what I needed.

When I flash the device with my code and my application auto-executes, the startup() macro works exactly as expected. My external LED turns on almost right away. Great success! BUT!

After the device sleeps upon completing that first execution, waking the device does not execute the startup() code. The rest of the application runs fine, but the function I’m calling via Startup() does not run.

A possible clue - upon completing the flash, the tiny onboard LED at D7 on the Electron turns on and my LED comes on right after that. Every other time I run the application upon waking after a button press, the tiny onboard LED at D7 does not turn on and my LED does not come on. I’m assuming there’s a connection between the two but I don’t know what it is.

Are there any known limitations of using Startup() when waking via a button press? Am I missing something obvious here?

@dpatey, the Startup() macro only runs when the Electron is reset, In you case, waking after sleep does NOT reset. Only if you use deep sleep does the Electron reset. As such, you need to look at different way to do what you want.

Using the “STOP” sleep mode causes the Electron to resume code right asfter the sleep() call. This is where you need to turn on D7. Not sure where you turn it off though. You could use a Software Timer in one-shot mode (run only once) to turn on D7 for a fixed amount of time then turn it off again. Posting your code might help.

Thanks @peekay123. I’ve posted the code below. The sleep mode I’m using (which I thought was stop, but which does not behave as you’ve described Stop’s behaviour) results in the device waking, blinking green for about 15 seconds while it looks for internet, breathing cyan for 12 seconds or so while it runs through my application code from the top, and then going back to sleep.

When it runs the first time from a flash, the LED at D7 and MY LED at D6 both turn on. Note that I’m not turning D7 on at any point, the system turns it on. I assume this is because the system turns that light on at reset, as you explained.

 // First we need some power. 
    int power = A5; 
    int led1 = D6;

void setup() {

// The pin powering the button is output
pinMode(power,OUTPUT); 

// Now we crank up the juice on that power pin
digitalWrite(power,HIGH);

// Debug
//Serial.begin(9600);
}

void hit_the_lights() {
    // Set the LED up as an output
    pinMode(led1, OUTPUT);
    digitalWrite(led1, HIGH);
}

STARTUP(hit_the_lights());

void loop() {

    if (Cellular.ready()) {
        
        // First we'll measure the fuel and log it to our console.
        FuelGauge fuel;
        float value;
        value = fuel.getSoC();
        String stateOfCharge = "{\"batt-value\": \"" + String(value) + "\"}";
        Particle.publish("fuel", stateOfCharge);
        delay(2000);
        
        // Now we send the signal to IFTTT that the button's been pushed and it does the rest.
        Particle.publish("buttonState","pushed");
        Particle.process();
        delay(10000);
        
        // Turn the LED off
        digitalWrite(led1, LOW);
        
        // Nighty night
        System.sleep(WKP,RISING);
    
    }
}

@peekay123 - confirmed, putting the device into SLEEP_MODE_DEEP instead of STOP triggered the reset and ran Startup() as you explained it would. The docs mention that I can use a RISING signal on the WKP pin to wake the device early from Deep Sleep, but it doesn’t seem to work. Very simply changed my sleep call to:

System.sleep(SLEEP_MODE_DEEP,30);

And it runs every 30 seconds and turns my LED on nicely. But my button, which I know is capable of sending a RISING signal to the WKP pin because that’s what it does in Stop mode, doesn’t wake the device as the docs say it should* when I press it. I’m continuing to tinker but I’m not sure what to even tinker with, as its a pretty simple call. :expressionless:

  • from the docs… Note: You can also wake the device “prematurely” by applying a rising edge signal to the WKP pin.

EDIT: I’ve also tried the System.sleep(SLEEP_MODE_DEEP); approach, no dice there either.

Answered in your other thread