Connecting NodeMCU to a NTP server
Ignore the IP suggested in example.
Search google for “NTP server”.
Being Australian I am using one from the pool listed at:
Code therefore becomes:
sntp.sync("0.au.pool.ntp.org", function(sec,usec,server) print('setting time to:', sec, usec, "from: " .. server) rtctime.set(sec, usec) sec, usec = rtctime.get() print('time set to: ', sec, usec) end, function() print('failed!') end )
The difference between times that you will see, when running this gem, is in and around 7 milliseconds which is the cost from function calls is all.
The idea would be to use a timer, set to max time and auto mode to run this occasionally to keep the time on the node synced with your local world. That is:
tmr.register(6, 6870947, tmr.ALARM_AUTO, sntp.sync(...)) -- see above code
You might like to set a flag if the sync fails, or publish a mqtt alarm, or something more useful as the print will be lost on the world since we are deploying nodes in the real-world sans terminals. Although consider loss of sync might also be loss of wifi access and so likely loss of communications with your mqtt server – so devilishly complicated this will all be.
This entry was posted on July 4, 2016 at 5:03 pm and is filed under Embedded, Lua, NodeMCU. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.