William Ye / Mbed 2 deprecated internet_clock

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Internet Clock with LCD based on the work by Jim Hamblen and Tyler Lisowski
00002 
00003 #include "mbed.h"
00004 #include "EthernetInterface.h"
00005 #include "NTPClient.h"
00006 #include "uLCD_4DGL.h"
00007 
00008 // Parameters
00009 char* domain_name = "0.uk.pool.ntp.org";
00010 int port_number = 123;
00011 
00012 // Networking
00013 EthernetInterface eth;
00014 NTPClient ntp_client;
00015 
00016 // Graphic LCD - TX, RX, and RES pins
00017 uLCD_4DGL uLCD(p9,p10,p11);
00018 
00019 int main() {
00020 
00021     time_t ct_time;
00022     char time_buffer[80];    
00023 
00024     // Initialize LCD
00025     uLCD.baudrate(115200);
00026     uLCD.background_color(BLACK);
00027     uLCD.cls();
00028 
00029     // Connect to network and wait for DHCP
00030     uLCD.locate(0,0); 
00031     uLCD.printf("Getting IP Address\n");
00032     eth.init();
00033     if ( eth.connect() == -1 ) {
00034         uLCD.printf("ERROR: Could not\nget IP address");
00035         return -1;
00036     }
00037     uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
00038     wait(1);
00039 
00040     // Read time from server
00041     uLCD.printf("Reading time...\n\r");
00042     ntp_client.setTime(domain_name, port_number);
00043     uLCD.printf("Time set\n");
00044     wait(2);
00045     eth.disconnect();
00046 
00047     // Reset LCD
00048     uLCD.background_color(WHITE);
00049     uLCD.textbackground_color(WHITE);
00050     uLCD.color(RED);
00051     uLCD.cls();
00052     uLCD.text_height(2);
00053 
00054     // Loop and update clock
00055     while (1) {
00056         uLCD.locate(0, 1);
00057         ct_time = time(NULL);
00058         strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
00059                                                 localtime(&ct_time));
00060         uLCD.printf("    UTC/GMT:\n%s", time_buffer);
00061         wait(0.1);
00062     }
00063 }