Photon Not Waking Up

I used the following code to test the sleep mode. I wanted to put it to sleep for 30 seconds and then have it wake back up.

#define WAIT_SECONDS 300
#define LED D7

int LastCheckIn = 0; //Last time the Photon published the time

void setup() {
    pinMode(LED, OUTPUT);
    Particle.publish("Battery Timer Online", NULL, 60, PRIVATE);
}

void loop() {
    int now = Time.now();
    if (now - LastCheckIn > WAIT_SECONDS) {
        LastCheckIn = now;
        Particle.publish("batterytimer", NULL, 60, PRIVATE);
        digitalWrite(LED, HIGH);
        System.sleep(30);
    } else {
        digitalWrite(LED, LOW);
        System.sleep(30);
    }
}

It only reported immediately after receiving the new firmware and has been to sleep for almost 2 hours. I have 3 questions:
1- Was my code written incorrectly and that is causing it not to wake up? Previously I had the exact same code without the 2 sleep instances and it was running for 4+ hours without an issue.
2- If the code was the problem, how should I have written it?
3- How can I reflash firmware to it since the wi-fi module is not on?

The problem is that you’re putting it to sleep in both the if-clause and the else-clause, so every time loop runs. you’re immediately putting it to sleep again. I think you probably want to take out the sleep command in the else-clause. To flash new firmware, you need to put the device in safe-mode (see here).

4 Likes

I second Ric’s assessment, but I’d also ask: Are you really intending to only power off the WiFi module?

I’ve never really understood the use of System.sleep(time) as it might just reenable the WiFi module at any unpredictable position in your code - especially when running bigger projects.
When I want to power down the WiFi module I’d rather call WiFi.off() and Particle.connect() where and when my logic wants it done - since the µC is still running, keeping track of the “sleep” period is easily done in code.

IMO the only useful applications for System.sleep() are Stop Mode (e.g. System.sleep(wake_pin, wake_edge, sleep_period)) or Standby Sleep (aka Deep Sleep System.sleep(SLEEP_MODE_DEEP, sleep_period)).

2 Likes

@Ric removing the sleep call from the loop did the trick.

@ScruffR I had not thought about turning off the WiFi but itself but this was just for testing purposes to test the System.sleep and see how long the battery lasts with the Photon.

Thanks for the help.