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
Result
- Serial Terminal Log
Diff: main.cpp
- Revision:
- 49:1923a727df5b
- Parent:
- 48:f2739ac5cb01
- Child:
- 55:8165a6a797a4
--- a/main.cpp Mon Jun 18 09:15:03 2018 +0100 +++ b/main.cpp Tue Jun 19 12:30:04 2018 +0100 @@ -1,8 +1,7 @@ #include "mbed.h" -#include "EthernetInterface.h" // Network interface -EthernetInterface net; +NetworkInterface *net; // Socket demo int main() { @@ -13,28 +12,35 @@ nsapi_size_or_error_t r; // Bring up the ethernet interface - printf("Ethernet socket example\n"); + printf("Mbed OS Socket example\n"); #ifdef MBED_MAJOR_VERSION - printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); + printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); #endif - r = net.connect(); + net = NetworkInterface::get_default_instance(); + + if (!net) { + printf("Error! No network inteface found.\n"); + return 0; + } + + r = net->connect(); if (r != 0) { - printf("Error! net.connect() returned: %d\n", r); + printf("Error! net->connect() returned: %d\n", r); } // Show the network address - const char *ip = net.get_ip_address(); - const char *netmask = net.get_netmask(); - const char *gateway = net.get_gateway(); + const char *ip = net->get_ip_address(); + const char *netmask = net->get_netmask(); + const char *gateway = net->get_gateway(); printf("IP address: %s\n", ip ? ip : "None"); printf("Netmask: %s\n", netmask ? netmask : "None"); printf("Gateway: %s\n", gateway ? gateway : "None"); // Open a socket on the network interface, and create a TCP connection to mbed.org TCPSocket socket; - r = socket.open(&net); + r = socket.open(net); if (r != 0) { printf("Error! socket.open() returned: %d\n", r); } @@ -85,6 +91,6 @@ socket.close(); // Bring down the ethernet interface - net.disconnect(); + net->disconnect(); printf("Done\n"); }