Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HTTPClient NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of HTTPClient_HelloWorld by
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.
Revision 9:b54448c7f712, committed 2017-01-13
- Comitter:
- mapellil
- Date:
- Fri Jan 13 14:43:38 2017 +0000
- Parent:
- 8:1b5c28fe5e94
- Child:
- 10:b3fbc83d5f9b
- Commit message:
- Added HTTPS example
Changed in this revision
--- a/HTTPClient.lib Thu Nov 17 14:10:21 2016 +0000 +++ b/HTTPClient.lib Fri Jan 13 14:43:38 2017 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/mapellil/code/HTTPClient/#ae30428dc030 +https://developer.mbed.org/users/mapellil/code/HTTPClient/#dafc02922e40
--- a/NSAPITests.lib Thu Nov 17 14:10:21 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/mapellil/code/NSAPITests/#950327f445a3
--- a/X_NUCLEO_IDW01M1v2.lib Thu Nov 17 14:10:21 2016 +0000 +++ b/X_NUCLEO_IDW01M1v2.lib Fri Jan 13 14:43:38 2017 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/mapellil/code/X_NUCLEO_IDW01M1v2/#ecec238129a0 +https://developer.mbed.org/teams/ST/code/X_NUCLEO_IDW01M1v2/#c8697141ce44
--- a/main.cpp Thu Nov 17 14:10:21 2016 +0000
+++ b/main.cpp Fri Jan 13 14:43:38 2017 +0000
@@ -1,83 +1,81 @@
-
-//#define TESTV2
-#ifdef TESTV2
#include "mbed.h"
-#include "SpwfInterface.h"
-#include "NSAPITests.h"
-
-//------------------------------------
-// Hyperterminal configuration
-// 9600 bauds, 8-bit data, no parity
-//------------------------------------
-
-Serial pc(USBTX, USBRX);
-DigitalOut myled(LED1);
-SpwfSAInterface spwf(D8, D2, false);
-
-int main()
-{
- int32_t err;
-// char * ssid = "STM";
-// char * seckey = "STMdemoPWD";
- char * ssid = "crespan";
- char * seckey = "Elfrontal1";
-
-
- pc.printf("\r\nSpwf Interface NSAPI Tests\r\n");
- pc.printf("\r\nconnecting to AP\r\n");
-
- err = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA);
-
- if (!err) {
- printf("Interface failed to connect with code %d\r\n", err);
- } else {
- do {
-
- nsapi_tests("SPWF Tests", &spwf, "192.168.1.19", 32001);
-
- } while (1);
- spwf.disconnect();
- }
-
- while(1)
- {
- wait(1);
- myled = !myled;
- }
-}
-
-#else
-
-#include "mbed.h"
-//#include "EthernetInterface.h"
#include "TCPSocket.h"
#include "HTTPClient.h"
-#ifdef LICIO
-#else
-EthernetInterface eth;
-#endif
+//www.mbed.com CA certificate in PEM format
+char CA_cert []="-----BEGIN CERTIFICATE-----\r\n"
+"MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\r\n"
+"A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\r\n"
+"b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\r\n"
+"MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\r\n"
+"YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT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);
+
+ printf("\r\nHTTPClient_HelloWorld mbed Application\r\n");
+ printf("\r\nconnecting to AP...\r\n");
+
+ SpwfSAInterface spwf(D8, D2, PC_12, PC_8, false);
spwf.connect("crespan","Elfrontal1", NSAPI_SECURITY_WPA2);
+ const char *ip = spwf.get_ip_address();
+ printf("\r\nIP Address is: %s\r\n", ip);
+
+ time_t ctTime;
+ ctTime = time(NULL);
+ printf ("Start Secure Socket connection with one way server autentication test\n\r");
+ if (!spwf.get_time(&ctTime)) printf ("ERROR get_time\n\r"); // read WiFi module time
+ else set_time(ctTime); // set Nucleo system time
+ printf("Initial System Time is: %s\r\n", ctime(&ctTime));
+ printf("Need to adjust time? if yes enter time in seconds elapsed since Epoch (cmd: date +'%%s'), otherwise enter 0 ");
+ int t=0;
+ scanf("%d",&t);
+ printf ("Entered time is: %d \n\r", t);
+ if (t != 0) {
+ time_t txTm=t; ctTime=t; set_time(txTm); // set Nucleo system time
+ if (!spwf.set_time(ctTime)) printf ("ERROR set_time\n\r");
+ } else {
+ if (!spwf.get_time(&ctTime)) printf ("ERROR get_time\n\r"); // read WiFi module time again to compensate time spent on scanf
+ else set_time(ctTime); // set Nucleo system time again to align with Wifi
+ }
+ printf ("The current system time is: %s\n\r", ctime (&ctTime));
+ spwf.get_time(&ctTime);
+ printf ("The current wifi time is: %s\n\r", ctime (&ctTime));
+
HTTPClient http(spwf);
-#else
- eth.init(); //Use DHCP
- eth.connect();
-#endif
int ret;
-
+
+ if (!spwf.clean_TLS_certificate(ALL)) printf ("ERROR clean_TLS_certificate\n\r");
+ if (!spwf.set_TLS_certificate(CA_cert, sizeof(CA_cert), FLASH_CA_ROOT_CERT)) printf ("ERROR set_TLS_certificate\n\r");
+ if (!spwf.set_TLS_SRV_domain("*.mbed.com",FLASH_DOMAIN)) printf ("ERROR set_TLS_CA_domain\n\r");
//GET data
printf("\nTrying to fetch page...\n\r");
- ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
+ ret = http.get("https://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
+
+//ret = http.get("https://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
+//ret = http.get("https://httpbin.org/ip", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
+//ret = http.get("https://www.mbed.com", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
// ret = http.get("http://httpstat.us", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT);
if (!ret)
{
@@ -88,7 +86,7 @@
{
printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode());
}
-
+
//POST data
HTTPMap map;
HTTPText inText(str, 512);
@@ -137,9 +135,11 @@
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
-// eth.disconnect();
+ spwf.disconnect();
+ printf ("WIFI disconnected, exiting ...\n\r");
- while(1) {
+ while(1) {
+ wait(1);
+ myled = !myled;
}
}
-#endif
\ No newline at end of file
--- a/mbed.bld Thu Nov 17 14:10:21 2016 +0000 +++ b/mbed.bld Fri Jan 13 14:43:38 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/d75b3fe1f5cb \ No newline at end of file
