Many of our systems use @tzapulica’s WiFiManager to obtain a WiFi connection and from there we connect to the Blynk server.
We have tried numerous different ways and one example is shown in the sketch below, without WiFi Manager:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
//#include <BlynkSimpleUserDefined.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
char auth[] = "d4fdxxxxxxxxxxxxxxxxxxxxxxxxxx";
SimpleTimer timer;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V5);
bool connected = false;
void clockDisplay() // Digital clock display of the time
{
//Blynk.disconnect();
//Blynk.config(auth);
//Blynk.connectWiFi(ssid, pass)
//Blynk.run();
Blynk.begin(auth, "xxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxx");
//Blynk.begin(auth);
//Blynk.connect();
//do {
// connected = Blynk.connect();
//} while (!connected);
while (Blynk.connect() == false) {
// Wait until connected
}
rtc.begin();
delay(1000);
Serial.println("Should be connected to Blynk now");
BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
hour(), minute(), second(),
day(), month(), year());
Blynk.disconnect();
}
void setup()
{
Serial.begin(115200); // See the connection status in Serial Monitor
Blynk.begin(auth, "xxxxxxxxxxxx", "xxxxxxxxxxxx");
while (Blynk.connect() == false) {
// Wait until connected
}
rtc.begin(); // Begin synchronizing time
delay(1000);
Serial.println("Should be connected to Blynk now");
timer.setInterval(10000L, clockDisplay); // Display digital clock every 10 seconds
BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
hour(), minute(), second(),
day(), month(), year());
delay(1000);
//Blynk.disconnect();
//clockDisplay();
}
void loop()
{
Blynk.run();
timer.run();
}
When we run this sketch Serial Monitor provides the following details:
[1250] Connected to WiFi
[1250] My IP: 192.168.10.226
[1251] Blynk v0.3.4
[5001] Connecting to blynk-cloud.com:8442
[5343] Ready (ping: 1ms).
Should be connected to Blynk now
[6660] Current time: 00:00:06 01 01 1970
[7660] Time sync: OK
[16660] Connecting to GargoyleTest
[16660] Connected to WiFi
[16660] My IP: 192.168.10.226
[16660] Blynk v0.3.4
[20661] Connecting to blynk-cloud.com:8442
[21059] Ready (ping: 1ms).
Should be connected to Blynk now
[22380] Current time: 11:56:33 30 03 2016
[22381] Disconnected
[26660] Connecting to GargoyleTest
[26660] Connected to WiFi
[26660] My IP: 192.168.10.226
[26660] Blynk v0.3.4
[26660] Connecting to blynk-cloud.com:8442
[26985] Ready (ping: 5ms).
Should be connected to Blynk now
[28296] Current time: 11:56:39 30 03 2016
[28297] Disconnected
So the only time we obtain the Time sync: OK response is on the first connection and the first request. Subsequent connections and requests whilst running the same sketch fails to provide any response from the RTC. Same applies when we try sync feature.
The night before we ran a test on one of our systems with the basic RTC sketch and as far as we can see the 1000+ connections to the server gave 100% response rate for the RTC widget.
However the way we connect we are lucky if it is better than 30%.
Yes we request RTC on all connections but most of the time we receive no response.
As I say with a basic sketch RTC looks to be 100% reliable but not with our more complex sketches. It could be something in our sketches but you seem to have also found an issue with RTC under certain circumstances.
One of our systems only connects to Blynk for a few seconds before we send the ESP into deepsleep. I’m sure RTC would be ok if we stayed connected for the next sequential RTC update which I believe are 5 or 10 minute intervals. This is not practical as it is a battery operated system.
At the moment we are using nntp before we connect to Blynk but it would be preferable to obtain the time stamp from Blynk especially now you are about to introduce timezone settings to the widget.
@Costas did you see commit @vshymanskyy did in those ticket? I believe you can tune RTC request time to any value. Current BLynk sketch was made as simple as possible to avoid complex sketch and time for RTC request is predefined in it and it is 5-10 minutes I think. I believe you can change it to whatever you need. @vshymanskyy right?
@Dmitriy if you look at the Serial Monitor and associated sketch at the start of this thread you will see that we connect, disconnect and reconnect several times over a 30 second interval. This is not the way the actual system works but it is more to highlight our problem. So the first connection gives RTC data but none of the subsequent connections.
Thinking about this it is probably the 5 / 10 minute standard interval that is set with RTC. We expect our systems to sleep for 10 minutes (maybe 5) so it might not be a problem, but when we are testing we set the sleep period very short (30 seconds sometimes) as we don’t want to wait 5 or 10 minutes to see if our latest sketch changes are working.
On second thoughts this can’t be the problem as the 1000+ connections with the standard RTC sketch we made the night before last were at 20 second intervals (with a short deepsleep included within the 20 seconds).
A sample of the 8000+ lines of Serial Monitor data for the 1000+ succesful RTC requests are shown below:
[15661] Current time: 22:42:39 29 03 2016
Connecting to GargoyleTest
[1244] Connected to WiFi
[1244] My IP: 192.168.10.226
[1245] Blynk v0.3.4
[5001] Connecting to blynk-cloud.com:8442
[5311] Ready (ping: 1ms).
[5623] Time sync: OK
[15905] Current time: 22:43:05 29 03 2016
Connecting to GargoyleTest
[1252] Connected to WiFi
[1252] My IP: 192.168.10.226
[1252] Blynk v0.3.4
[5001] Connecting to blynk-cloud.com:8442
[5344] Ready (ping: 3ms).
[5680] Time sync: OK
[15680] Current time: 22:43:32 29 03 2016
Connecting to GargoyleTest
[1250] Connected to WiFi
[1250] My IP: 192.168.10.226
[1250] Blynk v0.3.4
[5001] Connecting to blynk-cloud.com:8442
[5309] Ready (ping: 0ms).
[5638] Time sync: OK
[15638] Current time: 22:43:59 29 03 2016
Connecting to GargoyleTest
[1247] Connected to WiFi
[1247] My IP: 192.168.10.226
[1247] Blynk v0.3.4
[5001] Connecting to blynk-cloud.com:8442
[5385] Ready (ping: 1ms).
[5743] Time sync: OK
[15742] Current time: 22:44:32 29 03 2016
I cut and paste your code but I am unable to get real time. See results
347644] Connecting to blynk-cloud.com:8442
[348270] Ready (ping: 1ms).
Should be connected to Blynk now
[349885] Current time: 00:05:49 01 01 1970
[349886] Disconnected
[357644] Connecting to blynk-cloud.com:8442
[358308] Ready (ping: 1ms).
Should be connected to Blynk now
[359920] Current time: 00:05:59 01 01 1970
[359921] Disconnected
[367644] Connecting to blynk-cloud.com:8442
[368238] Ready (ping: 0ms).
Should be connected to Blynk now
[369814] Current time: 00:06:09 01 01 1970
[369815] Disconnected
I did use the example widget code provided and got the same results. I did get some notification on a few odd occasions that I was sync’ed but this is no longer the case and at the time I was not probing the RTC time so I have no idea that it was working.
What I do find strange is that the Timer Widget works at the prescribed time and works beautifully. I don’t know where it is getting the real time from?
OK I ran the exact example RTC from Blynk Lib (I only added access code) this is my result
[2245] Blynk v0.3.10 on NodeMCU
[5001] Connecting to blynk-cloud.com:8442
[5554] Ready (ping: 0ms).
Current time: 0:0:16 1 1 1970
Current time: 0:0:26 1 1 1970
Current time: 0:0:36 1 1 1970
Current time: 0:0:46 1 1 1970
Current time: 0:0:56 1 1 1970
Sketch below
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxxx";
SimpleTimer timer;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V5);
// Utility function for digital clock display: prints preceding colon and leading 0
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10) {
Serial.print('0');
}
Serial.print(digits);
}
// Digital clock display of the time
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
Serial.print("Current time: ");
Serial.print(currentTime);
Serial.print(" ");
Serial.print(currentDate);
Serial.println();
// Send time to the App
Blynk.virtualWrite(V1, currentTime);
// Send date to the App
Blynk.virtualWrite(V2, currentDate);
}
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, ssid, pass);
while (Blynk.connect() == false) {
// Wait until connected
}
// Begin synchronizing time
rtc.begin();
// Other Time library functions can be used, like:
// timeStatus(), setSyncInterval(interval)...
// Read more: http://www.pjrc.com/teensy/td_libs_Time.html
// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
}
void loop()
{
Blynk.run();
timer.run();
}
Close the IDE.
Cut and paste 0.4.3 libraries (all 6 of them) to somewhere for safe keeping. Don’t try to drag them anywhere.
Then delete 6 old libraries.
Paste in the 6 new libraries.
Reopen IDE and reflash your device.