RTC widget example sketch not syncing time


I am using an Arduino Mega with an ESP8266 as wifi shield to run a brewing rig (automated temp control and dashboard of temperatures and valve settings in Blynk app).

My goal with the RTC widget is to know the time for which the process has been running even in case I lost wifi connection and the arduino was ‘down’ while blynk.begin is trying to reconnect. Upon reconnection, if the program knows the current mashing rest period started at for example 12:05 and it is now 12:35 when Blynk.begin manages to reconnect there are 30min to add to the time tracking (which resets to 0 upon restart of the arduino given it is based on millis() ).

I have been able to make the RTC widget example work only in parts: It starts clocking hours, minutes and seconds since start of the program but does not sync the time (it stays 1970…) which does not help me (acting like a millis() funktion).

Since it obviously does not sync I was guessing rtc.begin must be the issue but repositioning it in the sequence etc. all did not help.

The only changes I made to the boiler plate example is the Blynk and wifi connections (given I am not using Ethernet) and that I want hour and minutes not as string but as integers to be able to calculate the minutes between two time readings.

I am out of ideas - whats the secret ?


/*----- BLYNK -----*/

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>  // NEU!
#include <WidgetRTC.h>  // NEU!

char auth[] = "xxxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxx";

/*----- connect to Wifi -----*/
#define EspSerial Serial1

#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

WidgetRTC rtc;  // NEU!

// Digital clock display of the time
void syncRTC()
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  int hourtest = hour();
  int minutetest = minute();
  int secondtest = int (second());
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("hour: ");
  Serial.print("minute: ");
  Serial.print("second: ");

  Serial.print("Current time: ");
  Serial.print(" ");

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);

// Synchronize time on connection

void setup()
  // Debug console

 /*----- Set ESP8266 baud rate -----*/
  EspSerial.begin(ESP8266_BAUD);  // vorhanden
  Blynk.begin(auth, wifi, ssid, pass);   // vorhanden

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

  setSyncInterval(60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(10000L, syncRTC);


void loop()


As you shared your Auth code (which you now need to regenerate) I checked the JSON for project and you haven’t added the RTC widget to your app, which is why it’s not getting the current time from the Blynk server.

Also, you shouldn’t be using Blynk.begin.
Instead you should use Blynk.config and Blynk.connect, as these won’t block the execution of your code.

If this is a mission critical (and what’s more critical than :beer::beer::beer:) then you may want to go for a hardware RTC that maintains the time even when Blynk isn’t available.

You should also use the hours/minutes/seconds of when a process starts, rather than using millis.
You may want to store critical data in EEPROM memory so that it’s retained and useable even after a reboot and no Blynk reconnection.


1 Like

Perfect - added the widget and it is working now.

I would love to use Blynk.config and Blynk.connect but for the life of me I cannot make it work. I read hours through the examples and tried… Any example how to exactly put it in the code above would be awesome.

Yes now that I have the timestamps I will replace millis. And EEPROM I just implemented.

I wanted to use RTC hardware but my I2C port is occupied by the LCD… so the widget is a good way of doing it. Accuracy within a couple of minutes is enough.

And yes - glad you understand the criticality of brewing with an IOT brewing rig :slight_smile: !

1 Like

I think it’s a bit harder with the horrible Arduino/ESP01 combo. Here’s one tip about how to get it working:

And a few more places to look:

Is there any reason why you can’t use a NodeMCU or ESP32?


It works perfectly!! thanks a million! I now do not lose the program when wifi gets lost, it reconnects automatically and I have the EEprom readings to keep the time tracking correct regardless. awesome :slight_smile:

I have my arduino setup - quite need a lot of IOs therefore using the Mega. Rather not inclined to change everything now that it works.

Are you writing the time to the EEPROM? If you are then that’s not the correct approach. Each EEPROM memory location has a life expectancy of around 100,000 write operations, so you need to keep writes to a minimum.

You should be using EEPROM to store key values that need to be restored at start-up, and only write those values if they have changed from the previous stored value.


I only write the start time of the measured period to the EEPROM:

The task is the following: A mash period for example ist 60 minutes at 65 °C. This time tracking should not get lost in case the Arduino gets interrupted at some point.

Now that you showed me how to implement the Blynk / Wifi reconnect without stopping the program the issue is much less relevant.

Nevertheless, my implementation is done by writing once per brew the program parameters to the EEPROM and storing the start time of the mash period (typically 1 -2) in the EEPROM which allows me to calculate the elapsed mash time upon restart during a program interrupt.

My understanding is that EEPROM.update and EEPROM.read do not ‘stress’ the memory. So per brew I have two to three writes. Which means I could brew 30.000 times with one arduino. At my rate of 10 brews per year that is 3000 years :slight_smile:

1 Like

I saw some sketches that make Blynk.run() dependent on Blynk.connected() - is that recommendable? I have quite a lot of data being transferred from the arduino to Blynk and already allocated the write - operations across three Blynk.timers with different time spacings to reduce the decelleration I noted…

You should be hoping that Blynk will be connected 99.9% of the time (at least) so adding a check within the void loop to see if it is connected is just eating-up processor time for that 0.1% or less of time when it isn’t connected.
The normal reason for checking if Blynk is connected in that way is to quickly be able to do a re-connect, or if you are doing something with deep sleep on a NodeMCU.