Simple mbed OS sockets example for mbed OS5 & W5500 SPI Ethernet controller. This is a quick example of a simple HTTP client program using the network-socket API that is provided as a part of mbed-os. The program brings up an underlying network interface, and uses it to perform an HTTP transaction over a TCPSocket.

Dependencies:   W5500Interface easy-connect

Fork of mbed-os-example-mbed5-sockets by mbed-os-examples

Result

  • Serial Terminal Log

/media/uploads/Bongjun/img021.png

Files at this revision

API Documentation at this revision

Comitter:
Bongjun
Date:
Mon Aug 13 08:12:45 2018 +0000
Parent:
54:0f5a9685585e
Commit message:
Simple mbed OS sockets example for mbed OS5 and W5500 SPI Ethernet controller

Changed in this revision

W5500Interface.lib Show annotated file Show diff for this revision Revisions of this file
easy-connect.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/W5500Interface.lib	Mon Aug 13 08:12:45 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/Bongjun/code/W5500Interface/#5118c2bff025
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/easy-connect.lib	Mon Aug 13 08:12:45 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/Bongjun/code/easy-connect/#307d527714cf
--- a/main.cpp	Tue Jul 31 14:15:07 2018 +0100
+++ b/main.cpp	Mon Aug 13 08:12:45 2018 +0000
@@ -1,4 +1,7 @@
+#include <string>
 #include "mbed.h"
+#include "easy-connect.h"
+#include "nsapi_dns.h"
 
 // Network interface
 NetworkInterface *net;
@@ -18,8 +21,16 @@
     printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
 #endif
 
-    net = NetworkInterface::get_default_instance();
+//    net = NetworkInterface::get_default_instance();
 
+    printf("Easy connect...\n");
+    net = easy_connect(true);
+    if (!net) {
+        printf("Cannot connect to the network, see serial output");
+        return 1;
+    }
+    printf("Connected to the network. Opening a socket...\n");
+ 
     if (!net) {
         printf("Error! No network inteface found.\n");
         return 0;
@@ -44,8 +55,10 @@
     if (r != 0) {
         printf("Error! socket.open() returned: %d\n", r);
     }
-
+    
     r = socket.connect("api.ipify.org", 80);
+    //r = socket.connect("23.23.114.123", 80);
+    
     if (r != 0) {
         printf("Error! socket.connect() returned: %d\n", r);
     }
@@ -69,16 +82,25 @@
     remaining = 256;
     rcount = 0;
     p = buffer;
+
+    r = socket.recv(p, remaining);
+    /*
+    // modified source..a bit strnage...
     while (0 < (r = socket.recv(p, remaining))) {
         p += r;
         rcount += r;
         remaining -= r;
     }
+    */
     if (r < 0) {
         printf("Error! socket.recv() returned: %d\n", r);
         goto disconnect;
     }
 
+    p += r;
+    rcount += r;
+    remaining -= r;
+
     printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
 
     // The api.ipify.org service also gives us the device's external IP address
--- a/mbed_app.json	Tue Jul 31 14:15:07 2018 +0100
+++ b/mbed_app.json	Mon Aug 13 08:12:45 2018 +0000
@@ -1,8 +1,51 @@
 {
+    "config": {
+        "network-interface":{
+            "help": "options are ETHERNET, ETHERNET_W5500, WIFI_ESP8266, WIFI_ODIN, MESH_LOWPAN_ND, MESH_THREAD",
+            "value": "ETHERNET_W5500"
+        },
+        "mesh_radio_type": {
+        	"help": "options are ATMEL, MCR20, SPIRIT1, EFR32",
+        	"value": "ATMEL"
+        },
+        "esp8266-tx": {
+            "help": "Pin used as TX (connects to ESP8266 RX)",
+            "value": "D1"
+        },
+        "esp8266-rx": {
+            "help": "Pin used as RX (connects to ESP8266 TX)",
+            "value": "D0"
+        },
+        "esp8266-debug": {
+            "value": true
+        },
+        "wifi-ssid": {
+            "value": "\"SSID\""
+        },
+        "wifi-password": {
+            "value": "\"Password\""
+        }
+    },
     "target_overrides": {
         "*": {
+            "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"],
+            "platform.stdio-baud-rate": 115200,
             "platform.stdio-convert-newlines": true,
-            "target.network-default-interface-type": "ETHERNET"
+            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
+            "mbed-mesh-api.6lowpan-nd-channel": 12,
+            "mbed-trace.enable": 0
+        },
+        "HEXIWEAR": {
+            "esp8266-tx": "PTD3",
+            "esp8266-rx": "PTD2"
+        },
+        "NUCLEO_F401RE": {
+            "esp8266-tx": "D8",
+            "esp8266-rx": "D2"
+        },
+        "NUCLEO_F411RE": {
+            "esp8266-tx": "D8",
+            "esp8266-rx": "D2"
         }
     }
 }