Tiny SNTP(NTP) Client
Dependencies: EthernetNetIf mbed
main.cpp@1:d3c1871be1e9, 2011-07-28 (annotated)
- Committer:
- okini3939
- Date:
- Thu Jul 28 17:23:43 2011 +0000
- Revision:
- 1:d3c1871be1e9
- Parent:
- 0:41e7cfdbd23a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:41e7cfdbd23a | 1 | #include "mbed.h" |
okini3939 | 0:41e7cfdbd23a | 2 | #include "EthernetNetIf.h" |
okini3939 | 0:41e7cfdbd23a | 3 | #include "TinySNTP.h" |
okini3939 | 0:41e7cfdbd23a | 4 | |
okini3939 | 0:41e7cfdbd23a | 5 | DigitalOut myled(LED1); |
okini3939 | 0:41e7cfdbd23a | 6 | Serial pc(USBTX, USBRX); |
okini3939 | 0:41e7cfdbd23a | 7 | EthernetNetIf eth; |
okini3939 | 0:41e7cfdbd23a | 8 | |
okini3939 | 0:41e7cfdbd23a | 9 | int main () { |
okini3939 | 0:41e7cfdbd23a | 10 | EthernetErr ethErr; |
okini3939 | 0:41e7cfdbd23a | 11 | uint32_t t; |
okini3939 | 0:41e7cfdbd23a | 12 | struct tm *tm_buf; |
okini3939 | 0:41e7cfdbd23a | 13 | time_t seconds = time(NULL); |
okini3939 | 0:41e7cfdbd23a | 14 | |
okini3939 | 0:41e7cfdbd23a | 15 | myled = 1; |
okini3939 | 0:41e7cfdbd23a | 16 | printf("Time as a string = %s\r\n", ctime(&seconds)); |
okini3939 | 0:41e7cfdbd23a | 17 | |
okini3939 | 0:41e7cfdbd23a | 18 | ethErr = eth.setup(); |
okini3939 | 0:41e7cfdbd23a | 19 | if(ethErr) { |
okini3939 | 0:41e7cfdbd23a | 20 | return -1; |
okini3939 | 0:41e7cfdbd23a | 21 | } |
okini3939 | 0:41e7cfdbd23a | 22 | |
okini3939 | 0:41e7cfdbd23a | 23 | ntpdate("ntp1.sakura.ad.jp", &t); |
okini3939 | 0:41e7cfdbd23a | 24 | |
okini3939 | 1:d3c1871be1e9 | 25 | t = t + 60 * 60 * 9; // JST |
okini3939 | 0:41e7cfdbd23a | 26 | tm_buf = localtime((time_t*)&t); |
okini3939 | 0:41e7cfdbd23a | 27 | printf("now: %04d/%02d/%02d %02d:%02d:%02d\n", 1900 + tm_buf->tm_year, tm_buf->tm_mon + 1, tm_buf->tm_mday, |
okini3939 | 0:41e7cfdbd23a | 28 | tm_buf->tm_hour, tm_buf->tm_min, tm_buf->tm_sec); |
okini3939 | 0:41e7cfdbd23a | 29 | |
okini3939 | 0:41e7cfdbd23a | 30 | myled = 0; |
okini3939 | 0:41e7cfdbd23a | 31 | return 0; |
okini3939 | 0:41e7cfdbd23a | 32 | } |