Fetch Current UTC Date and Time from NTP (Network Time Protocol)

Dependencies:   C12832_lcd EthernetInterface NTPClient mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
dwijaybane
Date:
Sat Oct 10 09:23:23 2015 +0000
Parent:
0:0d3b3b9fd82e
Commit message:
comments updated

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0d3b3b9fd82e -r 039c06964ad9 main.cpp
--- a/main.cpp	Tue Oct 06 10:38:36 2015 +0000
+++ b/main.cpp	Sat Oct 10 09:23:23 2015 +0000
@@ -1,26 +1,29 @@
-#include "mbed.h"
-#include "EthernetInterface.h"
-#include "NTPClient.h"
-#include "C12832_lcd.h"
+#include "mbed.h"                   // Basic Library required for onchip peripherals
+#include "EthernetInterface.h"      // Ethernet Library 
+#include "NTPClient.h"              // Network Time Protocol client Library
+#include "C12832_lcd.h"             // Library for SPI based LCD
 
-C12832_LCD lcd; //Graphics LCD
-EthernetInterface eth;
-NTPClient ntp;
+/* Create Objects */  
+C12832_LCD lcd;         // Initialize lcd object with SPI pins
+EthernetInterface eth;  // Initialize Ethernet object
+NTPClient ntp;          // Initialize NTP object
 
+// For Static IP
 static const char* mbedIp       = "192.168.0.160";  //IP
 static const char* mbedMask     = "255.255.255.0";  // Mask
-static const char* mbedGateway  = "192.168.0.254";    //Gateway
+static const char* mbedGateway  = "192.168.0.254";  //Gateway
 
 int main()
 {
     EthernetInterface eth;
-    eth.init(mbedIp,mbedMask,mbedGateway); //Use  these parameters for static IP
-    eth.connect();
+    // eth.init(); // For DHCP uncomment this line
+    eth.init(mbedIp,mbedMask,mbedGateway); //Use these parameters for static IP (Comment for DHCP)
+    eth.connect(); //Connect with Network and get IP address
     lcd.printf("Connected! IP Address is %s\n", eth.getIPAddress());
     
     lcd.cls();
     lcd.printf("Trying to update time...\r\n");
-    if (ntp.setTime("0.pool.ntp.org") == 0) {
+    if (ntp.setTime("0.pool.ntp.org") == 0) { // ntp server
         lcd.printf("Set time successfully\r\n");
         while(1) {
             lcd.cls();