Getting NTP time from servers

I’m trying to get the NTP time from the internet so I can synchronize activity on multiple devices. I found this code, but all I get is a timeout. Sometimes connection refused.

Please, can anyone help? The ability to have timed, synced activity on multiple devices will be amazing!!!

[code]

function ntp()

local socket = require (“socket”)

–local host = “0.north-america.pool.ntp.org
local host = “us.pool.ntp.org
local port = 13
local target = {10,13,16,19,7,22}
local date = “”
local timeadjust = 8*60*60 – CA or -0800 UTC (8 hours)
local timeserver, err

local timeserver,err = socket.connect(host,port)
if err then
print("Could not connect "…err)
return err
end
_,err = timeserver:send(“anything\n”)
if err then
print("Error in sending "…err)
return err
end

– KMW: need to rx a line, otherwise error.
_,_ = timeserver:receive(’*l’)

timeserver:settimeout(0.10)

print (“LINE”, line)

while 1 do
line,err = timeserver:receive(’*l’)
if err then
print("Error in receiving - "…err)
return err
end
if string.find(line,“UTC”) then
for i = 1, 6 do
if i == 5 then date = date … “20” end
if i == 6 then date = date … “.” end
date = date … string.sub(line,target[i],target[i]+1)
end
os.execute("date -u "…date)
return err
end
end

end
[/code] [import]uid: 37366 topic_id: 23843 reply_id: 323843[/import]

Update!

This is working (on my desktop Corona simulator)! However, sometimes it hangs, waiting, so I’m trying now to get the timeout to work faster. Not sure what the issues are.

It returns the date-time string, e.g. "56011 12-03-25 20:53:46 50 0 0 476.9 UTC(NIST) * ", ready for parsing.

These hosts sometimes work, sometimes time-out:

207.200.81.113
0.north-america.pool.ntp.org
time.nist.gov
nist1.datum.com
time-a.timefreq.bldrdoc.gov
utcnist.colorado.edu
nist1.aol-ca.symmetricom.com

function ntp(host)  
  
 local socket = require ("socket")  
  
 host = host or "us.pool.ntp.org"  
 local port = 13  
 local target = {10,13,16,19,7,22}  
 local date = ""  
 local timeadjust = 8\*60\*60 -- CA or -0800 UTC (8 hours)  
 local timeserver, err  
  
  
  
 local timeserver,err = socket.connect(host,port)  
 if err then  
 print("Could not connect: "..err)  
 return nil, err  
 end  
 \_,err = timeserver:send("\n")  
 if err then  
 print("Error in sending: "..err)  
 return nil, err  
 end  
  
 -- KMW: need to rx a line, otherwise error.  
 \_,\_ = timeserver:receive('\*l')  
  
 timeserver:settimeout(0.10, "t")  
  
  
 while 1 do  
 line,err = timeserver:receive('\*l')  
  
 print ("NTP RESULT:", line)  
  
 if err then  
 print("Error in receiving - "..err)  
 return nil, err  
 end  
 if string.find(line,"UTC") then  
  
 if (err) then  
 return nil, err  
 else  
 return line, nil  
 end  
  
 end  
 end  
  
end  

HERE is a slightly different version that successfully uses the timeout setting so the function does not hang! Woohoo.

[code]

function ntp(host)

local socket = require (“socket”)

–local host = “0.north-america.pool.ntp.org
host = host or “us.pool.ntp.org
local port = 13
local target = {10,13,16,19,7,22}
local date = “”
local timeadjust = 8*60*60 – CA or -0800 UTC (8 hours)
local timeserver, err

local timeserver,err = socket.tcp()
if err then
print("bind: could not make socket: "…err)
return nil, err
end

timeserver:settimeout(0.10, “t”)

–print ("Socket created for ",host)

timeserver:connect(host,port)
timeserver:settimeout(0.10, “t”)

–print ("Connect attempted to ",host)

if err then
print("Could not connect: "…err)
return nil, err
end

_,err = timeserver:send("\n")

–print ("Send blank line to ",host)

if err then
print("Error in sending: "…err)
return nil, err
end

– KMW: need to rx a line, otherwise error.
_,_ = timeserver:receive(’*l’)

line,err = timeserver:receive(’*l’)

–print (“NTP RESULT:”, line)

if err then
print("Error in receiving - "…err)
return nil, err
end

if string.find(line,“UTC”) then
if (err) then
return nil, err
else
return line, nil
end
end
return nil,“Bad result from server:”…line
end
[/code] [import]uid: 37366 topic_id: 23843 reply_id: 96137[/import]

I too am in dire need of this.  Mimetic, would you be interested in discussing this outside of the fourms?  Feel free to contact me at loughrank at gmail dot com.

I too am in dire need of this.  Mimetic, would you be interested in discussing this outside of the fourms?  Feel free to contact me at loughrank at gmail dot com.

Hello everybody,

did you find out a workaround to this topic ?

I need to fix this same exact issue.

Can you help ?

Stefat

Hello everybody,

did you find out a workaround to this topic ?

I need to fix this same exact issue.

Can you help ?

Stefat