SensorsIot / NTPtimeESP

Small library for ESP8266 which returns the actual time adjusted by time zone and summer time
232 stars 65 forks source link

Always fails on my first run #21

Closed viniciusov closed 5 years ago

viniciusov commented 6 years ago

Hello,

First of all, I would like to congratulate the author for this nice work. The library seems so easy to use and it has been so useful to me.

But I'm experience a little issue here. I'm using a ESP8266 board and a code very similar to your example but when I turn on the chip e run the code it ALWAYS fails on the first attempt to get a valid time. (More especifically, 'dateTime.valid' is not valid). After that, the library seems to work correctly and the code can get a valid time.

Can you help me with this issue?

kolyshkin commented 6 years ago

I believe it happens because the library is asynchronous, i.e. first time you call it to initiate a request and as it takes some time to receive a reply, when the function returns there's no reply received yet, so you have to wait some time and repeat. This is why it says you should check if dateTime.valid is true.

laurentopia commented 6 years ago

while (!dateTime.valid) {} will wait forever, so enphasis on repeat (call getNTPtime each time you test .valid)

viniciusov commented 6 years ago

@laurentopia , you should call NTPch.getNTPtime() inside your while loop. Otherwise it will never get another time and dateTime.valid will continue with the same state (False) forever.