Blynk and external NTP Server with NtpClientLib - compatibility?

I am building a new project with Blynk and external NTP Server.
Basically the problem is very simple to explain.

The sketch first call the Blynk.beguin and after NTP Server NTP.begin (libray NtpClientLib) is started for syncronization.
The problem NTP never get syncronized.
If I used the opposite sequence first call NTP server NTP.begin and then call the Blynk.beguin, the sketch freezes during the Blynk.beguin

When I use separated sketch, like only Blynk, it works ok.
And only NTP sync, it also work ok.

Also I noticed that by using WiFi.begin(ssid, pwd) and WiFi.status with Blynk, the status never get connected, however WiFi.localIP() can bring the IP address.

The question here by using Blynk and external NTP server NtpClientLib, is there any compatibility problem ?

Thanks.

I had no problem with NTPClient and Blynk lib version 0.4.0 (the previous one). Not tested with current, but there should be no problem too as NTP is started and time is acquired before blynk even starts. If you will not make it, tomorrow can share my experience. (Or possibly someone else will make it)
Good night (at least in EE :wink: )

I am using 0.4.1 the last one.

I didn’t try to wait for NTP time response, before calling Blynk. (It should be a trial tomorrow).
They are just in sequence for execution.
And in this case Blynk.beguin freezes on that line.

Here is the relevant lines :

include < NtpClientLib.h >
include < BlynkSimpleEsp8266.h >
: : : :
void setup()
{
// NTP - starting
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
: : : :
NTP.begin(“a.ntp.br”, 1, true);
NTP.setInterval(1800);
// NTP - end

Blynk.begin(auth, ssid_rede, senha_rede); ====> It freezes here.
}

Thanks for quick reply.
Good Night from Brazil.

Is there any reason you aren’t using the RTC widget and syncing with that? In my experience that works best (and fastest).

1 Like

Well @Lichtsignaal , as it comes to me:

  1. The device can work without Blynk (as a standalone)

  2. On my dashboard there is no place for that (would have to use Tabs, which I don’t want in this case):

  3. NTP works just fine.

And @evandrogomes, here is what I did:
Actually I just used a standard example for NTP client from TIME library, so these were included:

#include <TimeLib.h>
#include <WiFiUdp.h>

then there is setup():

void setup()   {
	...
        WiFistatus = connectWifi();
	if (WiFistatus) {
		NTPsetup();
		delay(1500);
		Blynk.config(auth);
		Blynk.disconnect();
		uint8_t counter = 0;
		while (timeStatus() == timeNotSet && counter < 5) {
			counter++;
			Serial.print(F("NTP Time was not set!, retry #"));
			Serial.println(counter);
			setSyncProvider(getNtpTime);
			delay(1500);
		}
	}
	if (timeStatus() == timeSet) {
		Serial.print(F("\r\n * NTP time retreived:\r\n"));
		...
	}
	else Serial.println(F("Getting time from NTP failed.(...)"));
	...
}

here is called NTP setup():

void NTPsetup() {
	Serial.println(F("Starting UDP"));
	Udp.begin(localPort);
	Serial.print("Local port: ");
	Serial.println(Udp.localPort());
	Serial.println("waiting for sync");
	setSyncProvider(getNtpTime);
	setSyncInterval(900);
}

and finally the connectWifi():

bool connectWifi() {
	Serial.print(F("connecting to WiFi AP: "));
	Serial.println(ssid);

	WiFi.persistent(false);
	WiFi.mode(WIFI_OFF);   // this is a temporary line, to be removed after SDK update to 1.5.4
	WiFi.mode(WIFI_STA);
	delay(200);
	WiFi.begin (ssid, pass);

	uint32_t  startTime = millis();
	while (WiFi.status() != WL_CONNECTED && millis() - startTime < 30000) {
		delay(1000);
		Serial.print('.');
	}
	Serial.println();
	// Check connection
	if (WiFi.status() == WL_CONNECTED) {
		Serial.print(F("WiFi connected with IP address: "));
		Serial.println(WiFi.localIP());
		return true;      //return true IF connected
	}
	else {
		Serial.print(F("WiFi connection to: "));
		Serial.print(ssid);
		Serial.println (F(" failed!"));
		Serial.println (F("Runing in standalone mode. (cycle POWER to retry)"));
		WiFi.disconnect();
		return false;   ////return false IF NOT connected
	}
	delay(500);
}

the Blynk is started later, already from loop() (in a timed manner):

note the Blynk.connect() and Blynk.disconnect() (which you may not need)

if (WiFi.status() == WL_CONNECTED && (!Blynk.connected()))  {
	...
	Serial.println(F("Setting up BLYNK connection..."));
	...
	Blynk.connect(); 
	uint32_t  startTime = millis();
	while (millis() - startTime < 30000UL) {
		...
		Serial.print(".");
		if (Blynk.connected()) break;
	}
	Serial.println();
	if (Blynk.connected()) {
		Serial.println (F("BLYNK service connected..."));
		...
	}
	else {
	Serial.println (F("TIMEOUT ERROR! Disconnected..."));
		...
		Blynk.disconnect();
	}
}
2 Likes

@marvin7, it was difficult for me to adapt your code with some compilation problem. Also, at beguining I want avoid usage of RTC widget, then I tried once again with external NTP.

I have adapted part of your code, how you handled the Blynk and NTP.
So, finally I got it.
Basically, I will explain some parts of the code.

The main changes was the Blynk.beguin. I am not using it in this way anymore.
The new lines (taken from your code) :

Blynk.config(auth); Blynk.disconnect();

After that : delay(30000); ( not sure if this is relevant )

Then the Blynk connection starts here.

if (!Blynk.connected()) {

     Blynk.connect();
     while (!Blynk.connected()) {
          Serial.print(".");
          delay(2000);
      }

  if (Blynk.connected()) {
      Serial.print("Blynk connected");
  }
  else {
      Serial.print("Could not connect to Blynk");
      Blynk.disconnect();
  }
}

After that, Blynk got connected and the NTP got syncronization.

Thanks a lot for help.

As a rule, such a long delays should be avoided, after all you have to wait for 30 seconds! Better check some condition and use timeout (you can see an example in my code too):

uint32_t  startTime = millis();
while (millis() - startTime < 30000UL && !Blynk.connected()) {
    someCode or empty loop;
}

where loop is repeated as long as Blynk is NOT connected (hence the ‘!’) OR passed time is greater than 30 thosands miliseconds (30s)

but I’m sure You will slowly get into this (as I did :wink: )

Yes. That was my first attempt.
Unfortunatelly, for unknown reasons it fails.

I am using ESP8266 NodeMCU.
When I use that code While with such time parameters, I get Flash Read Error in serial terminal.
And some hexa dump printouts.

Never mind.
This is only executed during the startup. :wink:

Thanks for sharing.