Showing posts with label NTP. Show all posts
Showing posts with label NTP. Show all posts

2017/05/14

NTP Server Synchronized LCD DS1307 RTC Clock


Requirements:

1. Arduino Uno R3
2. RTC (DS1307) Sensor Shield
3. W5100 Ethernet Shield
4. LCD Keypad Shield

Schetch:

/*
 * NTPSynchronizedRTC20170514.ino
 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

char dayOfWeek[9][4] = {"", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
time_t t;
int displayAtSecond;

// timeZoneOffset = (Time Zone) * 3600L eg. (+8) * 3600L = 28800L for Taipei, Taiwan
const long timeZoneOffset = 28800L; 
// sync to NTP server every "ntpSyncInterval" seconds, set to 1 hour or more to be reasonable
unsigned long ntpSyncInterval = 3600;
// adjust the sync latency with computer NTP client in seconds 
unsigned long syncLatency = 0;

// 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};
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(192, 168, 1, 123); // LAN 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;

// keep track of how long ago we updated the NTP server
unsigned long ntpLastUpdate = 0;

void setup()  {
  Serial.begin(115200);
  lcd.begin(16,2);
  lcd.print("*cheaphousetek*");
  lcd.setCursor(0,1);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     lcd.print("Unable to sync");
  else
     lcd.print("Sync system time ");
  displayAtSecond = second();
  delay(2000);
  lcd.clear();
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    lcd.setCursor(0,1);
    lcd.print("DHCP failed");
    for (;;);
  }
  Udp.begin(localPort);
}

void loop()
{
  if ((now() - ntpLastUpdate) >= ntpSyncInterval) {
    ntpSyncDS1307();
    // clear seconds displayed once a second when sync is needed, cause seconds blink to notify checking the network status
    lcd.setCursor(9, 1);
    lcd.print("       ");
  }
  
  // for LCD shield to disp date, day of the week, time and temperature once a second
  t = now();
  if (displayAtSecond != second(t)) { 
    digitalClockDisplay(); 
    displayAtSecond = second(t); 
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  dateDisplay();
  weekdayDisplay();
  timeDisplay();
  // display seconds since last NTP update
  lcd.setCursor(9, 1);
  lcd.print(now()-ntpLastUpdate);
     // Serial.print(now());
     // Serial.print("    ");
     // Serial.println(ntpLastUpdate);
}

void dateDisplay() {
  lcd.setCursor(0,0);
  lcd.print(year(t));
  lcd.print('-');
  if (month(t) < 10) {
    lcd.print('0');
  }
  lcd.print(month(t));
  lcd.print('-');
  if (day(t) < 10) {
    lcd.print('0');
  }
  lcd.print(day(t));
  // lcd.print(' ');
}

void weekdayDisplay() {
  lcd.setCursor(11,0);
  lcd.print(dayOfWeek[weekday()]);
}

void timeDisplay() {
  lcd.setCursor(0,1);
  if (hour(t) < 10) {
    lcd.print('0');
  }
  lcd.print(hour(t));
  lcd.print(':');
  if (minute(t) < 10) {
    lcd.print('0');
  }
  lcd.print(minute(t));
  lcd.print(':');
  if (second(t) < 10) {
    lcd.print('0');
  }
  lcd.print(second(t));
  // lcd.print(' ');
}

void ntpSyncDS1307() {
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  // wait to see if a replay is available
  delay(100);
  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);
    ntpLastUpdate = now();
    // clear seconds displayed once ntp sync succeeded
    lcd.setCursor(9, 1);
    lcd.print("       ");
  }
}

// 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 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();
}

2016/07/31

IPs And Domain Names of NTP Servers Available Publicly In Taiwan

From time to time I need an NTP server available publicly in Taiwan for reference to compare with my homebrew MakerNTP (posted on Dec. 24, 2015) Stratum 1 GPS-based NTP server, so I list some useful NTP servers.

1. time.stdtime.gov.tw: 118.163.81.61

2. tick.stdtime.gov.tw: 118.163.81.62

3. watch.stdtime.gov.tw: 118.163.81.63

4. tock.stdtime.gov.tw: 211.22.103.157

5. clock.stdtime.gov.tw: 211.22.103.158

6. tw.pool.ntp.org: A. 103.18.128.60 B.  117.56.223.235 C. 59.124.29.241 D. 59.125.122.217


2015/12/24

MakerNTP - A Low Cost Arduino Mega2560 GPS-based Stratum 1 NTP Server

My homebrew low locst (not more than 120 USD for retail) MakerNTP is now in operation for about one month without any problem in my home environment. The features of MakerNTP are:

Protocol: SNTP V4
Performance: 40 requests/second
Ethernet Port: 1
Default IP: 192.168.1.123
Power Supply: 9V (recommended) / 5V (USB)
Enclosure: ABS
Application: home automation, security surveillance ......

NOTE: Most SNTP clients will generally sync your system time to within 100 milliseconds of the correct time - often within 10 milliseconds. Of course, the accuracy is highly dependent on the accuracy of the server that it is connecting to.

Photos



2015/11/03

Convert Millisecond (Decimal Fraction) to 32-bits Binary Using Arduino

The function millis2bin() can be used to convert millisecond to Network Time Protocol (NTP) timestamp fraction (32-bits unsigned long) used by both NTP servers and clients.

Code

/*
  Program for convert millisecond (decimal fraction) to 32-bits binary 
  By Befun Hung on Nov. 2, 2015
 */

void setup() {   
  
  Serial.begin(115200);
  
  int i;
  unsigned long tempval=1;
  for (i=0;i<32;i++) {
    Serial.print(i);
    Serial.print(" ");
    Serial.print(tempval << (31-i));
    Serial.print(" ");
    tempval = 1;
    Serial.println(tempval << (31-i),BIN);
  }
  for (i=0;i<1000;i++) {
    Serial.print(i);
    Serial.print(" ");
    Serial.println(millis2bin((float) i/1000),BIN);
    // millis2bin((float) i/1000);
  }
}

unsigned long millis2bin( float fraction) {
  int j=0;
  unsigned long return_value = 0;
  unsigned long one = 1;
  
  while (fraction != 0 && j < 32) {
    fraction = fraction * 2.0;
    if (fraction >= 1.0) {
      return_value = return_value | (one << (31 - j));
      fraction = fraction -1;
    }
    j++;
  }
  // Serial.print(return_value);
  // Serial.print(" ");
  // Serial.println(return_value,BIN);
  return return_value;
}

// the loop routine runs over and over again forever:
void loop() {
}

Serial Monitor Output


2014/11/09

Arduino RF Wireless Message Display Using I2C LCD Keypad Shield

The function of this sketch is same as the sketch posted on May. 11, 2014, but using I2C LCD Keypad Shield as the output device.

Sketch

/*
SimpleReceiveI2CLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
Modified by Befun Hung on Nov. 09, 2014 
Use I2C LCD Keypad Shield as the output
*/

#include <VirtualWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// #define BACKLIGHT_PIN 13
LiquidCrystal_I2C lcd(0x20); //Set the LCD I2C address
// LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;

void setup()
{
  // pinMode(BACKLIGHT_PIN, OUTPUT);
  // digitalWrite(BACKLIGHT_PIN, HIGH);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2); //initialize the lcd
  // Print a message to the LCD.
  lcd.home(); //go home
  lcd.print("Device is ready!");
  // 
  Serial.begin(9600);
  Serial.println("Device is ready");
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
}
void loop()
{
  if (vw_get_message(message, &messageLength)) // Non-blocking
  {
    lcd.clear();
    Serial.print(counter);
    Serial.print(": ");
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 0);
    lcd.print(counter);
    lcd.print(":");
    lcd.setCursor(0,1);
    for (int i = 0; i < messageLength; i++)
    {
      Serial.write(message[i]);
      lcd.print(char(message[i]));
    }
    Serial.println();
    counter++;
  }
}

2014/05/11

Arduino RF Wireless Message Display

As I am building the RF wireless power socket for home automation, I need a stand along tool to display any wireless received message. During the lab, I use Arduino serial monitor to monitor the message received once a second, error always happens at different time point as following screen shot shows. After disconnecting the Arduino Uno from PC to monitor message received by using LCD keypad shield as a stand along system, the error condition gone.
The RF wireless message display can be used to display the time on the NTP synchronized RTC clock (my projects during Aug 2013), so no additional time adjustment at display side is needed.


Parts Used

1. Arduino Uno R3
2. cheaphousetek LCD Keypad Shield
3. RF Receiver (described in Arduino Cookbook 14.1)

Connection

1. Stack LCD Key Keypad Upon Arduino Uno R3
2. RF Receiver Data Pin - Arduino Uno D11
3. RF Receiver Vcc - Arduino Uno 5V
4. RF Receiver Gnd - Arduino Uno Gnd

Photo


The sketch runs correctly with output to LCD shield for 247,610 times.



Sketch

/*
SimpleReceiveLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include <VirtualWire.h>
// include the library code:
#include <LiquidCrystal.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  lcd.clear();
  Serial.begin(9600);
  Serial.println("Device is ready");
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
}
void loop()
{
  if (vw_get_message(message, &messageLength)) // Non-blocking
  {
    lcd.clear();
    Serial.print(counter);
    Serial.print(": ");
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 0);
    lcd.print(counter);
    lcd.print(":");
    lcd.setCursor(0,1);
    for (int i = 0; i < messageLength; i++)
    {
      Serial.write(message[i]);
      lcd.print(char(message[i]));
    }
    Serial.println();
    counter++;
  }
}

2013/10/11

NTP Server Synchronized DS1307 RTC Analog Datalogger With File Download Server

The sketch is an extension based on NTP Synchronized Analog Datalogger post on Aug. 21, 2013 by adding web interface file download server function. All datalogger files are saved in the root directory of the microSD card. With the network infrastructure settled and router proper configured, the datalogger files on the microSD can be download by any device with web browser built in from anywhere.

Requirements
1. Arduino Mega2560 R3
2. W5100 Ethernet Shield
3. RTC Sensor Shield (designed to stack on Arduino Uno cannot stack on Mega2560)
4. LM35 Temperature Sensor Breakout connect to RTC Sensor Shield A3

Connection


File Download Server Screen




Sketch

/*
 * NTPSynchronizedRTCAnalogDataLoggerWithFileDownloadServer.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 
 *
 * Modified by Befun Hung on Oct. 11, 2013 based on NTPSyncronizedAnalogDataLogger.ino
 * by adding web interface file download server function, 
 * so that files can be downloaded from the remote site the data logger installed
 */

#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;

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

// for list files
File root;
File webFile;

//------------------------------------------------------------------------------
// 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); 
  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);
  }
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  if (!SD.begin(SD_CS)) {
    Serial.println("SD failed");
    // while(1);
  }

  // start the web server:
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  
  root = SD.open("/");
}

void loop(){
  webServer();
  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);
    }
}

#define BUFSIZ 100
void webServer()
{
  char clientline[BUFSIZ];
  int index = 0;
  
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    
    // reset the input buffer
    index = 0;
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        // If it isn't a new line, add the character to the buffer
        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          index++;
          // are we too big for the buffer? start tossing out data
          if (index >= BUFSIZ) 
            index = BUFSIZ -1;
          
          // continue to read more data!
          continue;
        }
        
        // got a \n or \r new line, which means the string is done
        clientline[index] = 0;
        
        // Print it out for debugging
        Serial.print("clientline: ");
        Serial.println(clientline);
        
        // Look for substring such as a request to get the root file
        if (strstr(clientline, "GET / ") != 0) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          // print all the files, use a helper to keep it clean
          client.println("<h2>Files:</h2>");
          int numTabs;
          root.rewindDirectory();
          while (true) {
            File entry = root.openNextFile();
            if (! entry) {
              // no more files
              client.println("** no more files **");
              client.println("<br />");
              break;
            }
            client.print("<a href=\"");
            client.print(entry.name());
            client.print("\">");
            client.print(entry.name());
            client.print("</a>");
            client.print(" ");
            client.print(entry.size(), DEC);
            client.print("<br />");
            entry.close();
          }  
        } else if (strstr(clientline, "GET /") != 0) {
          // this time no space after the /, so a sub-file!
          char *filename;
          
          filename = clientline + 5; // look after the "GET /" (5 chars)
          // a little trick, look for the " HTTP/1.1" string and 
          // turn the first character of the substring into a 0 to clear it out.
          (strstr(clientline, " HTTP"))[0] = 0;
          
          // print the file we want
          Serial.print("filename: ");
          Serial.print(filename);
          Serial.println(" Opened!");
                    
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/plain");
          client.println();
          
          webFile = SD.open(filename);
          if (webFile) {
            while (webFile.available()) {
              client.write(webFile.read());
            }
            webFile.close();
          }
        } else {
          // everything else is a 404
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>File Not Found!</h2>");
        }
        break;
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

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);
    }
}

2013/08/17

NTP Server Synchronized DS1307 RTC Using TimeAlarms Library

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 Arduino system time is synchronized with DS1307. The sketch output time and "Sync OK" message to Arduino serial monitor at every successful synchronization.

A DHCP server is needed.

Requirements
1. Arduino Uno R3
2. W5100 Ethernet Shield
3. RTC Sensor Shield

Schetch

/*
 * TimeAlarmNTPSyncRTC.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 time is sync with DS1307
 */

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// 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};
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;
// keep track of how long ago we updated the NTP server
// unsigned long ntpLastUpdate = 0;
// adjust the sync latency with computer NTP client in seconds 
unsigned int syncLatency = 2;

void setup()
{
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  // 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
  if (Ethernet.begin(mac) == 0) {
    for (;;);
  }
  Udp.begin(localPort); 
ntpSyncDS1307();
}

void  loop(){  
  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);
    // ntpLastUpdate = now();
    // 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();
}