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, committed 2016-12-15
- Comitter:
- mbed_official
- Date:
- Thu Dec 15 16:15:05 2016 +0000
- Parent:
- 4:fcbe2bb4e326
- Child:
- 6:e3efa409b96b
- Commit message:
- Updated mbed-os 7b974ca -> 532da71
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets
Changed in this revision
--- a/README.md Mon Nov 28 16:00:06 2016 +0000 +++ b/README.md Thu Dec 15 16:15:05 2016 +0000 @@ -6,21 +6,9 @@ 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 -``` +**Note:** The current example is limited to the ethernet interface on supported devices. +To use the example with a different interface, you will need to modify main.cpp and +replace the EthernetInterface class with the appropriate interface. ### Documentation ###
--- a/esp8266-driver.lib Mon Nov 28 16:00:06 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://github.com/armmbed/esp8266-driver/#dc37b65ca877d969aa492f348626df6e1b0b1df0
--- 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(ð); - - // Brings down the ethernet interface - eth.disconnect(); - - printf("Done\n"); -} -#endif
--- a/mbed-os.lib Mon Nov 28 16:00:06 2016 +0000 +++ b/mbed-os.lib Thu Dec 15 16:15:05 2016 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#d5de476f74dd4de27012eb74ede078f6330dfc3f +https://github.com/ARMmbed/mbed-os/#532da7133f7d51db958eacb2b5f13e7cf7feb81f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Dec 15 16:15:05 2016 +0000 @@ -0,0 +1,7 @@ +{ + "target_overrides": { + "UBLOX_EVK_ODIN_W2": { + "target.device_has_remove": ["EMAC"] + } + } +}