![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
NTPクラインアントの例
Diff: main.cpp
- Revision:
- 0:813e195356e1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 26 04:42:55 2011 +0000 @@ -0,0 +1,54 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "NTPClient.h" +#include "TextLCD.h" + +EthernetNetIf ethif; //for DHCP +//EthernetNetIf ethif(IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx)); //for Static IP Address + +NTPClient ntp; +TextLCD lcd(p24, p26, p27, p28, p29, p30); + +int main() { + char buffer[17]; + lcd.locate(0,1); + lcd.printf("Start\n"); + lcd.locate(0,1); + lcd.printf("Setting up...\n"); + EthernetErr ethErr = ethif.setup(); + if (ethErr) { + lcd.locate(0,1); + lcd.printf("Error %d in setup.\n", ethErr); + return -1; + } + lcd.locate(0,1); + lcd.printf("Setup OK\r\n"); + IpAddr ethIp=ethif.getIp(); + lcd.locate(0,1); + lcd.printf("%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]); + wait(1.0f); + + time_t ctTime; + + Host server(IpAddr(), 123, "ntp2.jst.mfeed.ad.jp"); //near ntp server + ntp.setTime(server); +//UTC-->JST +9Hour(32400Sec) + ctTime = time(NULL); + ctTime+=32400; + set_time(ctTime); + + lcd.cls(); + while (1) { + lcd.locate(0,0); + ctTime = time(NULL); + lcd.locate(0,0); + strftime(buffer,17,"%Y/%m/%d(%a)",localtime(&ctTime)); + lcd.printf("%s",buffer); + lcd.locate(0,1); + strftime(buffer,17,"%X",localtime(&ctTime)); + lcd.printf("%s",buffer); + wait(1.0f); + } + return 0; +} +