I do some tests using NTPClient with esp12F module so if the wifi available & internet cut for some reason so the code stuck for 10 seconds is there any suggestion to cover that issue?
Post your code.
What do you mean "stuck for 10 seconds" ?
Interesting..
/** SNTP receive timeout - in milliseconds
* Also used as retry timeout - this shouldn't be too low.
* Default is 15 seconds. Must not be beolw 15 seconds by specification (i.e. 15000)
*/
#if !defined SNTP_RECV_TIMEOUT || defined __DOXYGEN__
#define SNTP_RECV_TIMEOUT 15000
#endif
are you sure it wasn't more like 15 seconds??
what lib you using??
~q
on an ESP8266 you don't need a third party library or a self written NTPClient. The ESP core comes already with the NTP functionality which runs in the background.
See
http://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm
http://werner.rothschopf.net/microcontroller/202112_arduino_esp_ntp_rtc_en.htm
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char *ssid = "*******";
const char *password = "********";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000, 60000);
// HH:MM:SS
// timeClient initializes to 10:00:00 if it does not receive an NTP packet
// before the 100ms timeout.
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay (500);
Serial.print (".");
}
timeClient.begin();
}
void loop() {
timeClient.update();
Serial.println(timeClient.getFormattedTime());
delay(1000);
}
only 1 second timeout in that lib..
// Wait till data is there or timeout...
byte timeout = 0;
int cb = 0;
do {
delay ( 10 );
cb = this->_udp->parsePacket();
if (timeout > 100) return false; // timeout after 1000 ms
timeout++;
} while (cb == 0);
could be a few more as it tries to listen on the udp port..
timeClient.update() will actually only update from time server once every 60 seconds, that correlates to the 60000 you have in the constructor ..
discovering internet disconnects involves a few timeouts, you could roll your own down detection, maybe ping with very short timeout that you know will always return unless internet is down..
the first post was from the esp8266 core and yes kind of built into time.h but eventually gets to where I showed with a 15 second timeout and looks like someone was lazy, as the timeout value is also the retry interval so can't really be shortened, stinkers..
the way I gather you are not really using esp8266, but just an interface board that adds wifi to your Arduino board, curious which board??
good luck, have fun.. ~q
Where to add this code?
It is appreciated if you can show me working code to try, thanks
it is 10 seconds not 1 second !
as mentioned already, don't use an external library. Use the core functionality.
10ms x 100 = 1000ms = 1 second.
calculation is correct but in real it takes 10 seconds
What does this produce?
Serial.println(millis());
timeClient.update();
Serial.println(millis());
Did you understand the post from @noiasca ? ... you don't need to use a library for NTP.
it is ok, NTP lib ignored
another point is there a way to use offset seconds instead of using Time Zone String ?
for example how to set UTC+3?
is there a way to check if esp8266 connected to server :
MY_NTP_SERVER "at.pool.ntp.org"
yes, you can define a callback which gets called when NTP was called.
read there:
http://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm
p.s.: I assume you are not connecting from Austria (at) ... chose a better fitting NTP pool for your country...
sure will choose another server location close to me
is it possible to help me with an example to try?
callback check if get a response from server
how to check if esp8266 not connected to server for some reason?