I just received my Ethernet W5100 shield.
I try to synchronize the DS1307 RTC via the NTP server.
The library Ethernet.h is installed, but during compilation of my program I get the error:
Test_syncing_DS1307_01.ino:16:22: fatal error: Ethernet.h: No such file or directory
compilation terminated.
Error compiling.
Who can test / adapt my program?
Otherwise: who has a working solution
/*
Trying to synchronize RTC with NetworkTimeProtocl.
Original post http://www.bajdi.com/syncing-ds1307-rtc-with-timeserver/
All other stuff removed to focuss to job
Hardware:
- Arduino Mega 2560 Rev.3
- Ethernet Schield W5100
- RTC DS1307
Software:
- Version 1.6.1
Edited: C.W.A. Baltus, 20160323
*/
//=============================================================================
//Libraries
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "RTClib.h"
#include <SPI.h>
#include <Wire.h>
unsigned int localPort = 8888; // local port to listen for UDP packets
byte timeServer[] = {81, 95, 126, 170}; // be.pool.ntp.org NTP server
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte pb[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
//Set external devices
RTC_DS1307 RTC;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,168, 14 }; //Edited 20160319
Server server(80);
const int chipSelect = 4; // CS for SD card
//=============================================================================
void setup()
{
Serial.begin(9600);
lcd.begin(20, 4); // set the LCD address to 0x20 for a 16 chars and 2 line display
Ethernet.begin(mac, ip);
server.begin();
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
} //End if
//Step 1: Set a wrong time
RTC.adjust(DateTime(1952, 7, 2, 10, 15, 0)); //1952, July 2
DateTime now = RTC.now();
//Compile strings
strDate = String(now.year());
strDate.concat("-");
strDate.concat(strNumber[now.month()]);
strDate.concat("-");
strDate.concat(strNumber[now.day()]);
strTime = strNumber[now.hour()];
strTime.concat(":");
strTime.concat(strNumber[now.minute()]);
strTime.concat(":");
strTime.concat(strNumber[now.second()]);
Serial.println(strDate & " " & strTime);
//Step 2: Wait 5 seconds
delay(5000(;
//Step 3: Adjust time with timeserver
DateTime now = RTC.now(); // get wrong time from RTC
int time = now.second();
Client client = server.available();
Udp.begin(localPort);
delay(250);
// send an NTP packet to a time server
sendNTPpacket(timeServer);
// wait to see if a reply is available
delay(1000);
if ( Udp.available() ) {
// read the packet into the buffer
Udp.readPacket(pb, NTP_PACKET_SIZE);
// NTP contains four timestamps with an integer part and a fraction part
// we only use the integer part here
unsigned long t1, t2, t3, t4;
t1 = t2 = t3 = t4 = 0;
for (int i=0; i< 4; i++)
{
t1 = t1 << 8 | pb[16+i];
t2 = t2 << 8 | pb[24+i];
t3 = t3 << 8 | pb[32+i];
t4 = t4 << 8 | pb[40+i];
}
// part of the fractional part
// could be 4 bytes but this is more precise than the 1307 RTC
// which has a precision of ONE second
// in fact one byte is sufficient for 1307
float f1,f2,f3,f4;
f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0;
f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0;
f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0;
f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0;
const unsigned long seventyYears = 2208988800UL;
t1 -= seventyYears;
t2 -= seventyYears;
t3 -= seventyYears;
t4 -= seventyYears;
// Adjust timezone and DST... in my case substract 4 hours for Chile Time
// or work in UTC?
t4 += (2 * 3600L); // Notice the L for long calculations!!
t4 += 1; // adjust the delay(1000) at begin of loop!
if (f4 > 0.4) t4++; // adjust fractional part, see above
RTC.adjust(DateTime(t4));
Serial.println("RTC synced");
//Show adjusted time
DateTime now = RTC.now();
//Compile strings
strDate = String(now.year());
strDate.concat("-");
strDate.concat(strNumber[now.month()]);
strDate.concat("-");
strDate.concat(strNumber[now.day()]);
strTime = strNumber[now.hour()];
strTime.concat(":");
strTime.concat(strNumber[now.minute()]);
strTime.concat(":");
strTime.concat(strNumber[now.second()]);
Serial.println(strDate & " " & strTime);
}
}
} //End setup
//=============================================================================
void loop()
{
//Empty
}
Test_syncing_DS1307_01.ino (4.37 KB)