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

Committer:
Bongjun
Date:
Mon Aug 13 08:12:45 2018 +0000
Revision:
55:8165a6a797a4
Parent:
49:1923a727df5b
Simple mbed OS sockets example for mbed OS5 and W5500 SPI Ethernet controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 55:8165a6a797a4 1 #include <string>
mbed_official 0:17bd84fc5087 2 #include "mbed.h"
Bongjun 55:8165a6a797a4 3 #include "easy-connect.h"
Bongjun 55:8165a6a797a4 4 #include "nsapi_dns.h"
mbed_official 0:17bd84fc5087 5
mbed_official 5:3e952c60d705 6 // Network interface
mbed_official 49:1923a727df5b 7 NetworkInterface *net;
mbed_official 0:17bd84fc5087 8
mbed_official 0:17bd84fc5087 9 // Socket demo
mbed_official 5:3e952c60d705 10 int main() {
mbed_official 47:08787ef063cb 11 int remaining;
mbed_official 47:08787ef063cb 12 int rcount;
mbed_official 47:08787ef063cb 13 char *p;
mbed_official 47:08787ef063cb 14 char *buffer = new char[256];
mbed_official 47:08787ef063cb 15 nsapi_size_or_error_t r;
mbed_official 47:08787ef063cb 16
mbed_official 5:3e952c60d705 17 // Bring up the ethernet interface
mbed_official 49:1923a727df5b 18 printf("Mbed OS Socket example\n");
mbed_official 48:f2739ac5cb01 19
mbed_official 48:f2739ac5cb01 20 #ifdef MBED_MAJOR_VERSION
mbed_official 49:1923a727df5b 21 printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
mbed_official 48:f2739ac5cb01 22 #endif
mbed_official 48:f2739ac5cb01 23
Bongjun 55:8165a6a797a4 24 // net = NetworkInterface::get_default_instance();
mbed_official 49:1923a727df5b 25
Bongjun 55:8165a6a797a4 26 printf("Easy connect...\n");
Bongjun 55:8165a6a797a4 27 net = easy_connect(true);
Bongjun 55:8165a6a797a4 28 if (!net) {
Bongjun 55:8165a6a797a4 29 printf("Cannot connect to the network, see serial output");
Bongjun 55:8165a6a797a4 30 return 1;
Bongjun 55:8165a6a797a4 31 }
Bongjun 55:8165a6a797a4 32 printf("Connected to the network. Opening a socket...\n");
Bongjun 55:8165a6a797a4 33
mbed_official 49:1923a727df5b 34 if (!net) {
mbed_official 49:1923a727df5b 35 printf("Error! No network inteface found.\n");
mbed_official 49:1923a727df5b 36 return 0;
mbed_official 49:1923a727df5b 37 }
mbed_official 49:1923a727df5b 38
mbed_official 49:1923a727df5b 39 r = net->connect();
mbed_official 47:08787ef063cb 40 if (r != 0) {
mbed_official 49:1923a727df5b 41 printf("Error! net->connect() returned: %d\n", r);
mbed_official 47:08787ef063cb 42 }
mbed_official 0:17bd84fc5087 43
mbed_official 0:17bd84fc5087 44 // Show the network address
mbed_official 49:1923a727df5b 45 const char *ip = net->get_ip_address();
mbed_official 49:1923a727df5b 46 const char *netmask = net->get_netmask();
mbed_official 49:1923a727df5b 47 const char *gateway = net->get_gateway();
mbed_official 13:ed9e4aa00044 48 printf("IP address: %s\n", ip ? ip : "None");
mbed_official 13:ed9e4aa00044 49 printf("Netmask: %s\n", netmask ? netmask : "None");
mbed_official 13:ed9e4aa00044 50 printf("Gateway: %s\n", gateway ? gateway : "None");
mbed_official 0:17bd84fc5087 51
mbed_official 0:17bd84fc5087 52 // Open a socket on the network interface, and create a TCP connection to mbed.org
mbed_official 5:3e952c60d705 53 TCPSocket socket;
mbed_official 49:1923a727df5b 54 r = socket.open(net);
mbed_official 47:08787ef063cb 55 if (r != 0) {
mbed_official 47:08787ef063cb 56 printf("Error! socket.open() returned: %d\n", r);
mbed_official 47:08787ef063cb 57 }
Bongjun 55:8165a6a797a4 58
mbed_official 47:08787ef063cb 59 r = socket.connect("api.ipify.org", 80);
Bongjun 55:8165a6a797a4 60 //r = socket.connect("23.23.114.123", 80);
Bongjun 55:8165a6a797a4 61
mbed_official 47:08787ef063cb 62 if (r != 0) {
mbed_official 47:08787ef063cb 63 printf("Error! socket.connect() returned: %d\n", r);
mbed_official 47:08787ef063cb 64 }
mbed_official 47:08787ef063cb 65
mbed_official 47:08787ef063cb 66 // Send a simple http request
mbed_official 47:08787ef063cb 67 char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
mbed_official 47:08787ef063cb 68 nsapi_size_t size = strlen(sbuffer);
mbed_official 0:17bd84fc5087 69
mbed_official 47:08787ef063cb 70 // Loop until whole request send
mbed_official 47:08787ef063cb 71 while(size) {
mbed_official 47:08787ef063cb 72 r = socket.send(sbuffer+r, size);
mbed_official 47:08787ef063cb 73 if (r < 0) {
mbed_official 47:08787ef063cb 74 printf("Error! socket.connect() returned: %d\n", r);
mbed_official 47:08787ef063cb 75 goto disconnect;
mbed_official 47:08787ef063cb 76 }
mbed_official 47:08787ef063cb 77 size -= r;
mbed_official 47:08787ef063cb 78 printf("sent %d [%.*s]\n", r, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 40:afef93b6d854 79 }
mbed_official 47:08787ef063cb 80
mbed_official 47:08787ef063cb 81 // Receieve an HTTP response and print out the response line
mbed_official 47:08787ef063cb 82 remaining = 256;
mbed_official 47:08787ef063cb 83 rcount = 0;
mbed_official 47:08787ef063cb 84 p = buffer;
Bongjun 55:8165a6a797a4 85
Bongjun 55:8165a6a797a4 86 r = socket.recv(p, remaining);
Bongjun 55:8165a6a797a4 87 /*
Bongjun 55:8165a6a797a4 88 // modified source..a bit strnage...
mbed_official 47:08787ef063cb 89 while (0 < (r = socket.recv(p, remaining))) {
mbed_official 47:08787ef063cb 90 p += r;
mbed_official 47:08787ef063cb 91 rcount += r;
mbed_official 47:08787ef063cb 92 remaining -= r;
mbed_official 47:08787ef063cb 93 }
Bongjun 55:8165a6a797a4 94 */
mbed_official 47:08787ef063cb 95 if (r < 0) {
mbed_official 47:08787ef063cb 96 printf("Error! socket.recv() returned: %d\n", r);
mbed_official 47:08787ef063cb 97 goto disconnect;
mbed_official 47:08787ef063cb 98 }
mbed_official 47:08787ef063cb 99
Bongjun 55:8165a6a797a4 100 p += r;
Bongjun 55:8165a6a797a4 101 rcount += r;
Bongjun 55:8165a6a797a4 102 remaining -= r;
Bongjun 55:8165a6a797a4 103
mbed_official 13:ed9e4aa00044 104 printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 13:ed9e4aa00044 105
mbed_official 13:ed9e4aa00044 106 // The api.ipify.org service also gives us the device's external IP address
mbed_official 47:08787ef063cb 107 p = strstr(buffer, "\r\n\r\n")+4;
mbed_official 47:08787ef063cb 108 printf("External IP address: %.*s\n", rcount-(p-buffer), p);
mbed_official 47:08787ef063cb 109 delete[] buffer;
mbed_official 0:17bd84fc5087 110
mbed_official 47:08787ef063cb 111 disconnect:
mbed_official 0:17bd84fc5087 112 // Close the socket to return its memory and bring down the network interface
mbed_official 0:17bd84fc5087 113 socket.close();
mbed_official 0:17bd84fc5087 114
mbed_official 5:3e952c60d705 115 // Bring down the ethernet interface
mbed_official 49:1923a727df5b 116 net->disconnect();
mbed_official 0:17bd84fc5087 117 printf("Done\n");
mbed_official 0:17bd84fc5087 118 }