WiFi example for mbed OS, fork from https://github.com/ARMmbed/mbed-os-example-wifi

This is an example of a Wi-Fi application using the Wi-Fi and network socket APIs that Mbed OS provides.

The program brings up the Wi-Fi and the underlying network interface and uses it to scan available networks, connects to a network, prints interface and connection details and performs an HTTP operation.

For more information about Wi-Fi APIs, please visit the Mbed OS Wi-Fi documentation.

This example has been used on the following platforms

Revision:
5:01be82512f7a
Parent:
1:67275d54581f
--- a/main.cpp	Thu Feb 22 09:22:32 2018 +0000
+++ b/main.cpp	Wed Dec 12 05:22:58 2018 +0000
@@ -17,36 +17,7 @@
 #include "mbed.h"
 #include "TCPSocket.h"
 
-#define WIFI_ESP8266    1
-#define WIFI_IDW0XX1    2
-#define WIFI_ESP32      3
-
-#if TARGET_UBLOX_EVK_ODIN_W2
-#include "OdinWiFiInterface.h"
-OdinWiFiInterface wifi;
-
-#elif TARGET_REALTEK_RTL8195AM
-#include "RTWInterface.h"
-RTWInterface wifi;
-
-#elif TARGET_GR_LYCHEE
-#include "ESP32Interface.h"
-ESP32Interface wifi(P5_3, P3_14, P7_1, P0_1);
-
-#else // External WiFi modules
-
-#if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
-#include "ESP8266Interface.h"
-ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
-#elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
-#include "SpwfSAInterface.h"
-SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
-#elif MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP32
-#include "ESP32Interface.h"
-ESP32Interface wifi(MBED_CONF_APP_WIFI_EN, MBED_CONF_APP_WIFI_IO0, MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
-#endif
-
-#endif
+WiFiInterface *wifi;
 
 const char *sec2str(nsapi_security_t sec)
 {
@@ -75,13 +46,23 @@
 
     int count = wifi->scan(NULL,0);
 
+    if (count <= 0) {
+        printf("scan() failed with return value: %d\n", count);
+        return 0;
+    }
+
     /* Limit number of network arbitrary to 15 */
     count = count < 15 ? count : 15;
 
     ap = new WiFiAccessPoint[count];
     count = wifi->scan(ap, count);
-    for (int i = 0; i < count; i++)
-    {
+
+    if (count <= 0) {
+        printf("scan() failed with return value: %d\n", count);
+        return 0;
+    }
+
+    for (int i = 0; i < count; i++) {
         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
@@ -100,9 +81,13 @@
     printf("Sending HTTP request to www.arm.com...\n");
 
     // Open a socket on the network interface, and create a TCP connection to www.arm.com
-    socket.open(net);
+    response = socket.open(net);
+    if(0 != response) {
+        printf("socket.open() failed: %d\n", response);
+        return;
+    }
 
-    response = socket.connect("www.arm.com", 80);
+    response = socket.connect("api.ipify.org", 80);
     if(0 != response) {
         printf("Error connecting: %d\n", response);
         socket.close();
@@ -110,24 +95,22 @@
     }
 
     // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
+    char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
     nsapi_size_t size = strlen(sbuffer);
-    response = 0;
-    while(size)
-    {
+
+    // Loop until whole request send
+    while(size) {
         response = socket.send(sbuffer+response, size);
         if (response < 0) {
             printf("Error sending data: %d\n", response);
             socket.close();
             return;
-        } else {
-            size -= response;
-            // Check if entire message was sent or not
-            printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
         }
+        size -= response;
+        printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
     }
 
-    // Recieve a simple http response and print out the response line
+    // Receieve a simple http response and print out the response line
     char rbuffer[64];
     response = socket.recv(rbuffer, sizeof rbuffer);
     if (response < 0) {
@@ -144,31 +127,41 @@
 {
     int count = 0;
 
-    printf("WiFi example\n\n");
+    printf("WiFi example\n");
+
+#ifdef MBED_MAJOR_VERSION
+    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();
+    if (!wifi) {
+        printf("ERROR: No WiFiInterface found.\n");
+        return -1;
+    }
+
+    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\n");
+        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();
 
     printf("\nDone\n");
 }