Using millis and epoch as a RTC?

I built a WiFi solar weather station. I does not have a RTC. I want to time stamp data when the router is down
(power failure). When the router is up, I can poll a server and get epoch (time since 1900). I also keep track of millis (mseconds since the station started).

I want to be able to combine the two (I know what time it is when I polled the server and I know how long it has been since the board booted), so I can be able to figure out what time it is without being able to contact the time server anymore.

I just cant get my head around the calculation I need to do.

Thanks,

-Dave

Maybe something like:

unsigned long epochTimeAtStart;
unsigned long offset ; // the value of millis() at the time epochTimeAtStart was first set.
. . .
Serial.println(  (millis() - offset)/1000UL + epochTimeAtStart) ;  // calculated epoch time

pas365:
I built a WiFi solar weather station. I does not have a RTC. I want to time stamp data when the router is down
(power failure). When the router is up, I can poll a server and get epoch (time since 1900). I also keep track of millis (mseconds since the station started).

I want to be able to combine the two (I know what time it is when I polled the server and I know how long it has been since the board booted), so I can be able to figure out what time it is without being able to contact the time server anymore.

I just cant get my head around the calculation I need to do.

Thanks,

-Dave

You need just one more number for your calculation.
When you get a valid response from the server, remember what the millis() value was at the time that you got that response.

Suppose that, at millis() value 100000, the server informs you that it is exactly seven o’clock.

Then, some time passes. Now you see that the millis() value is 400000. What time is it now? You know that, 300000 milliseconds ago, it was exactly seven o’clock. So now, it is 300000 milliseconds past seven o’clock. There are 1000 milliseconds in one second, and thus 60000 (sixty times 1000) milliseconds in one minute. How many times does 60000 go into 300000 ? It goes in five times, with no remainder, so we now know that it is exactly five minutes past seven.

Does this give you an idea of how to proceed?

Be aware that an Arduino’s millis() timer is not perfect. In my experience, they are accurate to within 1 or 2 minutes per day. I would recommend that you synchronize your Arduino to the time server at least once or twice per day.

By the way, what happens when the weather station itself has a power failure? Does it have a battery backup or something?