This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

NTP Server
#17
Create a scheduled script that runs once per day during the night when there are no other scheduled scripts or tasks.
The supplied time is in UTC so make sure that LM has correct timezone set for the automatic conversion to work.
For testing purposes you can comment out os.sleep. It is needed so the script is not triggered again if the time is shifted.

Code:
os.sleep(30)

require('serial')
port = serial.open('/dev/ttyACM0')
if not port then
  log('port not found')
  return
end
port:flush()

function settime(line)
  local parts = line:split(',')
  if parts[1] ~= '$GPRMC' then
    return
  end

  local hour, min, sec = string.match(parts[2] or '', '^(%d%d)(%d%d)(%d%d)%.')
  local day, month, year = string.match(parts[10] or '', '^(%d%d)(%d%d)(%d%d)$')

  if not hour or not day then
    return
  end

  local cmd = string.format('date -u -s "20%s-%s-%s %s:%s:%s"; hwclock -w',
    year, month, day, hour, min, sec)

  os.execute(cmd)
  return true
end

buf = {}
lines = 0

while true do
  char = port:read(1, 1)

  if char then
    if char == '\r' or char == '\n' then
      if #buf > 0 then
        line = table.concat(buf)
        lines = lines + 1

        if settime(line) then
          log('time updated')
          break
        elseif lines > 20 then
          log('could not update time')
          break
        end

        buf = {}
      end
    else
      buf[ #buf + 1 ] = char
    end
  end
end
Reply


Messages In This Thread
NTP Server - by Diggerz - 28.10.2020, 01:15
RE: NTP Server - by Daniel - 28.10.2020, 08:11
RE: NTP Server - by admin - 28.10.2020, 08:20
RE: NTP Server - by ceddix - 17.09.2025, 17:55
RE: NTP Server - by Diggerz - 30.10.2020, 02:07
RE: NTP Server - by fabiorusco - 17.12.2020, 11:57
RE: NTP Server - by Erwin van der Zwart - 19.12.2020, 06:03
RE: NTP Server - by Frank68 - 07.08.2025, 12:03
RE: NTP Server - by fabiorusco - 19.12.2020, 12:49
RE: NTP Server - by admin - 07.08.2025, 12:05
RE: NTP Server - by admin - 23.09.2025, 09:59
RE: NTP Server - by ceddix - 24.09.2025, 09:13
RE: NTP Server - by admin - 24.09.2025, 09:42
RE: NTP Server - by ceddix - 25.09.2025, 12:05
RE: NTP Server - by admin - 25.09.2025, 12:09
RE: NTP Server - by ceddix - 01.10.2025, 11:43
RE: NTP Server - by admin - 01.10.2025, 12:08
RE: NTP Server - by ceddix - 01.10.2025, 12:22

Forum Jump: