Hi ChipBurner, I originally used RTC.date$ and RTV,times with the script, causing it to do all date/time calculations relative to the RTC clock, which could get complicated.
But then I had an epiphany, and decided it was only necessary to refer to the rtc at startup in order to set the local esp time/date, so then all subsequent date/time calculations could be done using the local esp clock.
Later I decided it was unnecessary to use an rtc module on all devices except for one, which I would use as a dedicated little esp timeserver from which all other devices would sync their local esp date/time from when they start up, and also re-sync their time when any of the other devices start up.
The great advantage of having the single time server was that whenever it supplied its date/time to all other listening devices it could automatically include any required offset for drift or summertime changes.
A short simple bit of code allows the timeserver (or any device) to check if it has its own local rtc clock or not.
Code: [Local Link Removed for Guests]
i2c.setup 8,9
pause 10
rtc=0: i2c.begin 104
if i2c.end=0 then rtc=1
All non rtc devices send a utp timesync request at startup to the timeserver which sends back its current date and time, complete with any offsets or adjustments.
Code: [Local Link Removed for Guests]
send "TimeServer TimeSync" 'request an EasyNet TimerServer to respond by syncing out its time
The TimeServer has onscreen buttons for things like syncing to internet time, sending its timesync to all devices, setting its rtc clock from esp time, or setting esp time from the rtc time ... so it allows easily keeping on top of the time situation for all devices.
TimeServer.jpg
Code: [Local Link Removed for Guests]
AddTimeOffset:
t$ = time$
d$ = date$
day = val(word$(d$,1,"/"))
month = val(word$(d$,2,"/"))
year = val(word$(d$,3,"/"))
hour = val(word$(t$,1,":"))
min = val(word$(t$,2,":"))
sec = val(word$(t$,3,":"))
'wlog str$(day)+"-"+str$(month)+"-"+str$(year)+" "+str$(hour)+"."+str$(min)+"."+str$(sec)
min=min+8
if min>59 then min=min-60: hour=hour+1
SETTIME year, month, day, hour, min, sec
return
SyncTime:
send "All TimeSync " + word$(date$,3,"/")+","+word$(date$,2,"/")+","+word$(date$,1,"/")+","+word$(time$,1,":")+","+word$(time$,2,":")+","+word$(time$,3,":")
return
TimeSyncSent:
data$="x"
TimeSync:
if data$="" then
if timeserver=1 then wlog "timesync request from: " + udp.remote$: gosub SyncTime
elseif data$="x" then
data$=""
return
else
year=val(word$(data$,1,","))
month=val(word$(data$,2,","))
day=val(word$(data$,3,","))
hour=val(word$(data$,4,","))
mins=val(word$(data$,5,","))
secs=val(word$(data$,6,","))
SetTime year,month,day,hour,mins,secs
if rtc=1 then rtc.SetTime year,month,day,hour,mins,secs
endif
data$=""
return
netsync:
'Snippet to sync the date of the module using the date coming from the browser
'the Output Page must be open before running the script (otherwise there will be no effects)
A$ = |{|
A$ = A$ + |const d = new Date();|
A$ = A$ + |const year = d.getFullYear() % 100;|
A$ = A$ + |const month = d.getMonth() + 1;| ' Months are zero-based, so add 1|
A$ = A$ + |const day = d.getDate();|
A$ = A$ + |const hours = d.getHours();|
A$ = A$ + |const minutes = d.getMinutes();|
A$ = A$ + |const seconds = d.getSeconds();|
A$ = A$ + |const Settime = `SETTIME ${year},${month},${day},${hours},${minutes},${seconds}`;|
A$ = A$ + |connection.send("cmd:immediate" + Settime);|
A$ = A$ + |}|
jscall a$
return
rtc2esp:
'Used to set ESP internal timekeeper from RTC module time
t$ = rtc.time$
d$ = rtc.date$
'wlog "rtc = " + t$ + " " + d$
day = val(word$(d$,1,"/"))
month = val(word$(d$,2,"/"))
year = val(word$(d$,3,"/"))
hour = val(word$(t$,1,":"))
min = val(word$(t$,2,":"))
sec = val(word$(t$,3,":"))
'wlog str$(day)+"-"+str$(month)+"-"+str$(year)+" "+str$(hour)+"."+str$(min)+"."+str$(sec)
SETTIME year, month, day, hour, min, sec
refresh
return
esp2rtc:
'Used to set RTC module time from ESP internal timekeeper
t$ = time$
d$ = date$
rtctime$=t$
'wlog "esp = " + t$ + " " + d$
day = val(word$(d$,1,"/"))
month = val(word$(d$,2,"/"))
year = val(word$(d$,3,"/"))
hour = val(word$(t$,1,":"))
min = val(word$(t$,2,":"))
sec = val(word$(t$,3,":"))
'wlog str$(day)+"-"+str$(month)+"-"+str$(year)+" "+str$(hour)+"."+str$(min)+"."+str$(sec)
RTC.SETTIME year, month, day, hour, min, sec
refresh
return
I'm not suggesting you need a timeserver, but I am suggesting that you may be able to use some of these capabilities to resolve your own issues.
You do not have the required permissions to view the files attached to this post.