Help with converting RTC number to local time and date format

Hi, I wonder if someone can help me…

My project uses a Arduino mega and a ESP-01 wifi shield for doing some monitoring around the house. I am using a WDT to reset the mega if/when the connection is ever lost. I would like to log the time of the disconnect to EEPROM memory so that I have a record of when the project goes offline.

I am intending to save the time and date to EEPROM as the int32(?) which is given by now() from the Blynk RTC Widget. Therefore, in order to read the number back out from the EEPROM memory, I need to convert the number into the local time and date format.

I have searched in a number of places and I have found code to convert from raw time number to time date format but it doesn’t work in my script. I get an error message which I think may be caused by a conflict between the WidgetRTC.h, TimeLib.h and time.h libraries.

This is where I got the example code, but notice that this script doesn’t have the WidgetRTC.h library.

I am running on a Arduino mega and a ESP-01 wifi shield. I am running Blynk off the online server on iOS phone.



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

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266_hack.h>

// Real Time Clock library
#include <WidgetRTC.h>
#include <TimeLib.h>
#include <time.h>

// Blynk project authentication code
char auth[] = "xxxx";

// WiFi credentials.
char ssid[] = "xxxx";
char pass[] = "xxxx";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

WidgetRTC rtc;

char dateBuffer[12];
String currentTime;
String currentDate;

String Recorded_time;
tmElements_t tm;
char time_stamp[80];

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

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);

  Blynk.begin(auth, wifi, ssid, pass);
  
  //Real time clock begin.
  rtc.begin();

  // Run Blynk.run a few times to allow rtc to sync correctly.
  Blynk.run();
  Blynk.run();
  Blynk.run();
  Blynk.run();
    
  timer.setInterval(1000L, Uptime);
  timer.setInterval(5001L, clockDisplay);

  setSyncInterval(10 * 60); // Sync interval for RTC in seconds (interval is in seconds so this is 10 minutes)
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

//// Uptime counter
void Uptime()
{
  Blynk.virtualWrite(V0, millis() / 1000);
}

void  LogDisconnected()
{
  // Print to serial monitor
  Serial.print("Disconnected at: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  Serial.flush();
}

// Digital clock display of the time
void clockDisplay()
{
  sprintf(dateBuffer, "%02d:%02d:%02d", hour(), minute(), second());  
  currentTime = dateBuffer;
  sprintf(dateBuffer, "%04d/%02d/%02d", year(), month(), day()); 
  currentDate = dateBuffer;
  
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  Recorded_time=now();
  Serial.println(Recorded_time);
  tm = localtime(Recorded_time);  <-------------------  This is where the problem is.
  strftime (time_stamp,80,"%a %I:%M %p %d %b %G %Z.",tm); <-------------------  This is where the problem is.
  Serial.println(time_stamp);
}

This is the error message…
‘localtime’ was not declared in this scope

if I comment this line out then I get another one…
‘strftime’ was not declared in this scope

@jonny.chad Change to
tm = localtime(&Recorded_time);
strftime (time_stamp,80,“%a %I:%M %p %d %b %G %Z.”,tm);

Hi Mohan, thats for your reply and thanks for your original code which I copied. Unfortunately making that change did not help with the error. Good news is that I have figured out a way of achieving what I am trying to do, and as I suspected, it is quite straight forward. The problem with my original code was the three libraries WidgetRTC.h, TimeLib.h and time.h together were causing conflicts. After having a bit more of a look at the TimeLib.h library, I realised this library has the functions I need and I don’t need to use the time.h library.

The working code is as follows…

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

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266_hack.h>

// Real Time Clock library
#include <WidgetRTC.h>
#include <TimeLib.h>

// Blynk project authentication code
char auth[] = "xxxx";

// WiFi credentials.
char ssid[] = "xxxx";
char pass[] = "xxxx";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

WidgetRTC rtc;

char Buffer[12];
String timebuffer;
String datebuffer;
long Recorded_time_num; 
tmElements_t Recorded_time_ele;

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

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);

  Blynk.begin(auth, wifi, ssid, pass);
  
  //Real time clock begin.
  rtc.begin();

  // Run Blynk.run a few times to allow rtc to sync correctly.
  Blynk.run();
  Blynk.run();
  Blynk.run();
  Blynk.run();
    
  timer.setInterval(1000L, Uptime);
  timer.setInterval(5001L, clockDisplay);

  setSyncInterval(10 * 60); // Sync interval for RTC in seconds (interval is in seconds so this is 10 minutes)
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

//// Uptime counter
void Uptime()
{
  Blynk.virtualWrite(V0, millis() / 1000);
}

// Digital clock display of the time
void clockDisplay()
{
  sprintf(Buffer, "%02d:%02d:%02d", hour(), minute(), second());  
  timebuffer = Buffer;
  sprintf(Buffer, "%04d/%02d/%02d", year(), month(), day()); 
  datebuffer = Buffer;
  
  Serial.print("Current time: ");
  Serial.print(timebuffer);
  Serial.print(" ");
  Serial.print(datebuffer);
  Serial.println();

  Recorded_time_num=now();
  Serial.println(Recorded_time_num);

  breakTime(Recorded_time_num,Recorded_time_ele);
  sprintf(Buffer, "%02d:%02d:%02d", Recorded_time_ele.Hour, Recorded_time_ele.Minute, Recorded_time_ele.Second);  
  timebuffer = Buffer;
  sprintf(Buffer, "%04d/%02d/%02d", Recorded_time_ele.Year+1970, Recorded_time_ele.Month, Recorded_time_ele.Day); 
  datebuffer = Buffer;

  Serial.print("Recorded time: ");
  Serial.print(timebuffer);
  Serial.print(" ");
  Serial.print(datebuffer);
  Serial.println();
}