Someone pm'd me with interest in the time server sync code I wrote. I thought I'd post the code here should anyone else want to use it. I made this into a pretty simple sketch that is part of a much larger project. Right now it simply prints the time on the serial port every second as an example. One caveat, is that it uses the WString.h library. The library is very nice but needs to be patched to plug a memory leak. Check here for the info on that.
//**************************************************************//
// Name : TimeClient //
// Author : Digger450 //
// Date : 07/24/2009 //
// Version : 1.0 //
//****************************************************************
#include <Ethernet.h>
#include <WString.h>
#include <DateTime.h>
#include <DateTimeStrings.h>
// network configuration. gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 191 }; // Local IP
byte timeServer[] = {192, 43, 244, 18 }; // Time Server
Client timeClient(timeServer, 13);
String lineBuffer = String(255);
long daylightTime = -5;
long standardTime = -6;
unsigned long updateTime = 3590000L;
#define TIME_MSG_LEN 11 // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER 255
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println("Clock Online");
}
void loop()
{
checkTime();
printTime();
delay(1000);
}
void checkTime()
{
unsigned long tempTimeComp = millis();
boolean responseRec = false;
long timeOut = millis();
long tempTime = 0;
long currentMJD;
long unixMJD = 40587;
long currentHour;
long currentMin;
long currentSec;
int dstVal;
long dstTime;
long unixTime;
lineBuffer = "";
if (abs(tempTimeComp - updateTime) > 3600000L)
{
Serial.println("Updating Time");
if (timeClient.connect())
{
Serial.println("Connected");
while (!responseRec)
{
tempTime = millis();
if ((abs(tempTime - timeOut)) > 10000)
{
Serial.println("Timeout On Response");
return;
}
if (timeClient.available())
{
char inChar = timeClient.read();
if (lineBuffer.length() < 255)
{
if (inChar != '\n')
{
lineBuffer.append(inChar);
}
}
if (lineBuffer.contains("*"))
{
currentMJD = atol(lineBuffer.substring(0, 5));
currentHour = atol(lineBuffer.substring(15, 17));
currentMin = atol(lineBuffer.substring(18, 20));
currentSec = atol(lineBuffer.substring(21, 23));
dstVal = atoi(lineBuffer.substring(24, 26));
if (dstVal > 0)
{
dstTime = daylightTime;
}
else
{
dstTime = standardTime;
}
unixTime = (currentMJD - unixMJD) * 86400L;
unixTime += (currentHour * 3600);
unixTime += (currentMin * 60);
unixTime += currentSec;
unixTime += (dstTime * 3600);
Serial.println(unixTime);
DateTime.sync(unixTime);
responseRec = true;
updateTime = millis();
timeClient.stop();
}
}
}
}
else
{
timeClient.stop();
Serial.println("Connection Failed");
delay(5000);
}
}
}
void printTime()
{
DateTime.available();
Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
Serial.print(" ");
Serial.print(DateTimeStrings.monthStr(DateTime.Month));
Serial.print(" ");
Serial.print(DateTime.Day, DEC);
Serial.print(" ");
Serial.print(DateTime.Hour,DEC);
Serial.print(":");
if(DateTime.Minute < 10)
Serial.print('0');
Serial.print(DateTime.Minute,DEC);
Serial.print(":");
if(DateTime.Second < 10)
Serial.print('0');
Serial.println(DateTime.Second,DEC);
}