![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
NTPクラインアントの例
main.cpp@0:813e195356e1, 2011-12-26 (annotated)
- Committer:
- yueee_yt
- Date:
- Mon Dec 26 04:42:55 2011 +0000
- Revision:
- 0:813e195356e1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:813e195356e1 | 1 | #include "mbed.h" |
yueee_yt | 0:813e195356e1 | 2 | #include "EthernetNetIf.h" |
yueee_yt | 0:813e195356e1 | 3 | #include "NTPClient.h" |
yueee_yt | 0:813e195356e1 | 4 | #include "TextLCD.h" |
yueee_yt | 0:813e195356e1 | 5 | |
yueee_yt | 0:813e195356e1 | 6 | EthernetNetIf ethif; //for DHCP |
yueee_yt | 0:813e195356e1 | 7 | //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 |
yueee_yt | 0:813e195356e1 | 8 | |
yueee_yt | 0:813e195356e1 | 9 | NTPClient ntp; |
yueee_yt | 0:813e195356e1 | 10 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
yueee_yt | 0:813e195356e1 | 11 | |
yueee_yt | 0:813e195356e1 | 12 | int main() { |
yueee_yt | 0:813e195356e1 | 13 | char buffer[17]; |
yueee_yt | 0:813e195356e1 | 14 | lcd.locate(0,1); |
yueee_yt | 0:813e195356e1 | 15 | lcd.printf("Start\n"); |
yueee_yt | 0:813e195356e1 | 16 | lcd.locate(0,1); |
yueee_yt | 0:813e195356e1 | 17 | lcd.printf("Setting up...\n"); |
yueee_yt | 0:813e195356e1 | 18 | EthernetErr ethErr = ethif.setup(); |
yueee_yt | 0:813e195356e1 | 19 | if (ethErr) { |
yueee_yt | 0:813e195356e1 | 20 | lcd.locate(0,1); |
yueee_yt | 0:813e195356e1 | 21 | lcd.printf("Error %d in setup.\n", ethErr); |
yueee_yt | 0:813e195356e1 | 22 | return -1; |
yueee_yt | 0:813e195356e1 | 23 | } |
yueee_yt | 0:813e195356e1 | 24 | lcd.locate(0,1); |
yueee_yt | 0:813e195356e1 | 25 | lcd.printf("Setup OK\r\n"); |
yueee_yt | 0:813e195356e1 | 26 | IpAddr ethIp=ethif.getIp(); |
yueee_yt | 0:813e195356e1 | 27 | lcd.locate(0,1); |
yueee_yt | 0:813e195356e1 | 28 | lcd.printf("%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]); |
yueee_yt | 0:813e195356e1 | 29 | wait(1.0f); |
yueee_yt | 0:813e195356e1 | 30 | |
yueee_yt | 0:813e195356e1 | 31 | time_t ctTime; |
yueee_yt | 0:813e195356e1 | 32 | |
yueee_yt | 0:813e195356e1 | 33 | Host server(IpAddr(), 123, "ntp2.jst.mfeed.ad.jp"); //near ntp server |
yueee_yt | 0:813e195356e1 | 34 | ntp.setTime(server); |
yueee_yt | 0:813e195356e1 | 35 | //UTC-->JST +9Hour(32400Sec) |
yueee_yt | 0:813e195356e1 | 36 | ctTime = time(NULL); |
yueee_yt | 0:813e195356e1 | 37 | ctTime+=32400; |
yueee_yt | 0:813e195356e1 | 38 | set_time(ctTime); |
yueee_yt | 0:813e195356e1 | 39 | |
yueee_yt | 0:813e195356e1 | 40 | lcd.cls(); |
yueee_yt | 0:813e195356e1 | 41 | while (1) { |
yueee_yt | 0:813e195356e1 | 42 | lcd.locate(0,0); |
yueee_yt | 0:813e195356e1 | 43 | ctTime = time(NULL); |
yueee_yt | 0:813e195356e1 | 44 | lcd.locate(0,0); |
yueee_yt | 0:813e195356e1 | 45 | strftime(buffer,17,"%Y/%m/%d(%a)",localtime(&ctTime)); |
yueee_yt | 0:813e195356e1 | 46 | lcd.printf("%s",buffer); |
yueee_yt | 0:813e195356e1 | 47 | lcd.locate(0,1); |
yueee_yt | 0:813e195356e1 | 48 | strftime(buffer,17,"%X",localtime(&ctTime)); |
yueee_yt | 0:813e195356e1 | 49 | lcd.printf("%s",buffer); |
yueee_yt | 0:813e195356e1 | 50 | wait(1.0f); |
yueee_yt | 0:813e195356e1 | 51 | } |
yueee_yt | 0:813e195356e1 | 52 | return 0; |
yueee_yt | 0:813e195356e1 | 53 | } |
yueee_yt | 0:813e195356e1 | 54 |