Clock display on app board LCD is set using an NTP network time server. DHCP service must be enabled for mbed to get an IP address. A network cable is needed.

Dependencies:   C12832_lcd EthernetInterface NTPClient mbed-rtos mbed

Committer:
4180_1
Date:
Tue Oct 01 19:53:45 2013 +0000
Revision:
1:9095ffb76813
Parent:
0:9653261dbcda
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:9653261dbcda 1 #include "mbed.h"
4180_1 0:9653261dbcda 2 #include "EthernetInterface.h"
4180_1 0:9653261dbcda 3 #include "NTPClient.h"
4180_1 0:9653261dbcda 4 #include "C12832_lcd.h"
4180_1 0:9653261dbcda 5
4180_1 0:9653261dbcda 6 C12832_LCD lcd; //Graphics LCD
4180_1 0:9653261dbcda 7 EthernetInterface eth;
4180_1 0:9653261dbcda 8 NTPClient ntp;
4180_1 0:9653261dbcda 9
4180_1 0:9653261dbcda 10 int main()
4180_1 0:9653261dbcda 11 {
4180_1 0:9653261dbcda 12 eth.init(); //Use DHCP
4180_1 0:9653261dbcda 13 wait(2);
4180_1 0:9653261dbcda 14 lcd.cls();
4180_1 0:9653261dbcda 15 lcd.printf("Getting IP Address\r\n");
4180_1 0:9653261dbcda 16 if(eth.connect(60000)!=0) {
4180_1 0:9653261dbcda 17 lcd.printf("DHCP error - No IP");
4180_1 0:9653261dbcda 18 wait(10);
4180_1 0:9653261dbcda 19 } else {
4180_1 0:9653261dbcda 20 lcd.printf("IP is %s\n", eth.getIPAddress());
4180_1 0:9653261dbcda 21 wait(2);
4180_1 0:9653261dbcda 22 }
4180_1 0:9653261dbcda 23 lcd.cls();
4180_1 0:9653261dbcda 24 lcd.printf("Trying to update time...\r\n");
4180_1 1:9095ffb76813 25 if (ntp.setTime("0.pool.ntp.org") == 0) {
4180_1 0:9653261dbcda 26 lcd.printf("Set time successfully\r\n");
4180_1 0:9653261dbcda 27 while(1) {
4180_1 0:9653261dbcda 28 lcd.cls();
4180_1 0:9653261dbcda 29 lcd.locate(0,0);
4180_1 0:9653261dbcda 30 time_t ctTime;
4180_1 0:9653261dbcda 31 ctTime = time(NULL);
4180_1 0:9653261dbcda 32 lcd.printf("%s\r\n", ctime(&ctTime));
4180_1 0:9653261dbcda 33 lcd.printf("Current Time (UTC)");
4180_1 0:9653261dbcda 34 wait(1);
4180_1 0:9653261dbcda 35 }
4180_1 0:9653261dbcda 36 } else {
4180_1 0:9653261dbcda 37 lcd.printf("NTP Error\r\n");
4180_1 0:9653261dbcda 38 }
4180_1 0:9653261dbcda 39
4180_1 0:9653261dbcda 40 eth.disconnect();
4180_1 0:9653261dbcda 41
4180_1 0:9653261dbcda 42 while(1) {
4180_1 0:9653261dbcda 43 }
4180_1 0:9653261dbcda 44 }