我正在使用ESP8266和NodeMCU和Lua进行一个小项目。我怀疑我发现了一个错误,但是由于我是Lua的新手(和另外两个人一样!),所以我希望在确认我是否正确或是否错过了某些东西方面有所帮助(更多)可能!)。
NodeMCU固件包含一个内置的SNTP客户端模块,该模块将同步时间更新为系统时钟(rtctime模块)。 NTP同步失败时(或之前),似乎会调用成功回调函数。例如,如果未连接wifi,或者有时在启动后首次尝试同步(连接wifi),就会发生这种情况。根据doco,如果当前时间不可用,则rtctime.get()返回零;否则,返回0。这是我得到的结果,进一步表明NTP同步未成功。我无法弄清楚为什么现在要在失败函数之前(而不是在失败函数之前)调用成功函数(如我所期望的那样)。
我指的sntp模块在这里-不幸的是,C源代码让我有些头疼:https://nodemcu.readthedocs.io/en/master/en/modules/sntp/
我的(最小)代码:
-- Define callback function for ntp sync success
function ntpSyncSuccess (sec, usec, server, info)
print('SNTP time sync successful!')
print("rtctime.get() returns: ", rtctime.get())
end
-- Configure and start NTP time sync with auto repeat enabled
sntp.sync("0.au.pool.ntp.org",
ntpSyncSuccess(sec, usec, server, info), --success callback
function() -- error callback
print('SNTP time sync failed!')
end,
1 -- enable autorepeat (SNTP sync every 1000 seconds (~17 min))
)
我启动设备并运行代码时的串行输出结果(请注意最后两行和第三行):
NodeMCU custom build by frightanic.com
branch: master
commit: 11592951b90707cdcb6d751876170bf4da82850d
SSL: false
modules: cron,file,gpio,i2c,net,node,rotary,rtctime,sntp,struct,tmr,uart,wifi
build created on 2019-01-16 03:11
powered by Lua 5.1.4 on SDK 2.2.1(6ab97e9)
lua: cannot open init.lua
> print(uart.setup(0, 115200, 8, 0, 1, 1 ))
115200
> dofile("ntpTest.lua")
SNTP time sync successful!
rtctime.get() returns: 0 0 0
> SNTP time sync failed!
答案 0 :(得分:1)
只是为了让我们可以在此处“关闭”此Q(一旦您接受了答案)。它必须是函数引用而不是函数调用。
sntp.sync("0.au.pool.ntp.org",
ntpSyncSuccess, -- no (), no parameters
function() -- error callback