Mbed clock is set using NTP and UTC displayed on Nokia LCD. Seehttp://mbed.org/users/4180_1/notebook/internet-nokia-lcd-clock/

Dependencies:   EthernetNetIf NTPClient_NetServices mbed

Committer:
4180_1
Date:
Fri May 25 00:41:26 2012 +0000
Revision:
0:da7f4b6d2b7c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:da7f4b6d2b7c 1 #include "mbed.h"
4180_1 0:da7f4b6d2b7c 2 #include "EthernetNetIf.h"
4180_1 0:da7f4b6d2b7c 3 #include "NTPClient.h"
4180_1 0:da7f4b6d2b7c 4 #include "NokiaLCD.h"
4180_1 0:da7f4b6d2b7c 5 // Internet of Things clock example: LCD time is set via internet NTP time server
4180_1 0:da7f4b6d2b7c 6 EthernetNetIf eth;
4180_1 0:da7f4b6d2b7c 7 NTPClient ntp;
4180_1 0:da7f4b6d2b7c 8 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
4180_1 0:da7f4b6d2b7c 9
4180_1 0:da7f4b6d2b7c 10 int main() {
4180_1 0:da7f4b6d2b7c 11 //system time structure
4180_1 0:da7f4b6d2b7c 12 time_t ctTime;
4180_1 0:da7f4b6d2b7c 13 char *time_str;
4180_1 0:da7f4b6d2b7c 14 //clear LCD
4180_1 0:da7f4b6d2b7c 15 lcd.cls();
4180_1 0:da7f4b6d2b7c 16 lcd.locate(0,2);
4180_1 0:da7f4b6d2b7c 17 // lcd.printf prints to LCD display;
4180_1 0:da7f4b6d2b7c 18 lcd.printf("Get IP addr...");
4180_1 0:da7f4b6d2b7c 19 EthernetErr ethErr = eth.setup();
4180_1 0:da7f4b6d2b7c 20 //Get an Internet IP address using DHCP
4180_1 0:da7f4b6d2b7c 21 if (ethErr) {
4180_1 0:da7f4b6d2b7c 22 //error or timeout getting an IP address
4180_1 0:da7f4b6d2b7c 23 lcd.cls();
4180_1 0:da7f4b6d2b7c 24 lcd.locate(0,2);
4180_1 0:da7f4b6d2b7c 25 lcd.printf("Net Error %d",ethErr);
4180_1 0:da7f4b6d2b7c 26 return -1;
4180_1 0:da7f4b6d2b7c 27 }
4180_1 0:da7f4b6d2b7c 28 lcd.locate(0,3);
4180_1 0:da7f4b6d2b7c 29 lcd.printf("Reading Time...");
4180_1 0:da7f4b6d2b7c 30 //specify time server URL
4180_1 0:da7f4b6d2b7c 31 Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
4180_1 0:da7f4b6d2b7c 32 //Read time from server
4180_1 0:da7f4b6d2b7c 33 ntp.setTime(server);
4180_1 0:da7f4b6d2b7c 34 lcd.locate(0,4);
4180_1 0:da7f4b6d2b7c 35 lcd.printf("Time set");
4180_1 0:da7f4b6d2b7c 36 //Delay for human time to read LCD display
4180_1 0:da7f4b6d2b7c 37 wait(1);
4180_1 0:da7f4b6d2b7c 38 while (1) {
4180_1 0:da7f4b6d2b7c 39 // loop and periodically update the LCD's time display
4180_1 0:da7f4b6d2b7c 40 lcd.cls();
4180_1 0:da7f4b6d2b7c 41 ctTime = time(NULL);
4180_1 0:da7f4b6d2b7c 42 lcd.locate(0,1);
4180_1 0:da7f4b6d2b7c 43 lcd.printf("UTC:");
4180_1 0:da7f4b6d2b7c 44 lcd.locate(0,3);
4180_1 0:da7f4b6d2b7c 45 time_str = ctime(&ctTime);
4180_1 0:da7f4b6d2b7c 46 time_str[strlen(time_str)-1] = 0;
4180_1 0:da7f4b6d2b7c 47 lcd.printf("%s", time_str);
4180_1 0:da7f4b6d2b7c 48 wait(1);
4180_1 0:da7f4b6d2b7c 49 }
4180_1 0:da7f4b6d2b7c 50 }