NuMaker WiFi TCP Example

Revision:
24:38e186a10abe
Parent:
23:dc536ce49b37
Child:
26:f5163fee2653
--- a/main.cpp	Tue Oct 08 15:40:04 2019 +0800
+++ b/main.cpp	Tue Oct 08 15:48:42 2019 +0800
@@ -6,51 +6,6 @@
 #define MBED_HEAP_STATS_ENABLED 1
 //#define LOCAL_LAN
 
-#define ETHERNET        1
-#define WIFI            2
-#define MESH_LOWPAN_ND  3
-#define MESH_THREAD     4
-
-#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
-#define ESP8266_AT_ONBOARD      1   // On-board ESP8266
-#define ESP8266_AT_EXTERN       2   // External ESP8266 through UNO D1/D0
-
-#ifndef ESP8266_AT_SEL
-#error("ESP8266_AT_SEL missing. Select ESP8266 on-board/external.")
-#endif
-
-#include "ESP8266Interface.h"
-#   if ESP8266_AT_SEL == ESP8266_AT_ONBOARD
-#       if TARGET_NUMAKER_IOT_M487
-DigitalOut esp_rst(PH_3, 0);        // Simulate reset button pressed
-ESP8266Interface esp(PH_8, PH_9);
-#       elif TARGET_NU_PFM_M2351_NPSA_NS
-DigitalIn esp_gpio0(PD_6);          // Go boot mode by default
-                                    // User can change to F/W update mode by short'ing ESP8266 GPIO0/GND
-                                    // before power-on
-DigitalOut esp_pwr_off(PD_7, 1);    // Disable power to on-board ESP8266
-ESP8266Interface esp(PD_1, PD_0);
-#       elif TARGET_NUMAKER_IOT_M263A
-ESP8266Interface esp(PC_7, PC_6, false, PE_13, PC_8, PE_12);
-//ESP8266Interface esp(PC_7, PC_6, false, PE_13, PC_8, NC);
-//ESP8266Interface esp(PC_7, PC_6);
-#       endif
-#   elif ESP8266_AT_SEL == ESP8266_AT_EXTERN
-ESP8266Interface esp(D1, D0);
-#   endif
-#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
-#include "EthernetInterface.h"
-EthernetInterface eth;
-#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
-#define MESH
-#include "NanostackInterface.h"
-LoWPANNDInterface mesh;
-#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
-#define MESH
-#include "NanostackInterface.h"
-ThreadInterface mesh;
-#endif
-
 namespace {
     // Test connection information
 #ifndef LOCAL_LAN
@@ -84,20 +39,6 @@
 }
 
 int main() {
-#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
-#   if ESP8266_AT_SEL == ESP8266_AT_ONBOARD
-#       if TARGET_NUMAKER_IOT_M487
-    wait_ms(5);
-    esp_rst = 1;                    // Simulate reset button released
-    wait_ms(5);
-#       elif TARGET_NU_PFM_M2351_NPSA_NS
-    wait_ms(50);
-    esp_pwr_off = 0;                // Turn on on-board ESP8266
-    wait_ms(50);
-#       endif
-#   endif
-#endif
-
 #if MBED_HEAP_STATS_ENABLED
     mbed_stats_heap_t heap_stats;
 #endif
@@ -109,26 +50,15 @@
 
     printf("Start Connection ... \r\n");
 
+    NetworkInterface *network_interface = NetworkInterface::get_default_instance();
+    if (NULL == network_interface) {
+        printf("NULL network interface! Exiting application....\r\n");
+        return 0;
+    }
 
-    NetworkInterface *network_interface = 0;
-   
-#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
     printf("\n\rUsing WiFi \r\n");
     printf("\n\rConnecting to WiFi..\r\n");
-    rc = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
-    network_interface = &esp;
-#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
-    printf("Using Ethernet\r\n");
-    rc = eth.connect();
-    network_interface = ð
-#endif
-#ifdef MESH
-    printf("Using Mesh\r\n");
-    printf("\n\rConnecting to Mesh..\r\n");
-    rc = mesh.connect();
-    network_interface = &mesh;
-#endif
-
+    rc = network_interface->connect();
     if(rc == 0) {
         printf("\n\rConnected to Network successfully\r\n");
     } else {
@@ -189,15 +119,7 @@
  
     printf(" Close socket & disconnect ... \r\n");
     sock.close();
-    
-#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
-    esp.disconnect();
-#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
-    eth.disconnect();
-#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
-    mesh.disconnect();
-#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
-    mesh.disconnect();
-#endif
+
+    network_interface->disconnect();
     printf(" End \r\n");
 }