HTTP and HTTPS example application for Mbed OS 5

Dependencies:   mbed-http

This application demonstrates how to make HTTP and HTTPS requests and parse the response from Mbed OS 5.

It consists of six example applications, which you can select in source/select-demo.h:

Response parsing is done through nodejs/http-parser.

Note: HTTPS requests do not work on targets with less than 128K of RAM due to the size of the TLS handshake. For more background see mbed-http.

To build

  1. If you're using WiFi, specify the credentials in mbed_app.json.
  2. Build the project in the online compiler or using Mbed CLI.
  3. Flash the project to your development board.
  4. Attach a serial monitor to your board to see the debug messages.

Defining the network interface

This application uses the on-board network interface for your board. If you use an external network interface (f.e. a WiFi module) you need to add the driver to this project. Then, open network-helper.h and specify which network driver to use.

More information is in the Mbed OS documentation under IP Networking.

Entropy (or lack thereof)

On all platforms that do not have the TRNG feature, the application is compiled without TLS entropy sources. This means that your code is inherently unsafe and should not be deployed to any production systems. To enable entropy, remove the MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY macros from mbed_app.json.

Flash size

Default flash size for HTTPS is very large, as the application is loading the default Mbed TLS configuration. To use a more optimized version, you can disable unused cypher suites and other Mbed TLS features with a custom configuration file. Create a new configuration file, then add in mbed_app.json:

"MBEDTLS_CONFIG_FILE=\"mbedtls_config.h\""

to the macros array.

Running tests

You can run the integration tests from this project via Mbed CLI.

  1. In select-demo.h set the DEMO macro to DEMO_TESTS.
  2. Set your WiFi credentials in mbed_app.json.
  3. Then run the tests via:

$ mbed test -v -n mbed-http-tests-tests-*

Tested on

  • K64F with Ethernet.
  • NUCLEO_F411RE with ESP8266 (not working on Mbed OS 5.12+)
  • ODIN-W2 with WiFi.
  • K64F with Atmel 6LoWPAN shield.
  • DISCO-L475VG-IOT01A with WiFi (requires the wifi-ism43362 driver).

Files at this revision

API Documentation at this revision

Comitter:
Jan Jongboom
Date:
Fri Jan 04 13:32:26 2019 +0100
Parent:
34:7da6cfc032fc
Commit message:
Update to Mbed OS 5.11

Changed in this revision

.hgignore Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
mbed-http.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
source/network-helper.h Show annotated file Show diff for this revision Revisions of this file
source/select-demo.h Show annotated file Show diff for this revision Revisions of this file
diff -r 7da6cfc032fc -r 4b847971db1b .hgignore
--- a/.hgignore	Wed Dec 05 17:16:00 2018 +0900
+++ b/.hgignore	Fri Jan 04 13:32:26 2019 +0100
@@ -7,4 +7,5 @@
 easy-connect/
 mbed-http/
 .git/
-
+wifi-ism43362/
+wifi-ism43362.lib
diff -r 7da6cfc032fc -r 4b847971db1b README.md
--- a/README.md	Wed Dec 05 17:16:00 2018 +0900
+++ b/README.md	Fri Jan 04 13:32:26 2019 +0100
@@ -46,10 +46,23 @@
 
 to the `macros` array.
 
+## Running tests
+
+You can run the integration tests from this project via Mbed CLI.
+
+1. In `select-demo.h` set the `DEMO` macro to `DEMO_TESTS`.
+1. Set your WiFi credentials in `mbed_app.json`.
+1. Then run the tests via:
+
+```
+$ mbed test -v -n mbed-http-tests-tests-*
+```
+
 ## Tested on
 
 * K64F with Ethernet.
 * NUCLEO_F411RE with ESP8266.
 * ODIN-W2 with WiFi.
 * K64F with Atmel 6LoWPAN shield.
+* DISCO-L475VG-IOT01A with WiFi (requires the [wifi-ism43362](https://github.com/ARMmbed/wifi-ism43362/) driver).
 * [Mbed Simulator](https://github.com/janjongboom/mbed-simulator).
diff -r 7da6cfc032fc -r 4b847971db1b mbed-http.lib
--- a/mbed-http.lib	Wed Dec 05 17:16:00 2018 +0900
+++ b/mbed-http.lib	Fri Jan 04 13:32:26 2019 +0100
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/sandbox/code/mbed-http/#fa4d71265625
+https://developer.mbed.org/teams/sandbox/code/mbed-http/#6daf67a96a91
diff -r 7da6cfc032fc -r 4b847971db1b mbed-os.lib
--- a/mbed-os.lib	Wed Dec 05 17:16:00 2018 +0900
+++ b/mbed-os.lib	Fri Jan 04 13:32:26 2019 +0100
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#e1bea44212b8275f7d8ce7253e758c2e25c57482
+https://github.com/ARMmbed/mbed-os/#c966348d3f9ca80843be7cdc9b748f06ea73ced0
diff -r 7da6cfc032fc -r 4b847971db1b mbed_app.json
--- a/mbed_app.json	Wed Dec 05 17:16:00 2018 +0900
+++ b/mbed_app.json	Fri Jan 04 13:32:26 2019 +0100
@@ -18,12 +18,14 @@
             "platform.stdio-convert-newlines": true,
             "mbed-mesh-api.6lowpan-nd-channel-page": 0,
             "mbed-mesh-api.6lowpan-nd-channel": 12,
-            "mbed-trace.enable": 1,
+            "mbed-trace.enable": null,
             "mbed-http.http-buffer-size": 2048,
-            "tls-socket.debug-level": 0,
             "nsapi.default-wifi-security": "WPA_WPA2",
             "nsapi.default-wifi-ssid": "\"SSID\"",
             "nsapi.default-wifi-password": "\"Password\""
+        },
+        "DISCO_L475VG_IOT01A": {
+            "target.network-default-interface-type" : "WIFI"
         }
     }
 }
diff -r 7da6cfc032fc -r 4b847971db1b source/network-helper.h
--- a/source/network-helper.h	Wed Dec 05 17:16:00 2018 +0900
+++ b/source/network-helper.h	Fri Jan 04 13:32:26 2019 +0100
@@ -2,9 +2,7 @@
 #define _MBED_HTTP_EXAMPLE_H_
 
 #include "mbed.h"
-#ifdef TARGET_SIMULATOR
-#include "EthernetInterface.h"
-#endif
+#include "NetworkInterface.h"
 
 /**
  * Connect to the network using the default networking interface,
@@ -14,11 +12,12 @@
 NetworkInterface *connect_to_default_network_interface() {
     printf("[NWKH] Connecting to network...\n");
 
-#ifdef TARGET_SIMULATOR
-    NetworkInterface* network = new EthernetInterface();
-#else
     NetworkInterface* network = NetworkInterface::get_default_instance();
-#endif
+
+    if (!network) {
+        printf("[NWKH] No network interface found, select an interface in 'network-helper.h'\n");
+        return NULL;
+    }
 
     nsapi_error_t connect_status = network->connect();
 
diff -r 7da6cfc032fc -r 4b847971db1b source/select-demo.h
--- a/source/select-demo.h	Wed Dec 05 17:16:00 2018 +0900
+++ b/source/select-demo.h	Fri Jan 04 13:32:26 2019 +0100
@@ -7,6 +7,7 @@
 #define         DEMO_HTTPS                  4
 #define         DEMO_HTTPS_SOCKET_REUSE     5
 #define         DEMO_HTTPS_CHUNKED_REQUEST  6
+#define         DEMO_TESTS                  7
 
 #define         DEMO            DEMO_HTTP