A quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of mbed-os.

The program brings up the WiFi and the underlying network interface, and uses it to scans available networks, connects to a network, prints interface and connection details and performs simple HTTP operation.

Supported hardware:

Not that the mbed target board the WiFi shield gets connected to shouldn't have any other network interface e.g. Ethernet.

ESP8266 is a fallback option and will be used if the build is for unsupported platform.

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Tue Jul 24 15:15:02 2018 +0100
Parent:
70:011662e9019e
Child:
72:b4761c52cc91
Commit message:
Update application to support default interfaces from Mbed OS 5.9.x

.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp 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
--- a/README.md	Thu Jul 19 11:30:02 2018 +0100
+++ b/README.md	Tue Jul 24 15:15:02 2018 +0100
@@ -12,18 +12,20 @@
 
 ### Supported hardware ###
 
-* [u-blox Odin board](https://os.mbed.com/platforms/ublox-EVK-ODIN-W2/) built-in Wi-Fi module.
-* [Realtek RTL8195AM](https://os.mbed.com/platforms/REALTEK-RTL8195AM/) built-in Wi-Fi module.
-* [ST DISCO IOT board](https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/) with integrated [ISM43362 WiFi Inventek module](https://github.com/ARMmbed/wifi-ism43362).
-* [ST DISCO_F413ZH board](https://os.mbed.com/platforms/ST-Discovery-F413H/) with integrated [ISM43362 WiFi Inventek module](https://github.com/ARMmbed/wifi-ism43362).
-* [NUCLEO-F401RE](https://os.mbed.com/platforms/ST-Nucleo-F401RE/) with [X-NUCLEO-IDW04A1](http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-connect-hw/x-nucleo-idw04a1.html) Wi-Fi expansion board using pins D8 and D2 _(of the Arduino connector)_.
-* [NUCLEO-F401RE](https://os.mbed.com/platforms/ST-Nucleo-F401RE/) with [X-NUCLEO-IDW01M1](https://os.mbed.com/components/X-NUCLEO-IDW01M1/) Wi-Fi expansion board using pins PA_9 and PA_10 _(of the Morpho connector)_.
-* [NUCLEO-F429ZI](https://os.mbed.com/platforms/ST-Nucleo-F429ZI/) with ESP8266-01 module using pins D1 and D0.
-* [NUCLEO-L476RG](https://os.mbed.com/platforms/ST-Nucleo-L476RG/) with ESP8266-01 module using pins D8 and D2.
-* Other Mbed targets with an ESP8266 module, [X-NUCLEO-IDW04A1](http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-connect-hw/x-nucleo-idw04a1.html) or [X-NUCLEO-IDW01M1](https://os.mbed.com/components/X-NUCLEO-IDW01M1/) expansion board.
-  *(The Mbed target board the Wi-Fi shield connects to shouldn't have any other network interface, for example Ethernet.)*
-
-ESP8266 is a fallback option if the build is for unsupported platform.
+* All Mbed OS boards with build-in Wi-Fi module:
+    * [u-blox ODIN-W2](https://os.mbed.com/platforms/ublox-EVK-ODIN-W2/)
+    * [Realtek RTL8195AM](https://os.mbed.com/platforms/REALTEK-RTL8195AM/)
+    * [ST DISCO IOT board](https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/) with integrated [ISM43362 WiFi Inventek module](https://github.com/ARMmbed/wifi-ism43362).
+    * [ST DISCO_F413ZH board](https://os.mbed.com/platforms/ST-Discovery-F413H/) with integrated [ISM43362 WiFi Inventek module](https://github.com/ARMmbed/wifi-ism43362).
+    * [Advantech WISE-150](https://os.mbed.com/modules/advantech-wise-1530/)
+    * USI WM-BN-BM-22
+    * MxChip EMW3166
+* Boards with external WiFi shields.
+    * [NUCLEO-F401RE](https://os.mbed.com/platforms/ST-Nucleo-F401RE/) with [X-NUCLEO-IDW04A1](http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-connect-hw/x-nucleo-idw04a1.html) Wi-Fi expansion board using pins D8 and D2 _(of the Arduino connector)_.
+    * [NUCLEO-F401RE](https://os.mbed.com/platforms/ST-Nucleo-F401RE/) with [X-NUCLEO-IDW01M1](https://os.mbed.com/components/X-NUCLEO-IDW01M1/) Wi-Fi expansion board using pins PA_9 and PA_10 _(of the Morpho connector)_.
+    * [NUCLEO-F429ZI](https://os.mbed.com/platforms/ST-Nucleo-F429ZI/) with ESP8266-01 module using pins D1 and D0.
+    * [NUCLEO-L476RG](https://os.mbed.com/platforms/ST-Nucleo-L476RG/) with ESP8266-01 module using pins D8 and D2.
+    * Other Mbed targets with an ESP8266 module, [X-NUCLEO-IDW04A1](http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-connect-hw/x-nucleo-idw04a1.html) or [X-NUCLEO-IDW01M1](https://os.mbed.com/components/X-NUCLEO-IDW01M1/) expansion board.
 
 #### Connecting the ESP8266 ####
 
@@ -43,8 +45,8 @@
    mbed import mbed-os-example-wifi
    cd mbed-os-example-wifi
    ```
-   
-2. Configure the Wi-Fi shield to use.
+
+1. Configure the Wi-Fi shield to use.
 
    Edit ```mbed_app.json``` to include the correct Wi-Fi shield, SSID and password:
 
@@ -66,52 +68,48 @@
    ```
 
    Sample ```mbed_app.json``` files are provided for ESP8266 (```mbed_app_esp8266.json```), X-NUCLEO-IDW04A1 (```mbed_app_idw04a1.json```) and X-NUCLEO-IDW01M1 (```mbed_app_idw01m1```).
-   
+
    For WIFI_ISM43362, ignore the value of `wifi-shield` as it is already overrides per supported targets.
 
    For built-in Wi-Fi, ignore the value of `wifi-shield`.
 
-3. Compile and generate binary.
-
-   For example, for `GCC`:
+1. Compile and generate binary.
+    For example, for `GCC`:
+    ```
+    mbed compile -t GCC_ARM -m UBLOX_EVK_ODIN_W2
+    ```
 
-   ```
-   mbed compile -t GCC_ARM -m UBLOX_EVK_ODIN_W2
-   ```
-   
- 4. Open a serial console session with the target platform using the following parameters:
- 
+1. Open a serial console session with the target platform using the following parameters:
     * **Baud rate:** 9600
     * **Data bits:** 8
     * **Stop bits:** 1
     * **Parity:** None
- 
- 5. Copy or drag the application `mbed-os-example-wifi.bin` in the folder `mbed-os-example-wifi/BUILD/<TARGET NAME>/<PLATFORM NAME>` onto the target board.
- 
- 6. The serial console should display a similar output to below, indicating a successful Wi-Fi connection:
- 
- ```
- WiFi example
+
+1. Copy or drag the application `mbed-os-example-wifi.bin` in the folder `mbed-os-example-wifi/BUILD/<TARGET NAME>/<PLATFORM NAME>` onto the target board.
 
-Scan:
-Network: Dave Hot Spot secured: Unknown BSSID: 00:01:02:03:04:05 RSSI: -58 Ch: 1
-1 network available.
+1. The serial console should display a similar output to below, indicating a successful Wi-Fi connection:
+    ```
+    WiFi example
+
+    Scan:
+    Network: Dave Hot Spot secured: Unknown BSSID: 00:01:02:03:04:05 RSSI: -58 Ch: 1
+    1 network available.
 
-Connecting...
-Success
+    Connecting...
+    Success
 
-MAC: 00:01:02:03:04:05
-IP: 192.168.0.5
-Netmask: 255.255.255.0
-Gateway: 192.168.0.1
-RSSI: -27
+    MAC: 00:01:02:03:04:05
+    IP: 192.168.0.5
+    Netmask: 255.255.255.0
+    Gateway: 192.168.0.1
+    RSSI: -27
 
-Sending HTTP request to www.arm.com...
-sent 38 [GET / HTTP/1.1]
-recv 64 [HTTP/1.1 301 Moved Permanently]
+    Sending HTTP request to www.arm.com...
+    sent 38 [GET / HTTP/1.1]
+    recv 64 [HTTP/1.1 301 Moved Permanently]
 
-Done
-```
+    Done
+    ```
 
 ## Troubleshooting
 
--- a/main.cpp	Thu Jul 19 11:30:02 2018 +0100
+++ b/main.cpp	Tue Jul 24 15:15:02 2018 +0100
@@ -17,36 +17,43 @@
 #include "mbed.h"
 #include "TCPSocket.h"
 
-#define WIFI_ESP8266    1
-#define WIFI_IDW0XX1    2
-#define WIFI_ISM43362   3
+#define internal        1
+#define WIFI_ESP8266    2
+#define WIFI_IDW0XX1    3
+#define WIFI_ISM43362   4
 
-#if TARGET_UBLOX_EVK_ODIN_W2
-#include "OdinWiFiInterface.h"
-OdinWiFiInterface wifi;
-
-#elif TARGET_REALTEK_RTL8195AM
-#include "RTWInterface.h"
-RTWInterface wifi;
-
-#else // External WiFi modules
+WiFiInterface *wifi;
 
 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
+
 #include "ESP8266Interface.h"
-ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
+
+WiFiInterface *WiFiInterface::get_default_instance() {
+    static ESP8266Interface esp(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
+    return &esp;
+}
 
 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_ISM43362
+
 #include "ISM43362Interface.h"
-ISM43362Interface wifi;
+
+WiFiInterface *WiFiInterface::get_default_instance() {
+    static ISM43362Interface ism;
+    return &ism;
+}
 
 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
+
 #include "SpwfSAInterface.h"
-SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
 
-#endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
+WiFiInterface *WiFiInterface::get_default_instance() {
+    static SpwfSAInterface spwf(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
+    return &spwf;
+}
 
 #endif
 
+
 const char *sec2str(nsapi_security_t sec)
 {
     switch (sec) {
@@ -161,29 +168,35 @@
     printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
 #endif
 
-    count = scan_demo(&wifi);
+    wifi = WiFiInterface::get_default_instance();
+
+    count = scan_demo(wifi);
     if (count == 0) {
         printf("No WIFI APNs found - can't continue further.\n");
         return -1;
     }
 
     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
-    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
+    int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
     if (ret != 0) {
         printf("\nConnection error: %d\n", ret);
         return -1;
     }
 
     printf("Success\n\n");
-    printf("MAC: %s\n", wifi.get_mac_address());
-    printf("IP: %s\n", wifi.get_ip_address());
-    printf("Netmask: %s\n", wifi.get_netmask());
-    printf("Gateway: %s\n", wifi.get_gateway());
-    printf("RSSI: %d\n\n", wifi.get_rssi());
+    printf("MAC: %s\n", wifi->get_mac_address());
+    printf("IP: %s\n", wifi->get_ip_address());
+    printf("Netmask: %s\n", wifi->get_netmask());
+    printf("Gateway: %s\n", wifi->get_gateway());
+    printf("RSSI: %d\n\n", wifi->get_rssi());
 
-    http_demo(&wifi);
+    http_demo(wifi);
+
+    wifi->disconnect();
 
-    wifi.disconnect();
+#if MBED_CONF_APP_WIFI_SHIELD != internal
+    delete wifi;
+#endif
 
     printf("\nDone\n");
 }
--- a/mbed_app.json	Thu Jul 19 11:30:02 2018 +0100
+++ b/mbed_app.json	Tue Jul 24 15:15:02 2018 +0100
@@ -25,9 +25,6 @@
         "*": {
             "platform.stdio-convert-newlines": true
         },
-        "UBLOX_EVK_ODIN_W2": {
-            "target.device_has": ["EMAC"]
-        },
         "DISCO_F413ZH": {
             "wifi-shield": "WIFI_ISM43362"
         },