RTC not syncing when connecting to Local or Cloud Server over SSL

First post, been reading and learning for months.
I have a series of Arduino projects, all running on ESP8266 and using Blynk local server. They use RTC and it works well, if a bit twitchy when first connecting as the initial sync takes a while. Syncing stored variables seems to help, so that’s not the problem.

The problem is that, when I enable SSL, RTC then fails to work. I have spent a couple of hours playing around with things until it dawned on me that I should try the Blynk-Cloud server and see if the same thing happens there - that’s the gold standard.

So I customized the ESP8266_Standalone_SSL, first just getting it working, then adding the terminal, value, and RTC.

The issue happens with Blynk-cloud server as well; I successfully connect via SSL but no time is retrieved. In my code, I output the MAC to the terminal and it never does that, even though the program is still running which I verify by having a value showing millis() and it increments.

The Serial output shows:
[3261] Connected to WiFi
[3261] IP: 172.16.0.52
[3261]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.7 on NodeMCU

[5001] Connecting to blynk-cloud.com:8441
[5407] Certificate OK
[5457] Ready (ping: 1ms).
[5925] Connecting to blynk-cloud.com:8441
[6337] Certificate OK
time should be set: 0:00
Wifi connected and starting up
[11441] Connecting to blynk-cloud.com:8441
[11848] Certificate OK
[11896] Ready (ping: 1ms).

My code is as follows:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266_SSL.h>
// Time - workes with Blynk to get server time
#include <TimeLib.h>
#include <WidgetRTC.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "my auth";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "my ssid";
char pass[] = "my key";

// start timer to send data to blynk server
BlynkTimer timer;

// start real-time-clock widget
WidgetRTC rtc;

// Blynk virtual pin assignments
#define TERMINAL                 0  // V0
#define ALERT                    3  // V3

// for terminal - redirect all serial output by s/r for terminal.print and replace with terminal.print
WidgetTerminal terminal(TERMINAL);

// flip wifi mac so its in the correct order for display and usage
String ebFlipMAC(String macAddr) {
  macAddr = macAddr.substring(15) + ":" +  macAddr.substring(12, 15) + macAddr.substring(9, 12) + macAddr.substring(6, 9) + macAddr.substring(3, 6) + macAddr.substring(0, 2);
  return (macAddr);
}

// show properly formatted time
String ebShowTime () {
  if (minute() < 10) return (String(hour()) + String("\:0") +  String(minute()));
  else return (String(hour()) + String("\:") +  String(minute()) );
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  if (Blynk.connected())   {
    rtc.begin();
    Blynk.syncAll();
    terminal.println("");
    terminal.println("");
    terminal.println("********************************************");
    terminal.println("");
    terminal.println(F("Blynk v" BLYNK_VERSION));    
    terminal.println(F("Unit booted, on WiFi and syncing with server"));
    terminal.flush();
    terminal.print("Syncing");
    terminal.flush();
    for (int i = 0; i < 10; i++) {
      delay(10);
      terminal.print(".");
    }
  }
  else {
    Serial.println("WiFi not connected");
    delay(2000);
    return;
  }
  Blynk.run();
  Blynk.run();
  Blynk.run();
  Serial.println("time should be set: " + ebShowTime());
  terminal.println(" Time is prev value or 0: " + ebShowTime());
  terminal.flush();

  Serial.println("Wifi connected and starting up");
  delay(50);

  // print mac address
  terminal.print("MAC: ");
  terminal.println(ebFlipMAC(WiFi.macAddress()));
  terminal.println("");
  terminal.println("********************************************");
  
}

void loop()
{
  Blynk.run();
  //Blynk.virtualWrite(ALERT,millis());  // commented out to not flood blynk-cloud :slight_smile: 
}

In the terminal on my iPhone, it never gets past printing “Syncing” - it never shows the series of dots or " time is prev or 0:" so I suspect that’s when the first rtc call fails.

Running this without SSL shows the time immediately, not 0:0, and the MAC then displays. In my actual program, I use a SimpleTimer and print the time every minute; after 12 minutes, I still had no time except 0:12 which means RTC fails.

So, am I doing something wrong, or is there some issue with RTC not working over an SSL connection?

I can’t speak for any SSL issue, but you might wish to have your RTC connect to another time source as well as resynchronize every so often.

RTC is based off of this library.

setSyncProvider(getTimeFunction);  // set the external time provider
setSyncInterval(interval);         // set the number of seconds between re-sync

What’s your reasoning - in case the Blynk server is down or unreachable? My goal is to have my ESP9266 talk securely to my local Blynk server (which is hosted) and get the time from it for reporting purposes only. All internal tasks are controlled by the Blynk timer and the seconds/minutes/hours counters. So truly accurate time isn’t so much of a concern, it seems to me. But I realize that you’ve likely got a very good reason for the suggestion!

ETA: I’ll see if using a different time source works while using SSL,duh.

I think you got it at the end :wink: but both suggestions were…

  1. acquire time from alternate source instead of over SSL encrypted server link, as the added encryption might be overtasked enough to mess up the time sync.

  2. the re-sync was essentially the same reasoning… if it keeps checking, then even if it misses the first time or two, it should eventually sync the correct time.

I shouldn’t have read and responded when my head was hurting and I was groggy and clearly couldn’t comprehend. Thank you for being kind. I’ll update the code and post the results. Thanks again!

I haven’t had the time to fully explore this as I’m not a developer and so it’s not so easy to replace the RTC mechanism.

But I did test the TimeNTP_ESP8266WIFI example from the Time library. First without SSL, then with SSL enabled and using the Blynk.cc server. In both cases, the time syncs, so that’s good.

But how can I be sure that the problem isn’t really a bug in the Blynk RTC code? I’d prefer to have my ESP8266 do all its correspondence with my server, so getting my time is a goal.

I tried to get my new ESP32 to work, but I can’t get Blynk with SSL to compile for it. I’m not sure how to program for the Pi, but that’s my next step to see if SSL and RTC work together or if RTC is buggered as I suspect. Unless some kind soul reading this can confirm that they have RTC working over SSL. I’ll get to it when I can - and thanks for the inspiration to find a work-around, even if it’s sub-optimal.

Just use an RTC chip then: https://www.banggood.com/Wholesale-New-Arduino-I2C-RTC-DS1307-AT24C32-Real-Time-Clock-Module-Board-for-AVR-ARM-PIC-p-46446.html?rmmds=search

You could post the detailed issue on Blynk’s GitHub page: blynkkk (Blynk IoT platform) · GitHub

Thanks again Gunner, have a great one!