HTTP/HTTPS client Hello World application running with X-NUCLEO-IDW01M1v2 wifi board.

Dependencies:   HTTPClient NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of HTTPClient_HelloWorld by ST Expansion SW Team

Example of HTTP and HTTPS connections using X-NUCLEO-IDW01M1 Wi-Fi expansion board.
The application is meant to be used with mbed OS 2 ("Classic") only (no mbedOS 5 support).
For HTTPS connection it uses the TLS/SSL feature provided natively by the Wi-Fi module and performs secure connection to the server also verifying the server identity.
To avoid expired CA certificates, system time (in epoch) must be manually entered (e..g. using http://www.epochconverter.com/ ) .
Retrieval of current time from an NTP server is shown by this example.

Committer:
nikapov
Date:
Mon Jan 16 15:12:46 2017 +0000
Revision:
12:4ae87c278a63
Parent:
11:225ab4865c6b
Update libs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mapellil 8:1b5c28fe5e94 1
mapellil 8:1b5c28fe5e94 2 #include "mbed.h"
mapellil 3:6e7a93483c12 3 #include "TCPSocket.h"
donatien 0:0e0debc29569 4 #include "HTTPClient.h"
donatien 0:0e0debc29569 5
mapellil 3:6e7a93483c12 6
mapellil 9:b54448c7f712 7 //www.mbed.com CA certificate in PEM format
mapellil 9:b54448c7f712 8 char CA_cert []="-----BEGIN CERTIFICATE-----\r\n"
mapellil 9:b54448c7f712 9 "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\r\n"
mapellil 9:b54448c7f712 10 "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\r\n"
mapellil 9:b54448c7f712 11 "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\r\n"
mapellil 9:b54448c7f712 12 "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\r\n"
mapellil 9:b54448c7f712 13 "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\r\n"
mapellil 9:b54448c7f712 14 "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\r\n"
mapellil 9:b54448c7f712 15 "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\r\n"
mapellil 9:b54448c7f712 16 "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\r\n"
mapellil 9:b54448c7f712 17 "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\r\n"
mapellil 9:b54448c7f712 18 "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\r\n"
mapellil 9:b54448c7f712 19 "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\r\n"
mapellil 9:b54448c7f712 20 "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\r\n"
mapellil 9:b54448c7f712 21 "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\r\n"
mapellil 9:b54448c7f712 22 "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\r\n"
mapellil 9:b54448c7f712 23 "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\r\n"
mapellil 9:b54448c7f712 24 "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\r\n"
mapellil 9:b54448c7f712 25 "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\r\n"
mapellil 9:b54448c7f712 26 "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\r\n"
mapellil 9:b54448c7f712 27 "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\r\n"
mapellil 9:b54448c7f712 28 "-----END CERTIFICATE-----\r\n";
mapellil 3:6e7a93483c12 29
donatien 1:d263603373ac 30 char str[512];
mapellil 9:b54448c7f712 31 DigitalOut myled(LED1);
donatien 1:d263603373ac 32
donatien 0:0e0debc29569 33 int main()
donatien 0:0e0debc29569 34 {
mapellil 9:b54448c7f712 35
mapellil 9:b54448c7f712 36 printf("\r\nHTTPClient_HelloWorld mbed Application\r\n");
mapellil 9:b54448c7f712 37 printf("\r\nconnecting to AP...\r\n");
mapellil 9:b54448c7f712 38
mapellil 9:b54448c7f712 39 SpwfSAInterface spwf(D8, D2, PC_12, PC_8, false);
nikapov 11:225ab4865c6b 40 spwf.connect("STM","STMDemo", NSAPI_SECURITY_WPA2);
donatien 0:0e0debc29569 41
mapellil 9:b54448c7f712 42 const char *ip = spwf.get_ip_address();
mapellil 9:b54448c7f712 43 printf("\r\nIP Address is: %s\r\n", ip);
mapellil 9:b54448c7f712 44
mapellil 9:b54448c7f712 45 time_t ctTime;
mapellil 9:b54448c7f712 46 ctTime = time(NULL);
mapellil 9:b54448c7f712 47 printf ("Start Secure Socket connection with one way server autentication test\n\r");
mapellil 9:b54448c7f712 48 if (!spwf.get_time(&ctTime)) printf ("ERROR get_time\n\r"); // read WiFi module time
mapellil 9:b54448c7f712 49 else set_time(ctTime); // set Nucleo system time
mapellil 9:b54448c7f712 50 printf("Initial System Time is: %s\r\n", ctime(&ctTime));
mapellil 9:b54448c7f712 51 printf("Need to adjust time? if yes enter time in seconds elapsed since Epoch (cmd: date +'%%s'), otherwise enter 0 ");
mapellil 9:b54448c7f712 52 int t=0;
mapellil 9:b54448c7f712 53 scanf("%d",&t);
mapellil 9:b54448c7f712 54 printf ("Entered time is: %d \n\r", t);
mapellil 9:b54448c7f712 55 if (t != 0) {
mapellil 9:b54448c7f712 56 time_t txTm=t; ctTime=t; set_time(txTm); // set Nucleo system time
mapellil 9:b54448c7f712 57 if (!spwf.set_time(ctTime)) printf ("ERROR set_time\n\r");
mapellil 9:b54448c7f712 58 } else {
mapellil 9:b54448c7f712 59 if (!spwf.get_time(&ctTime)) printf ("ERROR get_time\n\r"); // read WiFi module time again to compensate time spent on scanf
mapellil 9:b54448c7f712 60 else set_time(ctTime); // set Nucleo system time again to align with Wifi
mapellil 9:b54448c7f712 61 }
mapellil 9:b54448c7f712 62 printf ("The current system time is: %s\n\r", ctime (&ctTime));
mapellil 9:b54448c7f712 63 spwf.get_time(&ctTime);
mapellil 9:b54448c7f712 64 printf ("The current wifi time is: %s\n\r", ctime (&ctTime));
mapellil 9:b54448c7f712 65
mapellil 8:1b5c28fe5e94 66 HTTPClient http(spwf);
mapellil 3:6e7a93483c12 67 int ret;
mapellil 9:b54448c7f712 68
mapellil 9:b54448c7f712 69 if (!spwf.clean_TLS_certificate(ALL)) printf ("ERROR clean_TLS_certificate\n\r");
mapellil 9:b54448c7f712 70 if (!spwf.set_TLS_certificate(CA_cert, sizeof(CA_cert), FLASH_CA_ROOT_CERT)) printf ("ERROR set_TLS_certificate\n\r");
mapellil 9:b54448c7f712 71 if (!spwf.set_TLS_SRV_domain("*.mbed.com",FLASH_DOMAIN)) printf ("ERROR set_TLS_CA_domain\n\r");
donatien 0:0e0debc29569 72 //GET data
mapellil 5:526c0df8cbff 73 printf("\nTrying to fetch page...\n\r");
mapellil 9:b54448c7f712 74 ret = http.get("https://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
mapellil 9:b54448c7f712 75
mapellil 9:b54448c7f712 76 //ret = http.get("https://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
mapellil 9:b54448c7f712 77 //ret = http.get("https://httpbin.org/ip", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
mapellil 9:b54448c7f712 78 //ret = http.get("https://www.mbed.com", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
mapellil 3:6e7a93483c12 79 // ret = http.get("http://httpstat.us", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
donatien 0:0e0debc29569 80 if (!ret)
donatien 0:0e0debc29569 81 {
mapellil 5:526c0df8cbff 82 printf("Page fetched successfully - read %d characters\n\r", strlen(str));
donatien 0:0e0debc29569 83 printf("Result: %s\n", str);
donatien 0:0e0debc29569 84 }
donatien 0:0e0debc29569 85 else
donatien 0:0e0debc29569 86 {
mapellil 5:526c0df8cbff 87 printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
donatien 0:0e0debc29569 88 }
mapellil 9:b54448c7f712 89
donatien 0:0e0debc29569 90 //POST data
donatien 0:0e0debc29569 91 HTTPMap map;
donatien 2:270e2d0bb85a 92 HTTPText inText(str, 512);
donatien 0:0e0debc29569 93 map.put("Hello", "World");
donatien 0:0e0debc29569 94 map.put("test", "1234");
mapellil 5:526c0df8cbff 95 printf("\nTrying to post data...\n\r");
mapellil 3:6e7a93483c12 96 ret = http.post("http://httpbin.org/post", map, &inText, HTTP_CLIENT_DEFAULT_TIMEOUT);
mapellil 3:6e7a93483c12 97 // ret = http.post("http://posttestserver.com/post.php", map, &inText, HTTP_CLIENT_DEFAULT_TIMEOUT);
donatien 0:0e0debc29569 98 if (!ret)
donatien 0:0e0debc29569 99 {
mapellil 5:526c0df8cbff 100 printf("Executed POST successfully - read %d characters\n\r", strlen(str));
mapellil 5:526c0df8cbff 101 printf("Result: %s\n\r", str);
donatien 0:0e0debc29569 102 }
donatien 0:0e0debc29569 103 else
donatien 0:0e0debc29569 104 {
mapellil 5:526c0df8cbff 105 printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
donatien 0:0e0debc29569 106 }
donatien 0:0e0debc29569 107
donatien 2:270e2d0bb85a 108 //PUT data
donatien 2:270e2d0bb85a 109 strcpy(str, "This is a PUT test!");
donatien 2:270e2d0bb85a 110 HTTPText outText(str);
donatien 2:270e2d0bb85a 111 //HTTPText inText(str, 512);
mapellil 5:526c0df8cbff 112 printf("\nTrying to put resource...\n\r");
donatien 2:270e2d0bb85a 113 ret = http.put("http://httpbin.org/put", outText, &inText);
donatien 2:270e2d0bb85a 114 if (!ret)
donatien 2:270e2d0bb85a 115 {
mapellil 5:526c0df8cbff 116 printf("Executed PUT successfully - read %d characters\n\r", strlen(str));
mapellil 5:526c0df8cbff 117 printf("Result: %s\n\r", str);
donatien 2:270e2d0bb85a 118 }
donatien 2:270e2d0bb85a 119 else
donatien 2:270e2d0bb85a 120 {
mapellil 5:526c0df8cbff 121 printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
donatien 2:270e2d0bb85a 122 }
donatien 2:270e2d0bb85a 123
donatien 2:270e2d0bb85a 124 //DELETE data
donatien 2:270e2d0bb85a 125 //HTTPText inText(str, 512);
mapellil 5:526c0df8cbff 126 printf("\nTrying to delete resource...\n\r");
donatien 2:270e2d0bb85a 127 ret = http.del("http://httpbin.org/delete", &inText);
donatien 2:270e2d0bb85a 128 if (!ret)
donatien 2:270e2d0bb85a 129 {
mapellil 5:526c0df8cbff 130 printf("Executed DELETE successfully - read %d characters\n\r", strlen(str));
mapellil 5:526c0df8cbff 131 printf("Result: %s\n\r", str);
donatien 2:270e2d0bb85a 132 }
donatien 2:270e2d0bb85a 133 else
donatien 2:270e2d0bb85a 134 {
donatien 2:270e2d0bb85a 135 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 2:270e2d0bb85a 136 }
donatien 2:270e2d0bb85a 137
mapellil 9:b54448c7f712 138 spwf.disconnect();
mapellil 9:b54448c7f712 139 printf ("WIFI disconnected, exiting ...\n\r");
donatien 0:0e0debc29569 140
mapellil 9:b54448c7f712 141 while(1) {
mapellil 9:b54448c7f712 142 wait(1);
mapellil 9:b54448c7f712 143 myled = !myled;
donatien 0:0e0debc29569 144 }
donatien 0:0e0debc29569 145 }