Difference between revisions of "Arduino: RTC DS1302 NTP Sync"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "// time ntp RTC DS1302 set #include <Time.h> #include <SPI.h> #include <Ethernet.h> #include <EthernetUdp.h> #include <DS1302.h> // Init the DS1302 // DS1302 rtc([CE/RST], [...")
 
 
Line 1: Line 1:
// time ntp RTC DS1302 set
+
==Code==
  
#include <Time.h>
+
// time ntp RTC DS1302 set
#include <SPI.h>
+
#include <Ethernet.h>
+
#include <Time.h>
#include <EthernetUdp.h>
+
#include <SPI.h>
#include <DS1302.h>
+
#include <Ethernet.h>
 
+
#include <EthernetUdp.h>
// Init the DS1302
+
#include <DS1302.h>
// DS1302 rtc([CE/RST], [I/O], [CLOCK]);
+
DS1302 rtc(8, 7, 6);
+
// Init the DS1302
 
+
// DS1302 rtc([CE/RST], [I/O], [CLOCK]);
// Ethernet library configuration
+
DS1302 rtc(8, 7, 6);
byte mac[] = { 0x24, 0x7B, 0xA2, 0x4A, 0x52, 0x10 };
+
IPAddress timeServer(128, 138, 140, 44 ); // time.nist.gov
+
// Ethernet library configuration
 
+
byte mac[] = { 0x24, 0x7B, 0xA2, 0x4A, 0x52, 0x10 };
unsigned int localPort = 8888;
+
IPAddress timeServer(128, 138, 140, 44 ); // time.nist.gov
const int NTP_PACKET_SIZE= 48;     
+
byte packetBuffer[NTP_PACKET_SIZE];  
+
unsigned int localPort = 8888;
EthernetUDP Udp;                   
+
const int NTP_PACKET_SIZE= 48;     
 
+
byte packetBuffer[NTP_PACKET_SIZE];  
void setup() {
+
EthernetUDP Udp;                   
  // Set the clock to run-mode, and disable the write protection
+
  rtc.halt(false);
+
void setup() {
  rtc.writeProtect(false);
+
  // Set the clock to run-mode, and disable the write protection
 
+
  rtc.halt(false);
  // Setup Communication
+
  rtc.writeProtect(false);
  Serial.begin(9600);
+
  Ethernet.begin(mac);
+
  // Setup Communication
  Udp.begin(localPort);
+
  Serial.begin(9600);
 
+
  Ethernet.begin(mac);
 
+
  Udp.begin(localPort);
  Serial.print("get time");
+
  setSyncInterval(10); // Set seconds between re-sync
+
  Serial.print("get time");
  getNtpTime;
+
  setSyncInterval(10); // Set seconds between re-sync
 
+
  getNtpTime;
  // The following lines can be commented out to use the values already stored in the DS1302
+
  rtc.setDOW(WEDNESDAY);        // Set Day-of-Week to MONDAY
+
  // The following lines can be commented out to use the values already stored in the DS1302
  // rtc.setTime(13, 0, 0);    // Set the time to 12:00:00 (24hr format)
+
  rtc.setDOW(WEDNESDAY);        // Set Day-of-Week to MONDAY
  rtc.setDate(30, 5, 2018);  // Set the date to Feb 2, 2016
+
  // rtc.setTime(13, 0, 0);    // Set the time to 12:00:00 (24hr format)
}
+
  rtc.setDate(30, 5, 2018);  // Set the date to Feb 2, 2016
 
+
}
void loop() {
+
  setSyncProvider(getNtpTime);
+
void loop() {
 
+
  setSyncProvider(getNtpTime);
  rtc.setTime(hour()+7, minute(), second());
+
 
+
  rtc.setTime(hour()+7, minute(), second());
  // Send Day-of-Week
+
  Serial.print(rtc.getDOWStr());
+
  // Send Day-of-Week
   Serial.print(" ");
+
  Serial.print(rtc.getDOWStr());
 +
  Serial.print(" ");
 +
    
 +
  // Send date
 +
  Serial.print(rtc.getDateStr());
 +
  Serial.print(" -- ");
 
    
 
    
  // Send date
+
  // Send time
  Serial.print(rtc.getDateStr());
+
  Serial.println(rtc.getTimeStr());
  Serial.print(" -- ");
+
 
 +
  getNtpTime;
 +
  // delay(1000);
 +
 +
 +
/*-------- NTP code ----------*/
 
   
 
   
  // Send time
+
unsigned long getNtpTime()
  Serial.println(rtc.getTimeStr());
+
{
 
+
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  getNtpTime;
+
    delay(500);
  // delay(1000);
 
}
 
 
 
/*-------- NTP code ----------*/
 
 
 
unsigned long getNtpTime()
 
{
 
  sendNTPpacket(timeServer); // send an NTP packet to a time server
 
    delay(500);
 
 
    
 
    
    if ( Udp.parsePacket() ) {
+
    if ( Udp.parsePacket() ) {
    Udp.read(packetBuffer,NTP_PACKET_SIZE);
+
      Udp.read(packetBuffer,NTP_PACKET_SIZE);
    unsigned long hi = word(packetBuffer[40], packetBuffer[41]);
+
      unsigned long hi = word(packetBuffer[40], packetBuffer[41]);
    unsigned long low = word(packetBuffer[42], packetBuffer[43]);
+
      unsigned long low = word(packetBuffer[42], packetBuffer[43]);
    unsigned long secsSince1900 = hi << 16 | low;  
+
      unsigned long secsSince1900 = hi << 16 | low;  
    const unsigned long seventyYears = 2208988800UL;     
+
      const unsigned long seventyYears = 2208988800UL;     
    unsigned long epoch = secsSince1900 - seventyYears;
+
      unsigned long epoch = secsSince1900 - seventyYears;
    return epoch;
+
      return epoch;
  }
+
  }
  return 0; // return 0 if unable to get the time
+
  return 0; // return 0 if unable to get the time
}
+
}
 
+
unsigned long sendNTPpacket(IPAddress& address)
+
unsigned long sendNTPpacket(IPAddress& address)
{
+
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);  // set all bytes in the buffer to 0
+
  memset(packetBuffer, 0, NTP_PACKET_SIZE);  // set all bytes in the buffer to 0
 
+
  // Initialize values needed to form NTP request
+
  // Initialize values needed to form NTP request
  packetBuffer[0] = B11100011;  // LI, Version, Mode
+
  packetBuffer[0] = B11100011;  // LI, Version, Mode
  packetBuffer[1] = 0;    // Stratum
+
  packetBuffer[1] = 0;    // Stratum
  packetBuffer[2] = 6;    // Max Interval between messages in seconds
+
  packetBuffer[2] = 6;    // Max Interval between messages in seconds
  packetBuffer[3] = 0xEC;  // Clock Precision
+
  packetBuffer[3] = 0xEC;  // Clock Precision
  // bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
+
  // bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
  packetBuffer[12]  = 49;  // four-byte reference ID identifying
+
  packetBuffer[12]  = 49;  // four-byte reference ID identifying
  packetBuffer[13]  = 0x4E;
+
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
+
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
+
  packetBuffer[15]  = 52;
 
+
  // send the packet requesting a timestamp:
+
  // send the packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
+
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
+
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
+
  Udp.endPacket();
}
+
}

Latest revision as of 16:48, 30 May 2018

Code

// time ntp RTC DS1302 set

#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <DS1302.h>

// Init the DS1302
// DS1302 rtc([CE/RST], [I/O], [CLOCK]);
DS1302 rtc(8, 7, 6);

// Ethernet library configuration
byte mac[] = { 0x24, 0x7B, 0xA2, 0x4A, 0x52, 0x10 };
IPAddress timeServer(128, 138, 140, 44 ); // time.nist.gov

unsigned int localPort = 8888;
const int NTP_PACKET_SIZE= 48;     
byte packetBuffer[NTP_PACKET_SIZE]; 
EthernetUDP Udp;                   

void setup() {
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);

  // Setup Communication
  Serial.begin(9600);
  Ethernet.begin(mac);
  Udp.begin(localPort);

  Serial.print("get time");
  setSyncInterval(10); // Set seconds between re-sync
  getNtpTime;

  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(WEDNESDAY);        // Set Day-of-Week to MONDAY
  // rtc.setTime(13, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(30, 5, 2018);   // Set the date to Feb 2, 2016
}

void loop() {
  setSyncProvider(getNtpTime);

  rtc.setTime(hour()+7, minute(), second());

  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
 
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
 
  // Send time
  Serial.println(rtc.getTimeStr());
  
  getNtpTime;
  // delay(1000);
}  

/*-------- NTP code ----------*/

unsigned long getNtpTime()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
    delay(500);
  
    if ( Udp.parsePacket() ) {
     Udp.read(packetBuffer,NTP_PACKET_SIZE);
     unsigned long hi = word(packetBuffer[40], packetBuffer[41]);
     unsigned long low = word(packetBuffer[42], packetBuffer[43]);
     unsigned long secsSince1900 = hi << 16 | low; 
     const unsigned long seventyYears = 2208988800UL;     
     unsigned long epoch = secsSince1900 - seventyYears;
     return epoch;
  }
  return 0; // return 0 if unable to get the time
}

unsigned long sendNTPpacket(IPAddress& address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);  // set all bytes in the buffer to 0

  // Initialize values needed to form NTP request
  packetBuffer[0] = B11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum
  packetBuffer[2] = 6;     // Max Interval between messages in seconds
  packetBuffer[3] = 0xEC;  // Clock Precision
  // bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
  packetBuffer[12]  = 49;  // four-byte reference ID identifying
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // send the packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}