RTC code to show value on widget

Can somebody give me a code to show Time in the rtc widget and use it in a project.

1 Like

Have you look into basic RTC example?

I have. Just don’t know how to write the value to the widget or extract the values

Ok, for that I would recommend you to read this http://www.arduino.cc/en/Tutorial/StringAdditionOperator it may help.

virtualwrite for rtc?

Here you are:

 **************************************************************/

//#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
int isFirstConnect = true;

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

SimpleTimer timer;

WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V5);

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

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "WIFI", "Password" ,IPAddress (xxx,xxx,x,xx));
  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Begin synchronizing time
  rtc.begin();

  // Display digital clock every 10 seconds
  timer.setInterval(5000L, clockDisplay);
  timer.setInterval(5000L, clockvalue);
}

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
            hour(), minute(), second(),
            day(), month(), year());

}

// Digital clock display of the time
void clockvalue()
{

 int gmthour = hour();
  if (gmthour == 24){
     gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if(hourdigits == 1){
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();  
  if(minutedigits == 1){
    displayminute = "0" + displayminute;
  }  
  String displaysecond = String(second(), DEC);
  int seconddigits = displaysecond.length();  
  if(seconddigits == 1){
    displaysecond = "0" + displaysecond;
  }
  String displaycurrenttime = displayhour + ":" + displayminute;
  Blynk.virtualWrite(0, displaycurrenttime);

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

The result:

Display at V0 and RTC widget at V5 (+02:00) (Spain)

Regards!

Thanks to @Costas and @Pavel! They helped me a lot with this widget!!

Thanks a lot mate.

The setup was working fine yesterday but from today it’s showing the year 1970… I don’t know what’s wrong. Can you take a look ? @Pavel @Dmitriy

Please post your code.

 #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

> > // These are the interrupt and control pins for СС3000
> > #define ADAFRUIT_CC3000_IRQ   3
> > #define ADAFRUIT_CC3000_VBAT  5
> > #define ADAFRUIT_CC3000_CS    10

> > #include <SPI.h>
> #include <Adafruit_CC3000.h>
> #include <BlynkSimpleCC3000.h>
> #include <SimpleTimer.h>
> #include <TimeLib.h>
> #include <WidgetRTC.h>
> SimpleTimer timer;

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



> WidgetRTC rtc;

> BLYNK_ATTACH_WIDGET(rtc, V5);

> void setup()
> {
>   pinMode(13,OUTPUT);
>   Serial.begin(9600); // See the connection status in Serial Monitor
>    Blynk.begin(auth, "net_9718015549call", "macbeth123", WLAN_SEC_WPA2);

>   while (Blynk.connect() == false) {
>     // Wait until connected
>   }

>   // Begin synchronizing time
>   rtc.begin();
> //Serial.print(hour());
>   // Display digital clock every 10 seconds
>   timer.setInterval(10000L, clockDisplay);
> }


> void clockDisplay()
> {
>   // You can call hour(), minute(), ... at any time
>   // Please see Time library examples for details
>   BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
>             hour(), minute(), second(),
>             day(), month(), year());

> }

> // Digital clock display of the time
> void clockvalue()
> {

>  int gmthour = hour();
>   if (gmthour == 24){
>      gmthour = 0;
>   }
>   String displayhour =   String(gmthour, DEC);
>   int hourdigits = displayhour.length();
>   if(hourdigits == 1){
>     displayhour = "0" + displayhour;
>   }
>   String displayminute = String(minute(), DEC);
>   int minutedigits = displayminute.length();  
>   if(minutedigits == 1){
>     displayminute = "0" + displayminute;
>   }  
>   String displaysecond = String(second(), DEC);
>   int seconddigits = displaysecond.length();  
>   if(seconddigits == 1){
>     displaysecond = "0" + displaysecond;
>   }
>   String displaycurrenttime = displayhour + ":" + displayminute;
>   Blynk.virtualWrite(0, displaycurrenttime);

> }
> void loop()
> {
>   Blynk.run();
>   timer.run();
> }

This is tarter strange, do you know if the problem happened after device restart, or was it online for the whole day?

After the restart.

I checked it yesterday, it was working fine.

Today I plugged it in and it started showing the year 1970 :confused:

@vshymanskyy could the problem be in

// Begin synchronizing time
rtc.begin();

? Maybe it should be placed in BLYNK_CONNECTED?

Any updates? @Dmitriy @vshymanskyy

@KAUSTUBHAGARWAL could you please try my suggestion?

BLYNK_CONNECTED() {
    rtc.begin();
}
1 Like

I don’t know the problem…It’s not working.

The weird thing is it was working fine on one day and now like this :confused:

I just checked rtc and working no problems… I think we need to look deeper in DEBUG logs…

I think rtc is on a 5 minute update cycle.
wait five minutes and your clock should update

@KAUSTUBHAGARWAL @Dmitriy @vshymanskyy

The RTC shows 1/1/1970 for the first 5 minutes if you miss this line of code:

while(Blynk.connect() == false);

equivalent to:

while(Blynk.connect() == false) { }

This line has to be inserted after Blynk.begin and before rtc.begin:

Blynk.begin(auth, wifi, ssid, pass);     // Blynk Server

while(Blynk.connect() == false);

rtc.begin();

In such a way the rtc hows immediately the correcet date and time.
Regarding the code to format date and time, in my opinion, the most elegant lines of code are:

sprintf(Date, "%04d/%02d/%02d", year(), month(), day());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());

Blynk.virtualWrite(V1, Date);
Blynk.virtualWrite(V2, Time);

If you don’t want leading zero, exclude the 0 from the formatting section of sprintf.

Ciao,
Giancarlo

1 Like

Date and Time are arrays of char:

char Date[16];
char Time[16];

Thanks Gianca for sharing !!!
That did the trick