get internet time on k64f
Dependencies: EthernetInterface NTPClient mbed-rtos mbed
Fork of Internet_LCD_Clock by
Diff: Internet_LCD_Clock.cpp
- Revision:
- 2:ec0b6a5ccef7
- Parent:
- 1:09fcc9b81f23
--- a/Internet_LCD_Clock.cpp Sat Apr 14 01:12:23 2012 +0000 +++ b/Internet_LCD_Clock.cpp Fri Apr 07 14:11:00 2017 +0000 @@ -1,41 +1,36 @@ #include "mbed.h" -#include "EthernetNetIf.h" +#include "EthernetInterface.h" #include "NTPClient.h" -#include "TextLCD.h" + +DigitalIn Switch(SW2); // Internet of Things clock example: LCD time is set via internet NTP time server -TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3 -EthernetNetIf eth; +EthernetInterface eth; NTPClient ntp; int main() { -//system time structure + //system time structure time_t ctTime; - //clear LCD - lcd.cls(); + // lcd.printf prints to LCD display; - lcd.printf("Get IP addr..."); - EthernetErr ethErr = eth.setup(); - //Get an Internet IP address using DHCP - if (ethErr) { - //error or timeout getting an IP address - lcd.cls(); - lcd.printf("Network Error \n\r %d",ethErr); - return -1; - } - lcd.cls(); - lcd.printf("Reading Time...\n\r"); + printf("Get IP addr...\r\n"); + eth.init(); //Use DHCP + eth.connect(); + printf("IP Address-a is %s\r\n", eth.getIPAddress()); + printf("Reading Time...\r\n"); //specify time server URL - Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); + TCPSocketConnection sock; + sock.connect("tock.stdtime.gov.tw", 123); //Read time from server - ntp.setTime(server); - lcd.printf("Time set"); + ntp.setTime("tock.stdtime.gov.tw"); + printf("Time set\n"); //Delay for human time to read LCD display wait(1); while (1) { - // loop and periodically update the LCD's time display - lcd.cls(); - ctTime = time(NULL); - lcd.printf("UTC: %s", ctime(&ctTime)); - wait(.25); + if(Switch==0){ + // loop and periodically update the LCD's time display + ctTime = time(NULL); + printf("UTC: %s\r\n", ctime(&ctTime)); + wait(.25); + } } }