Hello i did mange to find a RTC module. i have a DS3231 But i would still like to use the NTP server to update the clock. I searched online and i came across a site for the clock module and this is the sketch they had
/*
* NAME: NTP2RTC
* DATE: 2012-02-19
* URL: http://playground.arduino.cc/Main/DS1307OfTheLogshieldByMeansOfNTP
*
* PURPOSE:
* Get the time from a Network Time Protocol (NTP) time server
* and store it to the RTC of the adafruit logshield
*
* NTP is described in:
* http://www.ietf.org/rfc/rfc958.txt (obsolete)
* http://www.ietf.org/rfc/rfc5905.txt
*
* based upon Udp NTP Client, by Michael Margolis, mod by Tom Igoe
* uses the RTClib from adafruit (based upon Jeelabs)
* Thanx!
* mod by Rob Tillaart, 10-10-2010
*
* This code is in the public domain.
*
*/
/*
Dit is door BlackDog aangepastde code, er is gespeeld met de "delay" tijden!
Zet de goede NTP server aan!
DE serial poort snelheid staat op 115200
sketch uses the code from TimeNTP example to acquire a unix time stamp.
sets an RTC with that time stamp, and uses the RTC as synch provider for the Time library
*/
#include <Time.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include<Wire.h>;
//#include <DS1307RTC.h>
#include <DS3232RTC.h> //alternative drop in replacement //http://github.com/JChristensen/DS3232RTC
//the ds3232 library will help with alarm setting and temperature reading with ds3231rtc
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //controleer het MAC adres van je Ethernet Shield
// NTP Servers:
IPAddress timeServer(192, 168, 0, 1); // Blackdog Time Server
// IPAddress timeServer(192.79.237.14); // ntp1.nl.net, de tijdserver voor Nederland
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
const int timeZone = 2; // Central European Time
//const int timeZone = -5; // Eastern Standard Time (USA)
//const int timeZone = -4; // Eastern Daylight Time (USA)
//const int timeZone = -8; // Pacific Standard Time (USA)
//const int timeZone = -7; // Pacific Daylight Time (USA)
EthernetUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
void setup()
{
Serial.begin(115200); // let op deze poort snelheid, dit is geen 9600!!!
delay(1000);
Serial.println("TimeNTP Example");
if (Ethernet.begin(mac) == 0) {
// no point in carrying on, so do nothing forevermore:
while (1) {
Serial.println("Failed to configure Ethernet using DHCP");
delay(100);
}
}
Serial.print("IP number assigned by DHCP is ");
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
Serial.println("waiting for sync");
RTC.set(getNtpTime());
setSyncProvider(RTC.get);
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
if (timeStatus() == timeSet) {
digitalClockDisplay();
}
else {
Serial.println("The time has not been set. Please run the Time");
Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
Serial.println();
delay(100);
}
delay(100);
}
void digitalClockDisplay(){
tmElements_t tm;
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.println();
// Serial.print(day());
// Serial.print(" ");
// Serial.print(month());
// Serial.print(" ");
// Serial.print(year());
// Serial.println();
// Serial.print("Unix Time: ");
// Serial.println(makeTime(tm));
}
void printDigits(int digits){
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
I get a error from it i don't understand. If i select the uno board in the board menu i get no errors but sense i current do not have a uno board I'm using a m0 pro board i get this error message.
Arduino: 1.8.5 (Windows 10), Board: "Arduino M0 Pro (Programming Port)"
Build options changed, rebuilding all
In file included from C:\Users\Programmin-Computer\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.1.0/CMSIS/Device/ATMEL/samd21/include/samd21.h:69:0,
from C:\Users\Programmin-Computer\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.1.0/CMSIS/Device/ATMEL/samd.h:105,
from C:\Users\Programmin-Computer\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.1.0/CMSIS/Device/ATMEL/sam.h:470,
from C:\Users\Programmin-Computer\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.6.19\cores\arduino/Arduino.h:48,
from sketch\sketch_dec05a.ino.cpp:1:
C:\Users\Programmin-Computer\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.1.0/CMSIS/Device/ATMEL/samd21/include/samd21g18a.h:487:38: error: expected ')' before '*' token
#define RTC ((Rtc *)0x40001400UL) /**< \brief (RTC) APB Base Address */
^
C:\Users\Programmin-Computer\Documents\Arduino\libraries\DS3232RTC/DS3232RTC.h:116:18: note: in expansion of macro 'RTC'
extern DS3232RTC RTC;
^
C:\Users\Programmin-Computer\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.1.0/CMSIS/Device/ATMEL/samd21/include/samd21g18a.h:487:38: error: expected ')' before '*' token
#define RTC ((Rtc *)0x40001400UL) /**< \brief (RTC) APB Base Address */
^
C:\Users\Programmin-Computer\Documents\Arduino\libraries\DS3232RTC/DS3232RTC.h:116:18: note: in expansion of macro 'RTC'
extern DS3232RTC RTC;
^
C:\Users\PROGRA~1\AppData\Local\Temp\arduino_modified_sketch_18948\sketch_dec05a.ino: In function 'void setup()':
sketch_dec05a:77: error: request for member 'set' in '1073746944u', which is of pointer type 'Rtc*' (maybe you meant to use '->' ?)
RTC.set(getNtpTime());
^
sketch_dec05a:78: error: request for member 'get' in '1073746944u', which is of pointer type 'Rtc*' (maybe you meant to use '->' ?)
setSyncProvider(RTC.get);
^
exit status 1
request for member 'set' in '1073746944u', which is of pointer type 'Rtc*' (maybe you meant to use '->' ?)
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I'm not sure what to do can someone please help me out?