Can someone help me add in code to this example to 'SYNC' the RTC(ds1307)? The example works, I just don't know how to add the sync in. It's going to be a template for all my projects that will have a DS1307 and ENC28J60 on board.
#include <EtherCard.h> // https://github.com/jcw/ethercard
#include <Time.h> // http://www.arduino.cc/playground/Code/Time
#include <Timezone.h> // https://github.com/JChristensen/Timezone
// EtherCard
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "blog.cuyahoga.co.uk";
byte Ethernet::buffer[700];
// TimeZone : United States Eastern)
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240}; //UTC - 4 hours
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300}; //UTC - 5 hours
Timezone usEastern(usEDT, usEST);
void setup () {
Serial.begin(57600);
Serial.println("\n[NTP test]");
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) {
Serial.println( "Failed to access Ethernet controller");
}
if (!ether.dhcpSetup()) {
Serial.println("DHCP failed");
}
ether.printIp("Allocated IP: ", ether.myip);
ether.printIp("Gateway IP : ", ether.gwip);
ether.printIp("DNS IP : ", ether.dnsip);
if (!ether.dnsLookup(website)) {
Serial.println("DNS failed");
}
ether.printIp("Lookup IP : ", ether.hisip);
setTime(getNtpTime());
debug("Quick time check...");
}
void loop () {
ether.packetLoop(ether.packetReceive());
}
void debug(char* message) {
printTime();
Serial.print(" : ");
Serial.println(message);
}
void printTime() {
Serial.print(day());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.print(year());
Serial.print(" ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
uint8_t ntpServer[4] = { 192, 168, 1, 6 };
uint8_t ntpMyPort = 123;
unsigned long getNtpTime() {
unsigned long timeFromNTP;
const unsigned long seventy_years = 2208988800UL;
ether.ntpRequest(ntpServer, ntpMyPort);
while(true) {
word length = ether.packetReceive();
ether.packetLoop(length);
if(length > 0 && ether.ntpProcessAnswer(&timeFromNTP, ntpMyPort)) {
Serial.print("Time from NTP: ");
Serial.println(timeFromNTP);
return usEastern.toLocal(timeFromNTP - seventy_years);
}
}
return 0;
}