Nodemcu-firmware: sntp.sync fails even after Internet connection is established (in auto-repeat mode)

Created on 26 Feb 2018  路  3Comments  路  Source: nodemcu/nodemcu-firmware

Expected behavior

sntp.sync must perform sync successfully after Internet connection is established.

Actual behavior

Here are the specific steps to reproduce:

  1. Disconnect the Internet connection from your hotspot/router
  2. Boot and connect ESP8266 to the router (This is important. ESP8266 must freshly boot and sntp.sync must fail on 1st attempt)
  3. Start sntp.sync in auto-repeat mode
  4. Reconnect the Internet after 2-3 failed attempts of sntp.sync
  5. Actual Behavior: sntp.sync still keeps on failing with error code 4

Note: the pattern of errors: error code 1 (DNS lookup failed) followed by many 4(Timeout, no NTP response received) is necessary to reproduce the problem.

Test code

Providing a Minimal, Complete, and Verifiable example which will reproduce the problem.


NodeMCU custom build by frightanic.com
    branch: master
    commit: 5073c199c01d4d7bbbcd0ae1f761ecc4687f7217
    SSL: false
    modules: file,gpio,net,node,rtctime,sntp,tmr,uart,wifi
 build  built on: 2018-02-26 00:15
 powered by Lua 5.1.4 on SDK 2.1.0(116b762)
lua: cannot open init.lua
> =wifi.setmode(wifi.STATION)==wifi.STATION
true
> =wifi.sta.getip()
nil
> =wifi.sta.config({ssid="<ssid>", pwd="<password>"})
true
> =wifi.sta.connect()
> =wifi.sta.getip()
10.10.10.1  255.255.255.0   10.10.10.254
> sntp.sync(nil, function(sec) print(sec) end, function(err_code, err_desc) print(err_code, err_desc) end, 1)
> 1 0.nodemcu.pool.ntp.org
4   nil
4   nil
4   nil
4   nil

Note: after ESP printed 1, 4, 4; the Internet connection was re-established and sntp.sync kept on failing with same error code.

NodeMCU version

Branch: master
Revision: 5073c199c01d4d7bbbcd0ae1f761ecc4687f7217 (latest)

Hardware

NodeMCU version 2 Amica

bug

Most helpful comment

I'll take a look at this and fix it it properly.

All 3 comments

The autorepeat fails because of the initial DNS lookup failure. The current implementation will not try to recover from this automatically. So it will not retry a failed DNS lookup. After the initial attempt it assumes it correctly got a valid IP address.
There are several workarounds for this:

  • Do not rely on the autorepeat functionality, but implement the retry (DNS lookup if needed) and re-sync with Lua code
  • Avoid using DNS by using a fixed IP address of time server

Or we can have a look to fix this in the firmware, by applying the autorepeat also to the DNS lookup.

I'll take a look at this and fix it it properly.

I have a fix, but it needs more testing.... Hope to get a PR up in the next couple of days.

The current code is in https://github.com/pjsg/nodemcu-firmware/tree/fix-sntp-lookup

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamdyga picture adamdyga  路  4Comments

vsky279 picture vsky279  路  7Comments

HHHartmann picture HHHartmann  路  7Comments

sivix picture sivix  路  4Comments

nwf picture nwf  路  6Comments