Quick question: does millis() reset when flashing a firmware update?
I have a routine that is supposed to execute every 24 hours under certain circumstances but it doesn't. I do a lot of firmware tweaks and suspect that that may be the cause. If it does reset with every flash, would I be correct that using System.millis() would solve the problem?
Also, the documentation suggests that System.millis() doesn't reset even after a power outage and subequent reboot; is that correct?
unsigned long lastTankUpdate = millis();
if ( millis() - lastTankUpdate > 86400000){ // If it's been more than 24 hours since last message from tank
That code will work will not work (see below). The millis counter resets on every reset, including reset button, System.reset, OTA reset, and wake from hibernate sleep.
Confused Rick. The sample code I included in my post is what I have in production but which does not execute. Logic suggests that it doesn't execute because millis() gets reset somehow before the 24-hour period expires. Nowhere in my code do I reset the 'lastTankUpdate' variable except in the aforementuoned routine that never executes. So the implication is that my frequent flashing of modified code causes millis() to reset.
When you say "The millis counter resets on every reset...", are you saying that flashing new code qualifies as a 'reset'. If so, that's clearly my issue.
It's not yet clear to me that System.reset behaves differently in that regard.
Sorry, I misread your initial post. That code will not work, because there are a large number of things that reset millis. You should use the actual Unix date from Time.now().
Make sure that the time has been set using Time.isValid() before using it. It will be set a few seconds after connecting to the cloud.
Time.now() is in milliseconds, right? So this is the modification.
if ( Time.now() - lastTankUpdate > 86400000){ // If it's been more than 24 hours since last message from tank
Where does the Time.isValid() go, in setup()?
Time.now() is in seconds since January 1, 1970, like Unix.
It's not in milliseconds like millis() or Javascript getTime().
When the system first boots the time won't be valid because the RTC won't be set yet. Before you rely on it, check Time.isValid(). You usually do this before you check to see if it's time to do something.
Got it. But why are you working on a Sunday? Glad you are though!