Update library (02 Feb 2015)

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Fork of NTPClient_HelloWorld by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
ban4jp
Date:
Sun Feb 01 17:10:44 2015 +0000
Parent:
4:06cde8b92c77
Commit message:
Handle the error.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 06cde8b92c77 -r ef9b500c56e2 main.cpp
--- a/main.cpp	Sun Feb 01 16:59:48 2015 +0000
+++ b/main.cpp	Sun Feb 01 17:10:44 2015 +0000
@@ -8,20 +8,67 @@
 int main()
 {
     int ret = eth.init(); //Use DHCP
+    if (!ret) {
+        printf("Initialized, MAC: %s\n", eth.getMACAddress());
+    } else {
+        printf("Error eth.init() - ret = %d\n", ret);
+        return -1;
+    }
 
     ret = eth.connect();
+    if (!ret) {
+        printf("Connected, IP: %s, MASK: %s, GW: %s\n",
+               eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+    } else {
+        printf("Error eth.connect() - ret = %d\n", ret);
+        return -1;
+    }
+
 
     printf("Trying to update time...\n");
-    if (ntp.setTime("0.pool.ntp.org") == 0) {
-        printf("Set time successfully\n");
-        time_t ctTime;
-        ctTime = time(NULL);
-        printf("Time is set to (UTC): %s\n", ctime(&ctTime));
-    } else {
-        printf("Error\n");
+
+    time_t ctTime;
+    NTPResult result;
+
+    while(1) {
+        result = ntp.setTime("pool.ntp.org");
+        //result = ntp.setTime("pool.ntp.org", NTP_DEFAULT_PORT, 2000);
+
+        if (result == NTP_OK) {
+            time(&ctTime);
+            printf("Time is set to (UTC): %s\n", ctime(&ctTime));
+            break;
+        }
+
+        switch (result) {
+            case NTP_CONN:      ///<Connection error
+                printf("Connection error\n");
+                break;
+            case NTP_TIMEOUT:   ///<Connection timeout
+                printf("Connection timeout\n");
+                break;
+            case NTP_PRTCL:     ///<Protocol error
+                printf("Protocol error\n");
+                break;
+            case NTP_DNS:       ///<Could not resolve name
+                printf("Could not resolve name\n");
+                break;
+            default:
+                printf("Error result=%d\n", result);
+                return -1;
+        }
+
+        wait(5);
     }
 
+
     ret = eth.disconnect();
+    if (!ret) {
+        printf("Disconnected\n");
+    } else {
+        printf("Error eth.disconnect() - ret = %d\n", ret);
+    }
+
 
     while(1) {
     }