Millis() or another time function while in sleep

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?

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

  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)

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:

Have you considered SYSTEM_THREAD(ENABLED) and SEMI_AUTOMATIC?

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

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.

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.

There was a bugfix for short sleep periodes.
Try upgrading (maybe 0.6.0-rc.1) and see if that bug bit you too.

thanks. any chance to do it OTA?

I can’t find the guide. and i just messed up my usb connector on the elctron.

There you can find the instructions and the binaries to flash

These were the respective issues
https://github.com/spark/firmware/issues/1043
https://github.com/spark/firmware/issues/1075

you saved my butt yet again.

Going to bump this thread just in case something new has come up.

I need to measure a regular interval in milliseconds (~1400ms) and if a pulse is missed then the electron should send a publish. But if time.now only gives me seconds then trying to sleep would be useless? I can no longer measure the pulse interval correctly… Any suggestions?

If you need millisecond resolution, you could use a millis() timer or a software timer, but it would be helpful if you could give us more detail about what you’re trying to do. What have you tried so far, and what are the problems with your current approach?

Thanks for the fast response Rick, but as mentioned in the first post millis() and software timers dont carryover during sleep. At least as far as i know, (I don’t have any electrons to verify right now).

I have a regular interval pulse coming in on the WKP pin roughly every 1400ms but this can vary up to 500ms.

Right now I keep a running average of time between pulses and wait for an out of range event (+1/2 duration average) before triggering an alarm. So everything works as it should right now but I want to reduce power with sleep.

Ok, I think I get what you’re trying to do. Does the Electron need to wake up every 1400 milliseconds, or only when you exceed your missing pulse time? If the latter, I think you might need to use an external watchdog timer (with a timeout period of ~2100 ms), that the pulse would tickle every 1400 milliseconds. If a pulse doesn’t come in within that timeout period, the watchdog would wake your Electron. I assume you want to use deep sleep. Is that the case?

Hello @ScruffR

If i am using a photon microcontroller and i am using the stop mode described here. I have been putting my photon in stop mode for 1 second and found that it does stop and starts again after what appears to be 1 second without bridging the 3V3 to Vbat. Although it appears to be working, is it necessary to bridge 3v3 to Vbat so that i dont encounter any timing errors in the future?

As long your keep the device powered (via USB, Vin or 3v3) you don’t strictly have to.

1 Like