DS1307 and UDP NTP

Hello everyone!

Can someone please tell me is there a simple method for setting the time on a RTC using UNIX timestamp? i want to sync the DS1307 with a NTP server every given time and i will be using UNIX timestamp in my project so i don’t need to convert the seconds to human readable time.
With this library i saw the RTC.adjust function but it seems you have to put in the hours, minutes,seconds seperatly… i want to sync it with the UNIX tmestamp…
any ideas?

You can use the command RTC.set() and RTC.get() in the DS1307 library and set and read the RTC with Unix time.

The library also links to Time.h and Wire.h. If you know how to get the unix time stamp from an NTP server and into the Arduino you should be able to do this.

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

void setup() {
  
  Serial.begin(9600);
 
  Serial.println("Setting Unix time on RTC");
  Serial.println("1388534400  Jan 1, 2014 00:00:00");
  Serial.println();
  
  time_t unixTimeStamp = 1388534400; //Jan 1, 2014 00:00:00
  RTC.set(unixTimeStamp);
  
  
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    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();
    Serial.println("Unix Time");
    Serial.println(RTC.get());
  } 
  delay(1000);
}

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

i didn't know that you can call RTC.set and use UNIX time to set... ok Thank you very much i will give it a try as soo as i get the ethernet shield :) Thank you!