NTP client app setting system time and X-NUCLEO-IDW01M1 Wi-Fi board time connecting to internet NTP UTC time server.
Dependencies: NTPClient NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of NTPClient_HelloWorld by
Example of retrieval of current time using an NTP server.
Time is then used to perform a TLS secure connection to https://mbed.org.
The application is meant to be used with mbed OS 2 ("Classic") only (no mbedOS 5 support).
Diff: main.cpp
- Revision:
- 2:bf7dc5f5bca9
- Parent:
- 1:d263603373ac
- Child:
- 3:ca8860f482e0
diff -r d263603373ac -r bf7dc5f5bca9 main.cpp
--- 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) {
