NTP-client, time-sync
Posted: Fri Dec 19, 2014 1:15 pm
Hi,
Attached ntp.lua(.txt) can be used to get the current time from a NTP-Server or to keep a table-structure (hour,minute and second) constantly sync'ed (using a timer-function)
To run the 'realtime-daemon ':
will start a timer that:
- uses timer 1
- updates the internal 'clock' every 10 seconds
- and adjusts the clock every 30minutes (using an address from 3.at.pool.ntp.org)
(timeserver - ip can be omitted when 'ntpserver' inside the code is set)
Global TIME.hour TIME.minute and TIME.second will constantly be updated then..
Once started, you can save some memory be setting TIME.run=nil
For a one-time call use:
The function passed to :sync() will be called with a table as argument (T.hour T.minute T.second) once the NTP-server answered
Important: the program currently needs an internal timer (configurable in the source) for 'guarding' the udp-call against memory leakage.
Maybe this can be changed by setting up a single 'connect' and 'receive-handler' on startup but i haven't tried yet
To be honest, the 'full daemon' probably eats too much ram to be combined with 'real' work but perhaps you can tailor it to your needs...
Thomas
Attached ntp.lua(.txt) can be used to get the current time from a NTP-Server or to keep a table-structure (hour,minute and second) constantly sync'ed (using a timer-function)
To run the 'realtime-daemon ':
Code: Select all
TIME=loadfile("ntp.lua")()
TIME:run(1,10,1800,"193.170.62.252")
will start a timer that:
- uses timer 1
- updates the internal 'clock' every 10 seconds
- and adjusts the clock every 30minutes (using an address from 3.at.pool.ntp.org)
(timeserver - ip can be omitted when 'ntpserver' inside the code is set)
Global TIME.hour TIME.minute and TIME.second will constantly be updated then..
Once started, you can save some memory be setting TIME.run=nil
For a one-time call use:
Code: Select all
loadfile("ntp.lua")():sync(function(T) end)
The function passed to :sync() will be called with a table as argument (T.hour T.minute T.second) once the NTP-server answered
Important: the program currently needs an internal timer (configurable in the source) for 'guarding' the udp-call against memory leakage.
Maybe this can be changed by setting up a single 'connect' and 'receive-handler' on startup but i haven't tried yet
To be honest, the 'full daemon' probably eats too much ram to be combined with 'real' work but perhaps you can tailor it to your needs...
Thomas