Author Topic: LowPower.powerDown and millis()  (Read 394 times)

d00m178

  • Jr. Member
  • **
  • Posts: 76
LowPower.powerDown and millis()
« on: November 04, 2019, 10:56:16 AM »
Hello

I want to check the uptime of Moteino (then I will send it via LoRa)

so I have in code millis() function

Code: [Select]
      uptime = uptime+(millis()/1000);
      srv_data.uptime = uptime; //server uptime in _seconds_

then I send this srv_data via radio.

it works only if Moteino doesn't use sleep mode.
If Moteino has a sleep mode via
Code: [Select]
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

I assume that it sleep 8 seconds
1 time per cycle.
But then I receive wrong uptime variable - it a far less that real time from the start of Moteino.
It seems that I need take into account the time when Moteino is sleeping.
So I decided just add +8sec to uptime but this lead to another issue - uptime start to grow very fast and after 5 real minutes it can show about 10 min!
Please advice..

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6279
  • Country: us
    • LowPowerLab
Re: LowPower.powerDown and millis()
« Reply #1 on: November 04, 2019, 12:07:08 PM »
I advise not to assume anything, instead take measurements.
You know that while it sleeps, the timers are OFF. It then takes time to start everything up and then go back to sleep. That is overhead. So it's not going to be exactly 8 seconds.
Also, you have to ensure that nothing else is waking it, except the watchdog.
You can look around at some of the example sketches of how the "8 second sleep time" is accounted for. An example here for the mailbox notifier which reports "time ago" but I don't expect high accuracy from it.
Also you need to be ready to accept that sometimes if the mote is awaken in the middle of the 8 second cycle then that will depreciate accuracy if that's what you're looking for.
In general you should not try to keep time using the atmega timers, they are inaccurate even if you don't sleep. Even the best crystals have drift with temperature and age. It can be quite bad.
Instead, keep accurate time at a central node or a gateway, using NTP, or if you really have to an RTC.

d00m178

  • Jr. Member
  • **
  • Posts: 76
Re: LowPower.powerDown and millis()
« Reply #2 on: November 05, 2019, 11:07:48 AM »
thank you for advice.
Im not going to keep the time on the node.
Just want to know the uptime of this node - how many seconds it is in powered on state.
but seems even if I add some "drift" to the calculated uptime it anyway start to hurry or late.
and after hours or day it will definitely show wrong uptime data.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6279
  • Country: us
    • LowPowerLab
Re: LowPower.powerDown and millis()
« Reply #3 on: November 05, 2019, 01:02:30 PM »
That's if you consider uptime to include sleep time. Are you "uptime" when you sleep?

Instead what I do, to keep uptime, I send a simple "START" message to my gateway, which logs it with a timestamp. So I know when it came online, or when it last restarted. Simple, precise, no coding headaches and assumptions.