forked from wnasich/nodemcu-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntp_sync.lua
41 lines (36 loc) · 949 Bytes
/
ntp_sync.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
print('ntp_sync ...')
local hourCount = 0
local sntpServerIp
function startNtpSync()
if (hourCount == 0 and appStatus.wifiConnected) then
net.dns.resolve(cfg.sntpServerName, function(sk, ip)
if (ip) then
print('Resolved ' .. cfg.sntpServerName .. ' to ' .. ip)
sntpServerIp = ip
else
print('Resolve ' .. cfg.sntpServerName .. ' fail!')
print('Fallback to ' .. cfg.sntpServerIp)
sntpServerIp = cfg.sntpServerIp
end
doNtpSync()
end)
end
hourCount = hourCount + 1
if (hourCount >= cfg.sntpRefresh) then
hourCount = 0
end
end
function doNtpSync()
sntp.sync(
sntpServerIp,
function(sec,usec,server)
print('sntp sync success', sec, usec, server)
end,
function()
print('sntp sync failed!')
end
)
end
tmr.register(timerAllocation.syncSntp, 3600000, tmr.ALARM_AUTO, startNtpSync)
tmr.start(timerAllocation.syncSntp)
startNtpSync()