Millis() or another time function while in sleep


#1

New day, new problem

Is there any way to know if the deep sleep was interrupted by the timer or the WKP pin?

Right now I have a quick check of millis() vs a previous retained one. but it seems that the millis() after waking up start counting from zero.

I assume there must be a clock that keeps track while sleeping. can anyone tell me which function that is?

thanks,
Ouch


#2

That would the RTC via Time.now()


#3

is Time.now() cloud based? I am searching for it on the documentation for a non cloud dependant clock.


#4

@ouchboy, Time.now() uses the STM32 RTC so as long as the processor is powered, even if only via Vbat, then the time will be maintained. What is cloud based is the ability to sync the time to the cloud which occurs when the device first connects to the Cloud servers or when Time.sync() is called. :smile:


#5

you win the internet . :smiley: Thanks a lot.


#6

The RTC should be battery backed and hence will keep the time even without cloud connection.
If you are using an Electron VBAT is bridged to 3V3 and as long you’ve got the battery connected the RTC will keep the time.
On a Photon you need to bridge the two pins, or attach a cell battery/big cap to VBAT to keep the time.


Too slow :blush:


#7

you win … the intranet? O.o


#8

actually the next question would be:

is there anyway to just check wether it was the timeout or the wkp pin?


#9

This question was frequently asked before and the answer was: Not really.

You could have a sample and hold circuit and need a quick wake to still see if the SnH did register a button press, but the RTC is the easier although less reliable way.


#10

tahnks, I assumed that was the case. it isn’t that critical to find one or the other right now. but I will add one and text.

you win the dark web :wink:


#11

So i gave the sleep a test with just a 90 seconds sleep

I do a check on the previous time vs the new time. but it does not seem to be stable at all.

STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));


 unsigned long now = 0;
retained unsigned long previousMillis = Time.now();

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

void loop() {
   now=Time.now();
   ubidots.add("now", now);
   ubidots.add("previous", previousMillis);
 ubidots.add("diff",now-previousMillis);
//previousMillis=Time.now();
ubidots.sendAll();
    delay(10000);
    previousMillis=Time.now();
    System.sleep(SLEEP_MODE_DEEP, 90);
    
}

the result as follows.

any thoughts?


#12

I guess because you are using AUTOMATIC mode which only executes your code after the device regained cloud connection, which may take different amounts of time.
You can use SYSTEM_THREAD(ENABLED) to decouple your code from WiFi and cloud reconnection delays, but you need to check for your ubidots code whether you are WiFi.ready() or not.

BTW: For your code you could even use SYSTEM_MODE(SEMI_AUTOMATIC) and WiFi.connect() and not even connect to the Particle cloud to save time and energy.


#13

im using the Particle. so im guessing cell connect would behave the same way?
Also, the device itself wakes up t different times.


#14

Do you mean you are using an Electron?
But yes, cell connection can take even longer, but what I said for WiFi would work the same for Cellular.

Additionally, if you are using the Particle SIM and intend to wake every 23 minutes or less, you may want to consider using

  System.sleep(WKP, RISING, time, SLEEP_NETWORK_STANDBY);

instead of deep sleep. This wouldn’t use a lot more power but saves an aweful lot of data (6K per wake).
(unless you are already usin system v0.6.0-rc.1)


#15

i did mean electron. sorry.

ah that’s a good tip. my wakeup is an hour for test. the way things are going i think i am going to have to rely in checkingthe pin instead of the time.now() difference :confused:


#16

Have you considered SYSTEM_THREAD(ENABLED) and SEMI_AUTOMATIC?

With these your time compare should work just fine.


#17

I am giving look at the documentation right now :slight_smile: I will go for that. now i have to move my stuff home so i can work on the wifi. classic.


#18

so… does this make sense?
void loop {
now=Time.now();
WiFi.on();
WiFi.connect();
waitUntil(WiFi.ready);
waitUntil(Particle.connected);
SendStuff();
.
.
i get the breathing green led… hmm. could not find any guide for when i have to manually do the connections


#19

The basic idea yes, but actually no :wink:

Try instead

void loop {
  now=Time.now();
  // here you'd calculate difference between now and your expected wake time
  // and guess if it was a button or time wake
 
  if (!WiFi.ready())
  {
    //WiFi.on();                      // should not be required unless you called WiFi.off() or use MANUAL mode
    WiFi.connect();
    waitUntil(WiFi.ready);            // you could also use waitFor() if you want a timeout in case of WiFi problems (e.g. router down)
    //waitUntil(Particle.connected);  // will never continue since you don't ever call Particle.connect()
  }
  SendStuff();
  ...
  System.sleep(WKP, RISING, time, SLEEP_NETWORK_STANDBY);
}

Breathing green is OK for WiFi.ready() and ubidots, if you need the Particle cloud, you need to call Particle.connect() and waitUntil(Particle.connected) (instead of WiFi.connect() and WiFi.ready())

Cloud connection implicitly activates WiFi connection if not present.


#20

alright so it seems there is something weird with my electron just on sleep

even on a single loop it doesnt wake back up.

void setup() {

}

void loop() {

            delay(60000);
                       System.sleep(SLEEP_MODE_DEEP,600);

}

Using firmware 0.5.1

Oddly enough on my code with sleep longer than an hour it does seem to work.

any thoughts? only difference i could see is the usb cable is plugged.

edit: im really sorry for nagging you.