Example program for the Ethernet Shield V2.0 by SeeedStudio. The example gets an IP address via DHCP and then requests http://mbed.org/media/uploads/mbed_official/hello.txt
Dependencies: WIZ820ioInterface mbed
Diff: main.cpp
- Revision:
- 0:1e952439b254
- Child:
- 1:bb040724d21b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Apr 11 13:07:20 2014 +0000 @@ -0,0 +1,51 @@ +#include "mbed.h" +#include "WIZ820ioInterface.h" + +Serial pc(USBTX, USBRX); +WIZ820ioInterface eth(D11, D12, D13, D10, NC); + +int main() +{ + wait(3); + pc.printf(">>> Get IP via DHCP...\n"); + int s = eth.init(); // Use DHCP + + if (s != NULL) { + pc.printf(">>> Could not initialise. Halting!\n"); + exit(0); + } + + while (1) { + s = eth.connect(); + + if (s == false || s < 0) { + pc.printf(">>> Could not connect to network. Retrying!\n"); + wait(3); + } else { + break; + } + } + + // successful DHCP + pc.printf(">>> Got IP Address: %s\n", eth.getIPAddress()); + + char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; + TCPSocketConnection sock; + sock.connect("mbed.org", 80); + sock.send_all(http_cmd, sizeof(http_cmd)-1); + pc.printf(">>> Sent request to mbed.org\n"); + + char buffer[300]; + int ret; + while (true) { + ret = sock.receive(buffer, sizeof(buffer)-1); + if (ret <= 0) + break; + buffer[ret] = '\0'; + pc.printf(">>> Received %d chars from mbed.org:\n%s\n", ret, buffer); + } + sock.close(); + + eth.disconnect(); + return 0; +}