Millis() or another time function while in sleep

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?


That would the RTC via

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

@ouchboy, 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:


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

1 Like

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:


you win … the intranet? O.o

actually the next question would be:

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

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.

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:

1 Like

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.


 unsigned long now = 0;
retained unsigned long previousMillis =;

void setup() {

void loop() {;
   ubidots.add("now", now);
   ubidots.add("previous", previousMillis);
    System.sleep(SLEEP_MODE_DEEP, 90);

the result as follows.

any thoughts?

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.

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

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


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)

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 difference :confused:


With these your time compare should work just fine.

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.

so… does this make sense?
void loop {;
i get the breathing green led… hmm. could not find any guide for when i have to manually do the connections

The basic idea yes, but actually no :wink:

Try instead

void loop {;
  // 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 or use MANUAL mode
    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()

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.

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() {



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.