Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"NTP server not reachable", compromises time-based features #452

Closed
xoseperez opened this issue Jan 21, 2018 · 8 comments
Closed

"NTP server not reachable", compromises time-based features #452

xoseperez opened this issue Jan 21, 2018 · 8 comments

Comments

@xoseperez
Copy link
Owner

Originally reported by: P.B. (Bitbucket: PieBru, GitHub: PieBru)


Hi Xose, sorry for the partial #238 revival, but the problem persists randomly also in 1.12.1 and it may be crytical for time-based features.

When the error appears, usually immediately at boot, it doesn't recover until the next reset. If it depends on an external library a workaround could be force a reset after N consecutive NTP errors.

No benefits from NTP.setInterval(NTP_UPDATE_INTERVAL / 20, NTP_UPDATE_INTERVAL);

Despite the error, the time is initialized. I added this code at the bottom of loop():

#!arduino
    #if DEBUG_SUPPORT
    static unsigned got_year = 0;
    if (got_year < 2018 && year(now()) >= 2018) {
        got_year = year(now()); // Got a sync from some time provider (NTP, RTC, ...)
        DEBUG_MSG_P(PSTR("[MAIN] Time initialized %lu ms after boot, it might be %u-%02u-%02u %02u:%02u:%02u\n")
            , millis(), year(now()), month(now()), day(now())
            , hour(now()), minute(now()), second(now()));
    }
    #endif  // DEBUG_SUPPORT

and this is the OK output:

#!arduino
[MQTT] MQTT brokers found: 0
[MAIN] Time initialized 7659 ms after boot, it might be 2018-01-21 14:54:53
[NTP] Time: 2018-01-21 14:54:53
[MAIN] System OK

but often I get the NTP error (Wemos D1 Mini, default config):

#!arduino
[MQTT] MQTT brokers found: 0
[NTP] Error: NTP server not reachable
[NTP] Time: Not set
[NTP] Error: NTP server not reachable
[MAIN] Time initialized 9226 ms after boot, it might be 2018-01-21 14:52:52
[MAIN] System OK

This NTP error does not recover even after hours of successful Wifi and MQTT connection, so any time-based feature checking ntpConnected() would not work.

I didn't investigate further, as a trivial solution could be a forced reset.

Piero

@xoseperez
Copy link
Owner Author

The thing is that the NTP module has been heavily modified in the current dev branch and meant to be released with next version due to an update in the NTPClientLib library to support timezones with minutes and not only full hours. Germán has completely reworked the library and I'm even using a fork of it with some critical improvements to make it work smoothly with ESPurna.

I'd suggest you to test the dev branch and provide feedback. But I don't think it makes sense to debug this issue over the old implementation.

@AlbertWeterings
Copy link
Contributor

It looks like the "NTP server not reachable" compromises OTA updates. I have numerous devices that fails OTA updates and become corrupted (only upgradable by wired flashing), the NTP module also causes MQTT message delays. For now all my devices that are not time critical I compile without NTP to get them stable.

@xoseperez
Copy link
Owner Author

There are two possible solutions for this:

  • Increase the initial check interval from 15 seconds (current default) to something like 60 seconds.
  • Disable NTP checks when OTA is started

The first option is simply changing this line:

NTP.setInterval(NTP_UPDATE_INTERVAL);

to:

NTP.setInterval(60, NTP_UPDATE_INTERVAL);

Of course this should be done with a setting in general.h etc.

@xoseperez xoseperez added this to the 1.12.4 milestone Feb 5, 2018
@xoseperez xoseperez self-assigned this Feb 5, 2018
@AlbertWeterings
Copy link
Contributor

Hi,

Xose, can you post a list of all libraries that have a relation with NTP i found that all versions you compiled and that are in Releases don't have the issues I see with the once I compiled myself. I have these libraries installed:
NtpClientLib by German Martin Version 2.5.0
Time by Michael Margolis version 1.5.0
JustWiFi by Xose Perez Version 1.1.6

I'm starting to get the idea somehow I compile with a wrong Library

@xoseperez
Copy link
Owner Author

You can check the exact version/revision of the libraries I use to compile the release images in the platformio.ini file:

lib_deps =
ArduinoJson
https://github.com/marvinroger/async-mqtt-client#v0.8.1
Brzo I2C
https://bitbucket.org/xoseperez/debounceevent.git#2.0.1
Embedis
https://github.com/krosk93/espsoftwareserial#a770677
https://github.com/me-no-dev/ESPAsyncTCP#a57560d
https://github.com/me-no-dev/ESPAsyncWebServer#313f337
https://bitbucket.org/xoseperez/fauxmoesp.git#2.4.2
https://bitbucket.org/xoseperez/hlw8012.git#1.1.0
https://github.com/markszabo/IRremoteESP8266#v2.2.0
https://bitbucket.org/xoseperez/justwifi.git#1.1.6
https://github.com/madpilot/mDNSResolver#4cfcda1
https://github.com/xoseperez/my92xx#3.0.0
https://bitbucket.org/xoseperez/nofuss.git#0.2.5
https://github.com/xoseperez/NtpClient.git#b35e249
OneWire
PMS Library
PubSubClient
https://github.com/xoseperez/RemoteSwitch-arduino-library.git
https://github.com/xoseperez/Time

The main difference is that I use a fork of German's library that introduces some improvements to the first sync with the NTP server. Likely to be your problem. I did a PR with the changes and German has already merged it but it's in his dev branch, not master yet.

My fork also removes explicit dependency on the Margolis' Time library to use another fork of mine for that one since there is an issue compiling it in Windows machines.

@AlbertWeterings
Copy link
Contributor

AlbertWeterings commented Feb 17, 2018

I spend a few hours experimenting with the "Error: NTP server not reachable" issue and found setting the NTP_TIMEOUT to 2000 in NtpClientLib.h solved the issue for me. With the setting on its original value of 1500 it was reasonable stable at my own local NTP server but not on any remote.

Placed a issue in gmag11/NtpClient#56

@xoseperez
Copy link
Owner Author

I guess your mileage will vary but 2 seconds is plenty of time... Anyway it would be great if @gmag11 could make this setting configurable.

@xoseperez
Copy link
Owner Author

Closing since there is nothing left to be done atm from the ESPurna point of view. Changes done here are staged for release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants