[SOLVED] RTC Time Sync - setSyncInterval

Hi all,

I have a problem with RTC.
The first time I try to get time I get back year-> 1970

Thx,

Andreas

Here is my code:

#define BLYNK_PRINT Serial

#include "SimpleTimer.h"
#include <Ethernet.h>
#include <BlynkSimpleEsp8266.h>
#include <DallasTemperature.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


const char* auth     = "0fd--------a-d4";

const char* ssid     = "xxx"; 
const char* password = "xxxx";  //del


WidgetLED       GarageLed(V6);
WidgetRTC       rtc;
WidgetTerminal  BlynkTerminal(V2);
boolean         AlarmState = false;
boolean         NightMode;
int             AlarmTimerId;
int             PIRState = false;
unsigned long   LastAlarmMillis;
SimpleTimer     AlarmTimerOff;
SimpleTimer     Timer1;
bool isFirstConnect = true;


BLYNK_ATTACH_WIDGET(rtc, V5);


//==================================================================================
//==================================================================================


BLYNK_CONNECTED()
{
  if (isFirstConnect)
  {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}


//==================================================================================
//==================================================================================

void clockDisplay()
{
  Serial.println (TimeS());
}

//==================================================================================
//==================================================================================

String lpad(String txt, byte width, String pattern) {
  String tmp = txt;

  for (byte i=tmp.length(); i<width; i++)
    tmp = pattern + tmp;
 
  return tmp;
}

//==================================================================================
//==================================================================================

String TimeS()
{
  String currentTime = lpad(String(hour()), 2, "0") + ":" + lpad(String(minute()), 2, "0") + ":" + lpad(String(second()), 2, "0");
  String currentDate = String(day()) + "/" + month() + "/" + year();
  return (currentDate + " " + currentTime);
}


//==================================================================================
//==================================================================================

void setup()
{
  Timer1.setInterval(3000, clockDisplay);
 
  Serial.begin(115200);

  Blynk.begin(auth, ssid, password);
  while (Blynk.connect() == false) {}
  Serial.println("Connected To Blynk");
  
  rtc.begin();
}


//==================================================================================
//==================================================================================

void loop()
{
  Blynk.run();
  Timer1.run();
}



And the output I get: 

[3247] Connected to WiFi
[3247] IP: 192.169.200.162
[3247] Blynk v0.3.7 on ESP8266
[5001] Connecting to blynk-cloud.com:8442
[5182] Ready (ping: 1ms).
Connected To Blynk
1/1/1970 00:00:05
[5375] Time sync: OK
13/6/2016 09:30:21
13/6/2016 09:30:24
13/6/2016 09:30:27
13/6/2016 09:30:30
13/6/2016 09:30:33
13/6/2016 09:30:36

I think the problem is in Blynk.syncAll();

BLYNK_CONNECTED()
{
  if (isFirstConnect)
  {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

Inside syncAll() probably the virtual pins are synced before time sync. So if you try to get a timestime, you will not get it.

As a result of that when we have the following is automatically executed (because of SyncAll) it does not have the time synced yet.

BLYNK_WRITE(V4)
{
  if (param.asInt()==1)
  {
    NightMode = false;
    .... print timestamp
  }
  else
  {
    NightMode = true;
    .... print timestamp
  }  
}

I dont know if I am right but what I suggest is that inside SyncAll, timesync must be executed before the BLYNK_WRITE

Andreas

You probably right. You may skip wrong date with simple condition. like if year == 1970

1 Like

thx Dimitry

As it is an alarm logging, δate is needed from the first hit. For the time being I did exclude 1970 dates.

I do think though that there is a problem with RTC sync.

Check the following.

Time Sync is not done in the first minutes. It is done only after a big idle time ( [461822] Time sync: OK
)

[3249] Connected to WiFi
[3249] IP: 192.169.200.162
[3249] Blynk v0.3.7 on ESP8266
[5001] Connecting to blynk-cloud.com:8442
[5188] Ready (ping: 1ms).
Syncing ......AK..
## No Time ??       [Garage] Booted
## No Time ??       [Garage] Day Mode
## No Time ??       [Garage] ALARM On
## No Time ??       [Garage] ALARM Off
## No Time ??       [Garage] ALARM On
## No Time ??       [Garage] ALARM Off
## No Time ??       [Garage] ALARM On
## No Time ??       [Garage] ALARM Off
## No Time ??       [Garage] ALARM On
[461822] Time sync: OK
13/06/2016 14:16:39 [Garage] ALARM Off
13/06/2016 14:38:46 [Garage] ALARM On
[1791385] Time sync: OK
13/06/2016 14:38:48 [Garage] ALARM Off

@vshymanskyy any suggestions?

Yes.

TimeSync must be better handled.
It is not clear for me and maybe for others when the timesync occures.
I would really like an answer on that.

I believe one solution would be to include time sync in the syncall function or to sync with a separate command.

thx

Andreas

The same proble is described here

LINK

and here

LINK2

Guys PLEASE !

RTC is a great and important feature that does not work well.
Time sync happens in random times…

please :slight_smile:

Andreas

I don’t think it is random, I believe the default is to sync every 5 minutes and you can change the sync interval.

Costa please give more details:

  1. How to force time sync on startup (setup() function)
  2. How do we change the sync interval.

Thx

Andreas

http://www.pjrc.com/teensy/td_libs_Time.html as used by Blynk’s RTC widget at https://github.com/blynkkk/blynk-library/blob/b976c1f1522a30899e261cf0538ee66937b43a5d/examples/Widgets/RTC/RTC.ino makes use of setSyncInterval(seconds)

So in setup() maybe something like:

  setSyncInterval(1);
  rtc.begin();
  Blynk.syncAll();

Also do the == 1970 check.

Costas you are a GOD!

This works perfectly…

void setup()
{
  Serial.begin(115200);

  setSyncInterval(1);

  rtc.begin();
  Blynk.begin(auth, ssid, password);
  while (Blynk.connect() == false) {}

  delay(1000);
  setSyncInterval(900000);  //every 15min
}
1 Like