Datasheet

Flyport Wi-Fi and Ethernet Programmer's guide framework 2.3 (rev 1.0) www.openpicus.com
if(ts->tm_hour > 24)
{
ts->tm_hour = ts->tm_hour - 24;
}
else if(ts->tm_hour < 0)
{
ts->tm_hour = ts->tm_hour +24;
}
strftime(dateUTC, sizeof(dateUTC), "%Y-%m-%d / %H:%M.%S", ts);
if(UARTBufferSize(1) > 0)
{
UARTWrite(1, dateUTC);
UARTWrite(1, "\r\n");
// Removes all data in UARTRxBuffer, since we do not need it!
UARTFlush(1);
}
}
}
This example illustrates the code necessary to implement SNTP. First of all, “time.h” must be included
in the compiler library. This permits to use time_t and struct tm variables:
time_t now;
struct tm *ts;
epoch and epochtime are two 32bit Integers.
DWORD epoch=0;
DWORD epochtime=0xA2C2A;
The first one stores the value returned by
epoch=SNTPGetUTCSeconds();
the second has a value higher than a certain period since the SNTPGetUTCSeconds() function does
not return the correct value until some time has passed. To fix this problem there is the
while(epoch<epochtime)
which checks when the SNTP is sending the right value (in other words, a value greater than the past
reference “epochtime”).
Once the startup time has elapsed, and the application has passed the first while loop, it enters the
infinite while(1) loop and update the variables each time the loop executes.
epoch=SNTPGetUTCSeconds();
now=(time_t)epoch;
ts = localtime(&now);
In this way, the ts variable can be used, and the time values can be parsed using
strftime(dateUTC, sizeof(dateUTC), "%Y-%m-%d / %H:%M.%S", ts);
68