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:
- 1:bb040724d21b
- Parent:
- 0:1e952439b254
- Child:
- 2:3b6c391a682d
--- a/main.cpp Fri Apr 11 13:07:20 2014 +0000 +++ b/main.cpp Fri Apr 11 13:13:51 2014 +0000 @@ -7,16 +7,18 @@ int main() { wait(3); - pc.printf(">>> Get IP via DHCP...\n"); - int s = eth.init(); // Use DHCP - + + // Initialize the interface. + // If no param is passed to init() then DHCP will be used on connect() + int s = eth.init(); if (s != NULL) { pc.printf(">>> Could not initialise. Halting!\n"); exit(0); } + pc.printf(">>> Get IP address...\n"); while (1) { - s = eth.connect(); + s = eth.connect(); // Connect to network if (s == false || s < 0) { pc.printf(">>> Could not connect to network. Retrying!\n"); @@ -25,16 +27,16 @@ break; } } + pc.printf(">>> Got IP address: %s\n", eth.getIPAddress()); - // successful DHCP - pc.printf(">>> Got IP Address: %s\n", eth.getIPAddress()); - + // Prepare the http request to mbed.org 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"); + // Read the response char buffer[300]; int ret; while (true) { @@ -46,6 +48,7 @@ } sock.close(); + // Disconnect from network eth.disconnect(); return 0; }