ropg / ezTime

ezTime — pronounced "Easy Time" — is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, user events, millisecond precision and more.
MIT License
327 stars 92 forks source link

Timezones example - timeouts #20

Closed BugerDread closed 5 years ago

BugerDread commented 5 years ago

Hallo Ropg, thank you for ezTime library!

Right now Im testing it on ESP8266 but I found an issue with Timezones example.

If I change this example to be abe to run on ESP (WiFi.h -> ESP8266WiFi.h, set SSID and PASS), I get following output (debug enabled):

ezTime debug level set to INFO
Waiting for WiFi ... Querying pool.ntp.org ... ERROR: No network
connected
Waiting for time sync
Querying pool.ntp.org ... success (round trip 22 ms)
Received time: Thursday, 20-Dec-18 11:41:07.160 UTC
Time is in sync

UTC:             Thursday, 20-Dec-2018 11:41:07 UTC
Timezone lookup for: Pacific/Auckland ... (round-trip 43 ms)  success.
  Olsen: Pacific/Auckland
  Posix: NZST-12NZDT,M9.5.0,M4.1.0/3
New Zealand:     Friday, 21-Dec-2018 00:41:07 NZDT
Timezone lookup for: de ... ERROR: Timeout
Germany:         Friday, 21-Dec-2018 00:41:09 NZDT
Local (GeoIP):   Timezone lookup for: GeoIP ... ERROR: Timeout
Timeout

I always get New Zealand time correctly, Germany (de) timezone results in timeout in about 50% of tries, I never get GeoIP time - always timeout.

I think this is because of DDOS protection on the server you mentioned in readme: "will only respond to the same IP-number once every three seconds to prevent being used in dDoS attacks", but there is no delay in the example and we are calling setLocation 3 times in a row (from the same IP ofc).

Also if the myTZ.setLocation(F("de")) results in timeout, it shows New Zealand time for germany (as in output above), but this is because myTZ.setLocation(F("de")) is not checked if completed successfully as in "GeoIP part". The myTZ.setLocation(F("Pacific/Auckland")) is also not checked if completed successfully, but from my point of view this is not a problem, it demonstrate that you can but you are not required to check setLocation result in your sketch.

It works as expected when I add two delays this way (which confirms its 99% because of the DDoS protection):

#include <ezTime.h>
#include <ESP8266WiFi.h>

void setup() {

    Serial.begin(115200);
    while (!Serial) { ; }       // wait for Serial port to connect. Needed for native USB port only
        Serial.print(F("\nGood morning :-)"));
    WiFi.begin("your-ssid", "your-password");

    // Uncomment the line below to see what it does behind the scenes
    //setDebug(INFO);

    waitForSync();

    Serial.println();
    Serial.println("UTC:             " + UTC.dateTime());

    Timezone myTZ;

    // Provide official timezone names
    // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
    myTZ.setLocation(F("Pacific/Auckland"));
    Serial.print(F("New Zealand:     "));
    Serial.println(myTZ.dateTime());

        // wait a little bit to not trigger DDoS protection on server - see readme
        delay(5000);

    // Or country codes for countries that do not span multiple timezones
    myTZ.setLocation(F("de"));
    Serial.print(F("Germany:         "));
    Serial.println(myTZ.dateTime());    

        // wait a little bit to not trigger DDoS protection on server - see readme
        delay(5000);

    // See if local time can be obtained (does not work in countries that span multiple timezones)
    Serial.print(F("Local (GeoIP):   "));
    if (myTZ.setLocation()) {
        Serial.println(myTZ.dateTime());
    } else {
        Serial.println(errorString());
    }
}

void loop() {
    events();
}

I think the same apply to "EthernetShield" example, as it does the same (calling myTZ.setLocation() 3 times without delay), but I have no HW to test it.

ropg commented 5 years ago

Thank you for finding this, and confirming what it is.

You're absolutely right, hadn't occurred to me that my examples make multiple requests. The best solution would be to allow a couple of rapid requests (per hour) and then throttle, but maybe I'll just put in the delays in the examples... I'll have a think.

ropg commented 5 years ago

I just released 0.7.9 which just has the delays added to the two examples and a little explanatory text at setLocation. Maybe a more comprehensive overhaul of the server will make this unnecessary, but that's further in the future. Thanks again...

BugerDread commented 5 years ago

Nice to hear that :-)

I just finished very simple NTP clock using your library, see https://github.com/BugerDread/esp8266-ezTIME-wifi-clock

Thanks again for this library!

ropg commented 5 years ago

Cool!!! I will make sure I put a link with picture to this in the README when I get to it...

BugerDread commented 5 years ago

Added one more picture, feel free to use them if you want.

ropg commented 5 years ago

Just added your project to the README, in new chapter "Inspiration".