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).
main.cpp@3:ca8860f482e0, 2016-12-15 (annotated)
- Committer:
- mapellil
- Date:
- Thu Dec 15 10:09:47 2016 +0000
- Revision:
- 3:ca8860f482e0
- Parent:
- 2:bf7dc5f5bca9
- Child:
- 4:11f2b556765b
Replaced Ethernet interface with NetworkSocketAPI, added support to X_NUCLEO_IDWM01M1v2 wifi board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:0e0debc29569 | 1 | #include "mbed.h" |
mapellil | 3:ca8860f482e0 | 2 | #include "SpwfInterface.h" |
donatien | 2:bf7dc5f5bca9 | 3 | #include "NTPClient.h" |
donatien | 0:0e0debc29569 | 4 | |
donatien | 0:0e0debc29569 | 5 | int main() |
donatien | 0:0e0debc29569 | 6 | { |
mapellil | 3:ca8860f482e0 | 7 | time_t ctTime; |
mapellil | 3:ca8860f482e0 | 8 | ctTime = time(NULL); |
mapellil | 3:ca8860f482e0 | 9 | |
mapellil | 3:ca8860f482e0 | 10 | SpwfSAInterface spwf(D8, D2, false); |
mapellil | 3:ca8860f482e0 | 11 | printf ("\n\rConnecting to WiFi ...\n\r"); |
mapellil | 3:ca8860f482e0 | 12 | // spwf.connect("WIFI_NET_SSID","WIFI_NET_PASSW", NSAPI_SECURITY_WPA2); |
mapellil | 3:ca8860f482e0 | 13 | spwf.connect("wifi_ssid","wifi_passw", NSAPI_SECURITY_WPA2); |
mapellil | 3:ca8860f482e0 | 14 | NTPClient ntp(spwf); |
donatien | 0:0e0debc29569 | 15 | |
mapellil | 3:ca8860f482e0 | 16 | printf("Initial System Time is: %s\r\n", ctime(&ctTime)); |
donatien | 2:bf7dc5f5bca9 | 17 | printf("Trying to update time...\r\n"); |
donatien | 2:bf7dc5f5bca9 | 18 | if (ntp.setTime("0.pool.ntp.org") == 0) |
donatien | 0:0e0debc29569 | 19 | { |
donatien | 2:bf7dc5f5bca9 | 20 | printf("Set time successfully\r\n"); |
donatien | 2:bf7dc5f5bca9 | 21 | ctTime = time(NULL); |
donatien | 2:bf7dc5f5bca9 | 22 | printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); |
donatien | 0:0e0debc29569 | 23 | } |
donatien | 0:0e0debc29569 | 24 | else |
donatien | 0:0e0debc29569 | 25 | { |
donatien | 2:bf7dc5f5bca9 | 26 | printf("Error\r\n"); |
mapellil | 3:ca8860f482e0 | 27 | } |
mapellil | 3:ca8860f482e0 | 28 | spwf.disconnect(); |
donatien | 0:0e0debc29569 | 29 | } |