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.

Revision:
5:3e952c60d705
Parent:
0:17bd84fc5087
Child:
12:2e7466eba9a3
--- a/main.cpp	Mon Nov 28 16:00:06 2016 +0000
+++ b/main.cpp	Thu Dec 15 16:15:05 2016 +0000
@@ -1,74 +1,38 @@
 #include "mbed.h"
-#include "TCPSocket.h"
+#include "EthernetInterface.h"
 
-#define STR(x) STR2(x)
-#define STR2(x) #x
-
+// Network interface
+EthernetInterface net;
 
 // Socket demo
-void http_demo(NetworkInterface *net) {
-    TCPSocket socket;
+int main() {
+    // Bring up the ethernet interface
+    printf("Ethernet socket example\n");
+    net.connect();
 
     // Show the network address
-    const char *ip = net->get_ip_address();
+    const char *ip = net.get_ip_address();
     printf("IP address is: %s\n", ip ? ip : "No IP");
 
     // Open a socket on the network interface, and create a TCP connection to mbed.org
-    socket.open(net);
+    TCPSocket socket;
+    socket.open(&net);
     socket.connect("developer.mbed.org", 80);
 
     // Send a simple http request
     char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
     int scount = socket.send(sbuffer, sizeof sbuffer);
-    printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+    printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
 
     // Recieve a simple http response and print out the response line
     char rbuffer[64];
     int rcount = socket.recv(rbuffer, sizeof rbuffer);
-    printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
+    printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
 
     // Close the socket to return its memory and bring down the network interface
     socket.close();
-}
 
-
-// Example with the ESP8266 interface
-#if defined(MBED_DEMO_WIFI)
-#include "ESP8266Interface.h"
-
-ESP8266Interface wifi(D1, D0);
-
-int main() {
-    // Brings up the esp8266
-    printf("ESP8266 socket example\n");
-    wifi.connect(STR(MBED_DEMO_WIFI_SSID), STR(MBED_DEMO_WIFI_PASS));
-
-    // Invoke the demo
-    http_demo(&wifi);
-
-    // Brings down the esp8266 
-    wifi.disconnect();
-
+    // Bring down the ethernet interface
+    net.disconnect();
     printf("Done\n");
 }
-
-// Example using the builtin ethernet interface
-#else
-#include "EthernetInterface.h"
-
-EthernetInterface eth;
-
-int main() {
-    // Brings up the ethernet interface
-    printf("Ethernet socket example\n");
-    eth.connect();
-
-    // Invoke the demo
-    http_demo(&eth);
-
-    // Brings down the ethernet interface
-    eth.disconnect();
-
-    printf("Done\n");
-}
-#endif