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@9:c710dd7a4242, 2017-01-13 (annotated)
- Committer:
- mapellil
- Date:
- Fri Jan 13 15:39:45 2017 +0000
- Revision:
- 9:c710dd7a4242
- Parent:
- 7:a33ce4fe1b91
Updated Wifi lib
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" |
mapellil | 4:11f2b556765b | 4 | #include "TCPSocket.h" |
mapellil | 4:11f2b556765b | 5 | |
mapellil | 7:a33ce4fe1b91 | 6 | //www.mbed.com CA certificate in PEM format |
mapellil | 7:a33ce4fe1b91 | 7 | char CA_cert []="-----BEGIN CERTIFICATE-----\r\n" |
mapellil | 7:a33ce4fe1b91 | 8 | "MIIDVDCCAjygAwIBAgIDAjRWMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT\r\n" |
mapellil | 7:a33ce4fe1b91 | 9 | "MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i\r\n" |
mapellil | 7:a33ce4fe1b91 | 10 | "YWwgQ0EwHhcNMDIwNTIxMDQwMDAwWhcNMjIwNTIxMDQwMDAwWjBCMQswCQYDVQQG\r\n" |
mapellil | 7:a33ce4fe1b91 | 11 | "EwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UEAxMSR2VvVHJ1c3Qg\r\n" |
mapellil | 7:a33ce4fe1b91 | 12 | "R2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2swYYzD9\r\n" |
mapellil | 7:a33ce4fe1b91 | 13 | "9BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9mOSm9BXiLnTjoBbdq\r\n" |
mapellil | 7:a33ce4fe1b91 | 14 | "fnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIuT8rxh0PBFpVXLVDv\r\n" |
mapellil | 7:a33ce4fe1b91 | 15 | "iS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6cJmTM386DGXHKTubU\r\n" |
mapellil | 7:a33ce4fe1b91 | 16 | "1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmRCw7+OC7RHQWa9k0+\r\n" |
mapellil | 7:a33ce4fe1b91 | 17 | "bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5aszPeE4uwc2hGKceeoW\r\n" |
mapellil | 7:a33ce4fe1b91 | 18 | "MPRfwCvocWvk+QIDAQABo1MwUTAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTA\r\n" |
mapellil | 7:a33ce4fe1b91 | 19 | "ephojYn7qwVkDBF9qn1luMrMTjAfBgNVHSMEGDAWgBTAephojYn7qwVkDBF9qn1l\r\n" |
mapellil | 7:a33ce4fe1b91 | 20 | "uMrMTjANBgkqhkiG9w0BAQUFAAOCAQEANeMpauUvXVSOKVCUn5kaFOSPeCpilKIn\r\n" |
mapellil | 7:a33ce4fe1b91 | 21 | "Z57QzxpeR+nBsqTP3UEaBU6bS+5Kb1VSsyShNwrrZHYqLizz/Tt1kL/6cdjHPTfS\r\n" |
mapellil | 7:a33ce4fe1b91 | 22 | "tQWVYrmm3ok9Nns4d0iXrKYgjy6myQzCsplFAMfOEVEiIuCl6rYVSAlk6l5PdPcF\r\n" |
mapellil | 7:a33ce4fe1b91 | 23 | "PseKUgzbFbS9bZvlxrFUaKnjaZC2mqUPuLk/IH2uSrW4nOQdtqvmlKXBx4Ot2/Un\r\n" |
mapellil | 7:a33ce4fe1b91 | 24 | "hw4EbNX/3aBd7YdStysVAq45pmp06drE57xNNB6pXE0zX5IJL4hmXXeXxx12E6nV\r\n" |
mapellil | 7:a33ce4fe1b91 | 25 | "5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==\r\n" |
mapellil | 4:11f2b556765b | 26 | "-----END CERTIFICATE-----\r\n"; |
mapellil | 4:11f2b556765b | 27 | |
mapellil | 4:11f2b556765b | 28 | SpwfSAInterface spwf(D8, D2, false); // Wifi interface |
mapellil | 7:a33ce4fe1b91 | 29 | DigitalOut myled(LED1); |
donatien | 0:0e0debc29569 | 30 | |
donatien | 0:0e0debc29569 | 31 | int main() |
donatien | 0:0e0debc29569 | 32 | { |
mapellil | 3:ca8860f482e0 | 33 | time_t ctTime; |
mapellil | 3:ca8860f482e0 | 34 | ctTime = time(NULL); |
mapellil | 3:ca8860f482e0 | 35 | |
mapellil | 3:ca8860f482e0 | 36 | printf ("\n\rConnecting to WiFi ...\n\r"); |
mapellil | 7:a33ce4fe1b91 | 37 | spwf.connect("STM","STMDemo", NSAPI_SECURITY_WPA2); |
mapellil | 9:c710dd7a4242 | 38 | |
mapellil | 3:ca8860f482e0 | 39 | NTPClient ntp(spwf); |
donatien | 0:0e0debc29569 | 40 | |
mapellil | 3:ca8860f482e0 | 41 | printf("Initial System Time is: %s\r\n", ctime(&ctTime)); |
donatien | 2:bf7dc5f5bca9 | 42 | printf("Trying to update time...\r\n"); |
donatien | 2:bf7dc5f5bca9 | 43 | if (ntp.setTime("0.pool.ntp.org") == 0) |
donatien | 0:0e0debc29569 | 44 | { |
donatien | 2:bf7dc5f5bca9 | 45 | printf("Set time successfully\r\n"); |
donatien | 2:bf7dc5f5bca9 | 46 | ctTime = time(NULL); |
donatien | 2:bf7dc5f5bca9 | 47 | printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); |
mapellil | 4:11f2b556765b | 48 | if (!spwf.set_time(ctTime)) printf ("ERROR set_time\n\r"); |
mapellil | 4:11f2b556765b | 49 | if (!spwf.clean_TLS_certificate(ALL)) printf ("ERROR clean_TLS_certificate\n\r"); |
mapellil | 7:a33ce4fe1b91 | 50 | if (!spwf.set_TLS_certificate(CA_cert, sizeof(CA_cert), FLASH_CA_ROOT_CERT)) printf ("ERROR set_TLS_certificate\n\r"); |
mapellil | 7:a33ce4fe1b91 | 51 | if (!spwf.set_TLS_SRV_domain("*.mbed.com",FLASH_DOMAIN)) printf ("ERROR set_TLS_CA_domain\n\r"); |
mapellil | 4:11f2b556765b | 52 | // NOTE: DNS requires UDP socket. DNS is invoked while connecting a secure socket to resolve URL to IP, |
mapellil | 4:11f2b556765b | 53 | // so DNS doesnt answer as it requires unsecure UDP socket connection. |
mapellil | 4:11f2b556765b | 54 | // workaroud: convert URL to IP quering DNS before secure socket creation, then connect |
mapellil | 4:11f2b556765b | 55 | // the secure socket created to the retrieved IP. |
mapellil | 7:a33ce4fe1b91 | 56 | SocketAddress addr(&spwf, "www.mbed.com", 443); // must be called BEFORE set_secure_socket() to allow DNS udp connection |
mapellil | 7:a33ce4fe1b91 | 57 | spwf.set_secure_mode(); |
mapellil | 7:a33ce4fe1b91 | 58 | TCPSocket socket(&spwf); |
mapellil | 7:a33ce4fe1b91 | 59 | spwf.set_unsecure_mode(); |
mapellil | 6:96e92bfda33e | 60 | int err = socket.connect(addr); |
mapellil | 4:11f2b556765b | 61 | if (err != 0 )printf ("ERROR opening %d\n\r", err); |
mapellil | 6:96e92bfda33e | 62 | else printf ("--->>> Secure socket CONNECTED to: %s\n\r", addr.get_ip_address()); |
mapellil | 4:11f2b556765b | 63 | socket.close(); |
mapellil | 4:11f2b556765b | 64 | printf ("Socket closed\n\r"); |
mapellil | 4:11f2b556765b | 65 | } |
donatien | 0:0e0debc29569 | 66 | else |
donatien | 0:0e0debc29569 | 67 | { |
mapellil | 4:11f2b556765b | 68 | printf("Error: NTP could not contact server\r\n"); |
mapellil | 3:ca8860f482e0 | 69 | } |
mapellil | 3:ca8860f482e0 | 70 | spwf.disconnect(); |
mapellil | 4:11f2b556765b | 71 | printf ("WIFI disconnected, exiting ...\n\r"); |
mapellil | 7:a33ce4fe1b91 | 72 | |
mapellil | 7:a33ce4fe1b91 | 73 | while(1) { |
mapellil | 7:a33ce4fe1b91 | 74 | wait(1); |
mapellil | 7:a33ce4fe1b91 | 75 | myled = !myled; |
mapellil | 7:a33ce4fe1b91 | 76 | } |
donatien | 0:0e0debc29569 | 77 | } |