Simple example demonstrating how to use the NTP Client to set the time

Dependencies:   NTPClient mbed mbed-rtos EthernetInterface

Fork of HTTPClient_HelloWorld by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
donatien
Date:
Sun Aug 05 16:17:31 2012 +0000
Parent:
1:d263603373ac
Commit message:
Initial commit

Changed in this revision

HTTPClient.lib Show diff for this revision Revisions of this file
NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPClient.lib	Sun Aug 05 16:12:30 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/donatien/code/HTTPClient/#be61104f4e91
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Sun Aug 05 16:17:31 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/NTPClient/#881559865a93
--- a/main.cpp	Sun Aug 05 16:12:30 2012 +0000
+++ b/main.cpp	Sun Aug 05 16:17:31 2012 +0000
@@ -1,47 +1,29 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
-#include "HTTPClient.h"
+#include "NTPClient.h"
 
 EthernetInterface eth;
-HTTPClient http;
-char str[512];
+NTPClient ntp;
 
 int main() 
 {
     eth.init(); //Use DHCP
 
     eth.connect();
-    
-    //GET data
-    printf("Trying to fetch page...\n");
-    int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
-    if (!ret)
+   
+    printf("Trying to update time...\r\n");
+    if (ntp.setTime("0.pool.ntp.org") == 0)
     {
-      printf("Page fetched successfully - read %d characters\n", strlen(str));
-      printf("Result: %s\n", str);
+      printf("Set time successfully\r\n");
+      time_t ctTime;
+      ctTime = time(NULL);
+      printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
     }
     else
     {
-      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-    }
-    
-    //POST data
-    HTTPMap map;
-    HTTPText text(str, 512);
-    map.put("Hello", "World");
-    map.put("test", "1234");
-    printf("Trying to post data...\n");
-    ret = http.post("http://httpbin.org/post", map, &text);
-    if (!ret)
-    {
-      printf("Executed POST successfully - read %d characters\n", strlen(str));
-      printf("Result: %s\n", str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-    }
-    
+      printf("Error\r\n");
+    } 
+   
     eth.disconnect();  
 
     while(1) {