Example program for the SeeedStudio WiFi Shield V2.0, based on UART serial port connectivity (D0/D1 pins). This program connects to WiFi hotspot, obtains an IP using DHCP and downloads http://mbed.org/media/uploads/mbed_official/hello.txt
Dependencies: WiflyInterface mbed
Diff: main.cpp
- Revision:
- 1:ced62d2a69f9
- Parent:
- 0:f2524261196f
- Child:
- 2:61d5dde0c4b4
--- a/main.cpp Tue Apr 08 18:40:52 2014 +0000 +++ b/main.cpp Fri Apr 11 15:21:58 2014 +0000 @@ -1,39 +1,64 @@ #include "mbed.h" #include "WiflyInterface.h" +Serial pc(USBTX, USBRX); + /** * D1 - TX pin (RX on the WiFi side) * D0 - RX pin (TX on the WiFi side) - * NC - Reset pin + * NC - Reset pin; use D5 otherwise the shield might get into reset loop * LED1 - TCP status pin * "ssid" - hostspot name * "password" - hotspot passowrd * security method - NONE, WEP_128, WPA, WPA2 */ -WiflyInterface wifi(D1, D0, NC, LED1, "hotspot", "", NONE); -Serial pc(USBTX, USBRX); +WiflyInterface eth(D1, D0, D5, LED1, "hotspot", "", NONE); int main() { - int s = wifi.init(); // Use DHCP - if( s != NULL ) { - printf("Could not initialise. Halting!\n"); + wait(3); + + // Initialize the interface. + // If no param is passed to init() then DHCP will be used on connect() + int s = eth.init(); + if (s != NULL) { + printf(">>> Could not initialise. Halting!\n"); exit(0); } + printf(">>> Get IP address...\n"); while (1) { - s = wifi.connect(); // Set up the chip and join the network + s = eth.connect(); // Connect to network - if( s == false ) { - printf("Could not connect. Retrying!\n"); + if (s == false || s < 0) { + printf(">>> Could not connect to network. Retrying!\n"); wait(3); - continue; } else { - // Print the IP address every second - while(1) { - printf("Got IP: %s\n", wifi.getIPAddress()); - wait(1); - } + break; } } + 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); + printf(">>> Sent request to mbed.org\n"); + + // Read the response + char buffer[300]; + int ret; + while (true) { + ret = sock.receive(buffer, sizeof(buffer)-1); + if (ret <= 0) + break; + buffer[ret] = '\0'; + printf(">>> Received %d chars from mbed.org:\n%s\n", ret, buffer); + } + sock.close(); + + // Disconnect from network + eth.disconnect(); + return 0; }