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]