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.

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Wed Oct 26 12:00:09 2016 +0100
Child:
1:dc440a92c2c7
Commit message:
Initial commit
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
esp8266-driver.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-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Oct 26 12:00:09 2016 +0100
@@ -0,0 +1,27 @@
+### Getting started with the network-socket API ###
+
+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](github.com/armmbed/mbed-os).
+
+The program brings up an underlying network interface, and uses it to perform an HTTP
+transaction over a TCPSocket.
+
+### Ethernet ###
+
+By default, the example should use the ethernet interface available on the board. Simply
+compile, flash, and run.
+
+### WiFi ###
+
+To enable WiFi, you will need an [esp8266](https://developer.mbed.org/teams/ESP8266/).
+To enable WiFi in the example, you will need to define three defines during compile time.
+
+``` bash
+-DMBED_DEMO_WIFI            # enables wifi
+-DMBED_DEMO_WIFI_SSID=ssid  # ssid
+-DMBED_DEMO_WIFI_PASS=pass  # passphrase
+```
+
+### Documentation ###
+
+More information on the network-socket API can be found in the [mbed handbook](https://docs.mbed.com/docs/mbed-os-api-reference/en/5.1/APIs/communication/network_sockets/).
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/esp8266-driver.lib	Wed Oct 26 12:00:09 2016 +0100
@@ -0,0 +1,1 @@
+https://github.com/armmbed/esp8266-driver/#29d63ae2ee0a233e2fbd9577cdddc7661bb783d1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 26 12:00:09 2016 +0100
@@ -0,0 +1,74 @@
+#include "mbed.h"
+#include "TCPSocket.h"
+
+#define STR(x) STR2(x)
+#define STR2(x) #x
+
+
+// Socket demo
+void http_demo(NetworkInterface *net) {
+    TCPSocket socket;
+
+    // Show the network 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);
+    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);
+
+    // 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);
+
+    // 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();
+
+    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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Oct 26 12:00:09 2016 +0100
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e435a07d9252f133ea3d9f6c95dfb176f32ab9b6