Setting RTC From NTP


Author Message
Andrew_G
Guru

Joined: 18/10/2016
Location: Australia
Posts: 883
Posted: 03:30am 02 Jan 2024      

Hi All and belated seasons greetings.

I'm fiddling with getting the Date/Time from NTP, having retrieved the Daylight Saving status from OpenWeather.
NTP automatically updates DATE$ & TIME$ but, as far as I can see, one must then update the RTC by say :
RTC SETTIME MID$(Date$,7,4)+","+ MID$(Date$,4,2)+","+MID$(Date$,1,2)+"," +MID$(Time$,1,2)+"," +MID$(Time$,4,2)+"," +MID$(Time$,7,2).

Is there sense in having a command like "RTC SETTIME NOW". It would be up to the user to only use this before the internal clock drifted (or have I lost the plot again?).

Cheers,

Andrew
Edited 2024-01-02 13:31 by Andrew_G

phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3319
Posted: 04:43am 02 Jan 2024      

There is probably a better way , use Val(Mid$() to extract each value from DateTime$(now) and insert them in RTC SETTIME

TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6543
Posted: 05:20am 02 Jan 2024      

I don't have a WEBmite with an external RTC to test but this should do it:
RTC SETTIME LEFT$(DATETIME$(NOW),6)+RIGHT$(DATETIME$(NOW),9)



Jim

phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3319
Posted: 05:32am 02 Jan 2024      

> RTC SETTIME LEFT$(DATETIME$(NOW),6)+RIGHT$(DATETIME$(NOW),9)
Error : Date/time format
>

This might do it.
sub RTCSet
 local integer y,m,d,h,min,s
 y = val(mid$(datetime$(now),7,4))
 m = val(mid$(datetime$(now),4,2))
 d = val(mid$(datetime$(now),1,2))
 h = val(mid$(datetime$(now),12,2))
 min = val(mid$(datetime$(now),15,2))
 s = val(mid$(datetime$(now),18,2))
 RTC SETTIME y,m,d,h,min,s
end sub


Edit
Repaired a dumb mistake, had "m" twice
Edited 2024-01-02 15:57 by phil99

TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6543
Posted: 06:33am 02 Jan 2024      

I can't count!

try
rtc settime LEFT$(DATETIME$(NOW),6)+RIGHT$(DATETIME$(NOW),11)

phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3319
Posted: 06:52am 02 Jan 2024      

Yes, that is much easier.
When I searched the manual the RTC chapter made no mention of using a string, just coma separated values.
Though it is in the Command section, next revision perhaps?

Edit
This also works.
rtc settime date$+" "+time$

The above uses a 4 digit year without a problem, the Command description only shows 2 as in Jim's example.
Edited 2024-01-02 17:16 by phil99

TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6543
Posted: 07:41am 02 Jan 2024      

If it is happy with the 4 digit year
RTC SETTIME DATETIME$(NOW)

Andrew_G
Guru

Joined: 18/10/2016
Location: Australia
Posts: 883
Posted: 07:10pm 02 Jan 2024      

Thanks Jim and Phil.
(I too had assumed that it wouldn't accept a 4 digit year)
I'm thankful on two counts, one that MMBasic had the feature and two that you found it.

Cheers,

Andrew

phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3319
Posted: 02:49am 03 Jan 2024      

Setting RTC From GPS
A little off topic but may be useful if a GPS module is configured.
> If gps(valid) then rtc settime gps(date)+" "+gps(time)
> ? time$, gps(time)
13:43:19        13:43:19
>


Edit
For better synchronization.
If gps(valid) then
 t$=gps(time)
 do while t$=gps(time)
 loop
 rtc settime gps(date)+" "+gps(time)
endif

Edited 2024-01-03 13:26 by phil99

Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5991
Posted: 11:10am 03 Jan 2024      

This should be in the user manual.
RTC SETTIME DATETIME$(NOW)