2013/08/21

NTP Server Synchronized DS1307 RTC Analog Data Logger


This sketch combine analog sensor data logger on Aug. 15, 2013 and NTP server synchronized DS1307 RTC using TimeAlarm library on Aug. 17, 2013.

The shetch use fixed IP, so DHCP server is not needed in the local area network.

Because the RTC is synchronized with NTP server, so the data logger keeps the time within 5 seconds. The NTP sync may fail sometimes depending on the network conditions.

Sketch

/*
 * NTPSynchronizedAnalogDataLogger.ino
 *
 * This sketch calls alarm functions at 7:30 am and at 7:30 pm (19:30)
 * and sync DS1307, Arduino system time 
 *
 * At startup the system time read from DS1307, then both sync with NTP 
 */

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SD.h>
#define analogSensorStart 3 // sensor connect to A3
#define analogSensorEnd 3 // sensor connect to A3

// Enter a MAC address for your controller bellow.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 1, 177};
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(140, 112, 2, 188); // ntp2.ntu.edu.tw NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// timeZoneOffset = (Time Zone) * 3600L eg. (+8) * 3600L = 28800L for Taipei, Taiwan
const long timeZoneOffset = 28800L; 
// sync to NTP server every "ntpSyncTime" seconds, set to 1 hour or more to be reasonable
unsigned long ntpSyncTime = 21600;
// adjust the sync latency with computer NTP client in seconds 
unsigned int syncLatency = 2;

// sd card variables
File file; // test file
const uint8_t SD_CS = 4; // SD chip select
String file_name = ""; // file name should not prefix with "prefix_word"
char fn[] = "MMDDHHMM.CSV";
int i=0;
int displayAtSecond = 0;
time_t t;

//------------------------------------------------------------------------------
// call back for file timestamps 
void dateTime(uint16_t* date, uint16_t* time) {
  time_t timeStamp = now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(year(timeStamp), month(timeStamp), day(timeStamp));

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(hour(timeStamp), minute(timeStamp), second(timeStamp));

//------------------------------------------------------------------------------

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  setSyncProvider(RTC.get); // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);  
  // display RTC time
  Serial.print(year());
  Serial.print('-');
  Serial.print(month());
  Serial.print('-');
  Serial.print(day());
  Serial.print(' ');
  Serial.print(hour());
  Serial.print(':');
  Serial.print(minute());
  Serial.print(':');
  Serial.print(second());
  Serial.println(" --- RTC Time");
  // create the alarms 
  Alarm.alarmRepeat(7,30,0, ntpSyncDS1307);  // 7:30am every day
  Alarm.alarmRepeat(19,30,0, ntpSyncDS1307);  // 7:30pm every day 
  
  // start Ethernet and UDP
  Ethernet.begin(mac, ip);
  Udp.begin(localPort); 
  // Serial.println("connect to ntp server");
  ntpSyncDS1307();
  
  // process file name string
  t = now();
  if (month(t) < 10) {
      file_name = String(file_name + '0' + String(month(t), DEC));
    }
    else {
    file_name = String(file_name +String(month(t), DEC));
    }
  if (day(t) < 10) {
      file_name = String(file_name + '0' + String(day(t), DEC));
    }
    else {
    file_name = String(file_name +String(day(t), DEC));
    }
  if (hour(t) < 10) {
      file_name = String(file_name + '0' + String(hour(t), DEC));
    }
    else {
    file_name = String(file_name +String(hour(t), DEC));
    }
  if (minute(t) < 10) {
      file_name = String(file_name + '0' + String(minute(t), DEC));
    }
    else {
    file_name = String(file_name +String(minute(t), DEC));
    }

  file_name = String(file_name + ".CSV");

  for (i=0;i<=file_name.length();i++) {
    fn[i] = file_name.charAt(i);
  }
  Serial.print("File Name: ");
  Serial.println(fn);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  if (!SD.begin(SD_CS)) {
    Serial.println("SD failed");
    // while(1);
  }
}

void loop(){
  t = now();
  if (displayAtSecond != second(t)) {
    analogSensorDataLogger();
    displayAtSecond = second(t);
  }
  Alarm.delay(100); // wait 1/10 second between cycles
}

// functions to be called when an alarm triggers:
void ntpSyncDS1307() {
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  // wait to see if a replay is available
  delay(1000);
  if (Udp.parsePacket()) {
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
    // the timstamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, extract the two words:
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900)
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800L;
    // substract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears + timeZoneOffset + syncLatency;
    setTime(epoch);
    RTC.set(epoch);
    // output time and "Sync OK" message every sync 
    Serial.print(year());
    Serial.print('-');
    Serial.print(month());
    Serial.print('-');
    Serial.print(day());
    Serial.print(' ');
    Serial.print(hour());
    Serial.print(':');
    Serial.print(minute());
    Serial.print(':');
    Serial.print(second());
    Serial.print(' ');
    Serial.println("Sync OK");
  }
}

// send an NTP request to the time server at the given address
unsigned long 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 clodk
  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);
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

void analogSensorDataLogger() { 
  String data_string = "";
  data_string = String(year(t), DEC);
  data_string += "/";
  if (month(t) < 10) {
      data_string = String(data_string + '0' + String(month(t), DEC));
    }
    else {
    data_string = String(data_string +String(month(t), DEC));
    }
  data_string += "/";
  if (day(t) < 10) {
      data_string = String(data_string + '0' + String(day(t), DEC));
    }
    else {
    data_string = String(data_string +String(day(t), DEC));
    }
  data_string += " ";
  if (hour(t) < 10) {
      data_string = String(data_string + '0' + String(hour(t), DEC));
    }
    else {
    data_string = String(data_string +String(hour(t), DEC));
    }
  data_string += ":";
  if (minute(t) < 10) {
      data_string = String(data_string + '0' + String(minute(t), DEC));
    }
    else {
    data_string = String(data_string +String(minute(t), DEC));
    }
  data_string += ":";  
  if (second(t) < 10) {
      data_string = String(data_string + '0' + String(second(t), DEC));
    }
    else {
    data_string = String(data_string +String(second(t), DEC));
    }
  data_string += ",";

  //read sensor value from A0-A3 and append to the string
  for (int analogPin = analogSensorStart; analogPin <= analogSensorEnd; analogPin++)
    {
    int sensor = analogRead(analogPin);
    data_string += String(sensor);
    if (analogPin < analogSensorEnd) {
      data_string += ",";
      }
    }
  
  file = SD.open(fn, FILE_WRITE);
  if (file) {
    file.println(data_string);
    file.close();
    Serial.println(data_string);
    }
    else {
      Serial.print("error opening ");
      Serial.println(fn);
    }
}

3 comments:

  1. What are the various benefits of using a NTP Synchronized Analog Data Logger?

    Thanks
    Silvester Norman

    Change MAC Address

    ReplyDelete
  2. I just want the data logger on remote site can keep it's time stamp as accurate as possible, as the RTC may differ one second or two per day. Even more, on the coming article, I want the data logger on remote site with file download server function, accessed using web browser.

    ReplyDelete
    Replies
    1. In short you may provide the benefits of using NTP Synchronized Analog Data Logger in the next blog.It is good so see your concern to solve the query.Thanks fpr replying.

      Thanks
      Silvester Norman

      Change MAC Address

      Delete