Seeking help to rewrite a Sync function for Makuna-RTC

I’m migrating my project over from Arduino Mega to an ESP8266 environment and in doing so, it seems the RTClib library no longer works with the DS3231 RTC and/or the ESP. So I found another library that functions similarly, though when it comes to my function that attempts to update the RTC with readings from the Blynk server, the RTC time is set to 170 years into the future. I realize this is mostly a library issue, and not so much a hardware or Blynk issue. Still though, I’m asking for assistance in the event someone more skilled than I can spot any concerns or has specific knowledge of this library.

void syncRTCHardware()
{
  if (!Blynk.connected())
  {
    return;
  }//DateTime now = RTC.now();          // reads hardware time at beginning of loop
  RtcDateTime dt = rtcObject.GetDateTime();

  // time library is synchronized by the Blynk WidgetRTC
  if (Blynk.connected())
  {
    bool syncClocks = false;
    bool printOnce;
    if (dt.Hour() != hour() || dt.Minute() != minute()) syncClocks = true;
    if (syncClocks == true)
    {
      syncClocks = false;
      Serial.println("Resynching RTC Time...");
      if (printOnce)
      {
        terminal.println("Resynching RTC Time...");
        printOnce = false;
      }
      RtcDateTime dt = RtcDateTime(year(), month(), day(), hour(), minute(), second());   //Sync hardware with Blynk server time
      rtcObject.SetDateTime(dt); //configure the RTC with object
       //rtcObject.SetDateTime(RtcDateTime(year(), month(), day(), hour(), minute(), second()));   //Sync hardware with Blynk server time
      terminal.flush();
    }
    printOnce = true;
  }
}

Here is a link to the library documentation. https://github.com/Makuna/Rtc/wiki/RtcDateTime-object. When using RTClib, the syncRTChardware function worked flawlessly. I’ve adjusted the code the best I can to utilize the new library. The time populates the display widget when the above function is commented out, but when the function is implemented, it malfunctions and resets the RTC to the future.

Any help at all, even if better study leads is most wanted and welcomed. Thank you in advance!

A little Googling found some references to ESP8266 using additional libraries for I2C

1 Like

Thanks Gunner, I think that might be the tutorial I followed when switching to the Makuna library. It functions very similar to the RTClib which sort of made it easy for me to switch out my code, and when not trying to sync my RTC to Blynk’s server time, the library keeps the time and I don’t have any problems getting those readings to the display widgets. My problem is when I try to rewrite the time with the RTC widget values of (year(), month(), day(), hour(), minute(), second()). As previously stated, when using RTClib on the Arduino Mega, the time syncing worked great for well over a year. I guess I’m just experiencing growing pains trying to move my project into an ESP environment.

I think I saw reference to same modual on Adafruit… and something about syncing time from UI… check that out.

I’m back to using RTClib, but for some reason it still will not take the Blynk values and apply them to DateTime. I’ve compared both Blynk and RTClib libraries and they both use the same data types for the variables I’m working with. I’m thinking of just chucking the RTC as it’s holding up my project migration.

Question, I understand that if using the cloud server, the RTC widget retrieves the time from the cloud somewhere on the web, but does the local server store time locally, ie on the rPi? I just need the ability to maintain current time independent of the internet.

Local Server gets (and passes onto sketch’s RTC) it’s time from the RPi system (or whatever type PC used to run your LS). So you need to make sure that system is getting proper time via whatever method it uses.