TimeLib & DS1307RTC

This code is focused on demonstrating how to use the TimeLib & DS1307RTC library to provide basic output without the use of delay() to implement seconds counting.   A function is also included to alternatively return a ISO 8601 datetime string.  
/*
 * TimeLib & DS1307RTC library
 * 
 * Mark Kiehl
 * 
 * Portions derived from:
 *  https://github.com/PaulStoffregen/DS1307RTC
 *  Adafruit
*/

////////////////////////////////////////////////////////////////////////
// TimeLib & DS1307RTC library
// library source: https://github.com/PaulStoffregen/DS1307RTC
// Useful info:  https://www.pjrc.com/teensy/td_libs_DS1307RTC.html
#include 
#include 
#include 
// Define a 32 bit time structure
tmElements_t tm;
int hours = 0;
int minutes = 0;
int seconds = 0;
////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
//  ISO 8601 datetime function
//
//  time zone offset - set this to the offset in seconds to your local time
//  Eastern time zone difference is -5 hours; -5 h * (3600s/hr) = -18000
const long EST_UTC_offset = -18000L;

//  Global char variable to hold the ISO 8601 formatted datetime.  
char isoDateTime[25];

void SetIsoDateTime(long TZoffset) {
  //  Update global char variable isoDateTime with the RTC values
  //  stored in the 32 bit time structure tm (from tmElements_t).
  //  isoDateTime is a ISO 8601 formatted date/time string with the 
  //  specified time zone offset TZoffset.
  //
  //  TZ_offset = # seconds from UTC to your local time.
  //    Eastern time zone is -5 hrs; -5 h * (3600s/h) = -18000
  //
  //  https://github.com/pklaus/Arduino-Logger/blob/master/TimeNTP_UdpUpdated_SdFatLogger/TimeNTP_UdpUpdated_SdFatLogger.ino
  //
  sprintf(isoDateTime, "%4d-%02d-%02dT%02d:%02d:%02d%+05d",
     tmYearToCalendar(tm.Year), tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second, TZoffset/36 );
}
/////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);

  // Read the date & time into tm
  if (RTC.read(tm)) {
    Serial.println("DS1307RTC read successful");
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    while(true) {};
  }

  /*
  // Test setting the RTC
  tm.Hour = 21;
  //tm.Minute = 59;
  //tm.Second = 59;
  //tm.Day = 1;
  //tm.Month = 28;
  //tm.Year = 2017;
  if (!RTC.write(tm)) {
    Serial.println("RTC set/update FAILED!");
    while(true) {};
  }
  */
}

void loop() {
  
  // RTC.Read() gets the current date/time from the RTC module.
  // You need to check this result is true, otherwise you only
  // read the contents from the Time library and not what is in
  // the RTC.
  if (RTC.read(tm)) {
    // Print the time & date to the serial monitor using
    // either printDatetime() or SetIsoDateTime() 
    printDatetime();
    //OR:
    /*
    if (seconds != tm.Second) {
      seconds = tm.Second;
      SetIsoDateTime(EST_UTC_offset);
      Serial.print("ISO 8601:  "); 
      Serial.println(isoDateTime);
    } //(seconds != tm.Second)
    */
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    while(true) {};
  }

}

void printDatetime() {
  if ((hours != tm.Hour) || (minutes != tm.Minute)) {
    // Get the time from the RTC
    hours = tm.Hour;
    minutes = tm.Minute;
  } // if ((hours != tm.Hour) || (minutes != tm.Minute))
  
  if (seconds != tm.Second) {
    seconds = tm.Second;
    print2digits(hours);
    Serial.write(":");
    print2digits(minutes);
    Serial.write(":");
    print2digits(seconds);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  }  //(seconds != tm.Second)

}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.