Deep sleep and variables

I am encountering issues with the deep sleep mode with persistence of variables. The documentations states:

Note: The new Particle Photon firmware will not reset before going into stop mode so all the application variables are preserved after waking up from this mode.

However, in my test I have one variable declared that is initialized with analogRead() the first time around (if variable == 0). The sleep mode is activated with following in setup()

System.sleep(SLEEP_MODE_DEEP,60);

In 60 seconds, when the device wakes up, the value in the variable is reset to 0.

Am I interpreting the documentation wrong?

If i am not wrong, this feature is yet to be available.

Ping @mdma

Aha! So, all this time I am assuming that whatever is in the documentation (and available on the firmware library) is implemented.

No wonder, 'cuz I just put a simple System.sleep(A7, FALLING); in setup() and blinking led in loop() and expected the loop() to never execute but the device never went to sleep. The only thing working is System.sleep(SLEEP_MODE_DEEP, long seconds) but without the register persistence.

If you are using FALLING, did you add a Pull-up resistor? There’s some issues with sleep and it will be addressed once @satishgn gets to it :smile:

Yes, I did. But if that were a problem, the device will still sleep and not wake up isn’t it? :slight_smile: Anyway, I am going to wait for next version of the firmware before experimenting with advanced functions.

Maybe the difference is whether you’re using the Web IDE or local compiling, but I’ve been using the following successfully compiling locally on “latest”:

#include "application.h"
//SYSTEM_MODE(MANUAL);//!!!!!!!!!!!!!!!!!!!!!!!!!!!
SYSTEM_MODE(SEMI_AUTOMATIC);
char publishString[63];
const uint32_t SOFTDELAY500ms = 500UL;
const uint32_t SOFTDELAY1s   = 1000UL;
const uint32_t SOFTDELAY2s   = 2000UL;
const uint32_t SOFTDELAY3s   = 3000UL;
uint32_t lastTime;
bool receivedflag;

void myHandler(const char *event, const char *data){
   receivedflag = TRUE;
}//myhandler

void setup() {
 //   WiFi.antennaSelect(EXTERNAL);
    pinMode(D7,OUTPUT);
    Time.zone(-7);
    Spark.subscribe("MotionTimeReceived", myHandler, MY_DEVICES); 
    lastTime = millis();
    while(millis() - lastTime < SOFTDELAY1s) { // not sure why this is needed
     Spark.process();
   }
	receivedflag = FALSE;	
}//setup

void loop(){
    delay(1000);
    WiFi.off(); //isn't sleep on photon supposed to do this automatically?

	  System.sleep(D3, RISING); //Sleep indefinitely till D3 HIGH    
//    System.sleep(SLEEP_MODE_DEEP,0);
    Spark.connect();  

    lastTime = millis();
    while(!Spark.connected()) { // not sure why this is needed
     Spark.process();
     delay(50);
    }//while(millis() - lastTime < SOFTDELAY1s)
    delay(500);
    sprintf(publishString,"%04d/%02d/%02d %02d:%02d:%02d",Time.year(),Time.month(),
        Time.day(),Time.hour(),Time.minute(),Time.second());
	  if (Spark.connected()) {
      delay(100);//may? cause redflash?
	    Spark.publish("MotionTime",publishString,60,PRIVATE); 
      lastTime = millis();
      while((!receivedflag) && (millis() - lastTime < SOFTDELAY3s)) { // allows processing of receipt msg?
       Spark.process();
       if (receivedflag == TRUE) { //confirm receipt msg
        delay(500);
        digitalWrite(D7, HIGH);
        delay(500);
        Spark.process();
        digitalWrite(D7, LOW);
        delay(500);
       }//if (receivedflag == TRUE)
      }//while((!receivedflag) && (millis() - lastTime < SOFTDELAY3s))
    receivedflag = FALSE;
	  }//if (Spark.connected())
}//loop

I’m using a 4k7 pulldown resistor.
Hope this helps

Hello @rajiv,

Currently after adding the latest link time optimisation(LTO) step during build phase, we have noticed that sleep is not working consistently.

If you are able to locally build and deploy via dfu-util, you can try the following make command to get sleep working.

make v=1 clean all PLATFORM_ID=6 MODULAR=n COMPILE_LTO=n program-dfu

In general Photon’s sleep should work as follows:

When you have something like below code. it uses the STM32’s low power “STANDBY” mode where after waking from deep sleep, it won’t retain the SRAM contents. It’s akin to timed RESET.

System.sleep(SLEEP_MODE_DEEP,60);

To have your SRAM variables and execution state retained on photon during sleep, you need to call STM32’s low power “STOP” mode via

System.sleep(D2, RISING, 60);//wakeup on rising edge of D2 or after 60 seconds
1 Like

Hey @satishgn,

I’m trying to set up the Photon wakeup pin.

When I do have system.sleep command with 10 sec - the wakeup time works and the pin wakeup works. when I change the time to 60 sec the photon goes to sleep forever.

int SleepTime = 60;

void setup() {
  Serial.begin(9600);
  
}

void loop() {
  delay (5000);
  
  Serial.print("going to sleep for "); Serial.print(SleepTime); Serial.println(" sec");
  
  System.sleep(D0,RISING,SleepTime);
  
  if (digitalRead(D0) == HIGH) {
      Serial.println("Door is Open");
        Spark.publish("Door-Open", "HOME", 60, PRIVATE);
        //while (digitalRead(D0) == HIGH); // hang tight here until motion stops
    }
  
}

Do you have any suggestion?

Hi @itayd100 which version of the firmware are you using. If it was 0.4.3rc2 it could have been a problem as LTO was enabled due to which sleep was not working as expected.

If you have the option of testing locally, please try the firmware’s develop branch: https://github.com/spark/firmware/tree/develop

A new release for use with Web IDE should happen in the next week.

1 Like

Hey @satishgn,
I finally succeed to burn it locally (firmware 043) but I still have issues with that - the wakeup pin doesn’t work.

I am having another issue: although the Photon burned with the new application I get “Error 2” and “Error 74”:

Downloading to address = 0x080a0000, size = 2752
Download	[=========================] 100%         2752 bytes
Download done.
File downloaded successfully
dfu-util: Error during download get_status
make[1]: *** [program-dfu] Error 74
make: *** [modules/photon/user-part] Error 2

I tried to work by this, but I still get the error.

Someone else?

There’s some issue with the Sleep function and as far as i can tell the download went ok

1 Like

@kennethlimcp thanks for your answer. I just want to say that you are really active here and happy to help all the time - Chapeau Ă  lui!

And now, back to business, Do you know if this going to be fixed soon?

1 Like