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:


   print('setting time to:', sec, usec, "from: " .. server)
   rtctime.set(sec, usec)
   sec, usec = rtctime.get()
   print('time set to: ', sec, usec)


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.



3 Responses to “Connecting NodeMCU to a NTP server”

  1. OK, I am late to this party, but after (and still) pulling a lot of my hair out, I think I can add a little spice to this.

    First, and the big one. Call this out of wifi.sta.eventMonReg like this.

    wifi.sta.eventMonReg(wifi.STA_GOTIP, function()
    sntp.sync(“″, — UR fav NTP server, Dua
    function(sec, usec, server, info)

    The rest of your program here….

    function(x,y) — Note that this is the failed function for sntp.sync
    print(‘failed! ‘..x..” “..y)
    1 — This makes sntp.sync run once in awhile on it’s own.

    If you DO NOT run it out of wifi.sta.eventMonReg it will start OK sometimes and not start others. Remember that wifi.sta.config will fall through before the network is started, so even if you put sntp.sync after it, that will not guarantee that the network will be up before sntp.sync is called. Using wifi.sta.eventMonReg and putting the sntp.sync after it will guarantee the network is up.

    If you keep getting time stamps from the 70’s, sntp.sync is not connecting.

    I have found very little info on the error codes that sntp.sync is supposed to put in it’s error function, but I do try and trap them and print them (x and y).

    I have found that using cron, the clock seems to be all over the place. I am guessing that whatever the 8266 uses for on board time keeping is not real accurate and sntp makes big changes from update to update.

    As an aside, I am wondering if doing full erases and/or playing with different firmwares as not overwritten an on board calibration register for the on board clock.

    It is interesting that this article is talking about 7ms differences. I am seeing seconds of drift between sntp updates.

    • Cheers. Good info! Thanks!

      The 7ms may be due to small sample size of my data so if I run it longer it may, indeed, drift. So, let’s say my experimental method was less than more.

  2. More info. Urg… Don’t put the rest f your program within the event monitor function as the event monitor gets called every time sntp.sync gets called. You may want to have it print that it got called but that is about it. Put the rest of your code outside of that. Just remember that you may not have your time synced right away.

    Also, the failure function will crash, as far as I can tell, even though they say sntp.snc should return two things on failure, apparently it does not, and you will get an error and crash there.

    I am not sure how tight my time is synced, though it can not be off by too much as with the auto re-sync it runs about 8 times an hour.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: