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).
Revision 6:96e92bfda33e, committed 2017-01-03
- Comitter:
- mapellil
- Date:
- Tue Jan 03 16:26:45 2017 +0000
- Parent:
- 5:88cece84fb6e
- Child:
- 7:a33ce4fe1b91
- Commit message:
- Used socket connect(SocketAddress addr) to allow DNS resolution
Changed in this revision
| 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/NTPClient.lib Tue Jan 03 13:02:14 2017 +0000 +++ b/NTPClient.lib Tue Jan 03 16:26:45 2017 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/ST-Expansion-SW-Team/code/NTPClient/#510bffa8b3d9 +https://developer.mbed.org/teams/ST-Expansion-SW-Team/code/NTPClient/#2ac816b0f4ab
--- a/main.cpp Tue Jan 03 13:02:14 2017 +0000
+++ b/main.cpp Tue Jan 03 16:26:45 2017 +0000
@@ -41,7 +41,7 @@
ctTime = time(NULL);
printf ("\n\rConnecting to WiFi ...\n\r");
- spwf.connect("ssid","passw", NSAPI_SECURITY_WPA2);
+ spwf.connect("crespan","Elfrontal1", NSAPI_SECURITY_WPA2);
NTPClient ntp(spwf);
printf("Initial System Time is: %s\r\n", ctime(&ctTime));
@@ -59,14 +59,12 @@
// so DNS doesnt answer as it requires unsecure UDP socket connection.
// workaroud: convert URL to IP quering DNS before secure socket creation, then connect
// the secure socket created to the retrieved IP.
- SocketAddress addr(&spwf, "www.amazon.it");
- const char *ip=addr.get_ip_address();
- printf ("www.amazon.it resolved to: %s\n\r", ip);
+ SocketAddress addr(&spwf, "www.amazon.it", 443); // must be called BEFORE set_secure_socket() to allow DNS udp connection
spwf.set_secure_sockets();
TCPSocket socket(&spwf);
- int err = socket.connect(ip,443);
+ int err = socket.connect(addr);
if (err != 0 )printf ("ERROR opening %d\n\r", err);
- else printf ("--->>> Secure socket CONNECTED to: %s\n\r", ip);
+ else printf ("--->>> Secure socket CONNECTED to: %s\n\r", addr.get_ip_address());
spwf.set_unsecure_sockets();
socket.close();
printf ("Socket closed\n\r");
