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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "mbed.h"
00020 #include "WiflyInterface.h"
00021 
00022 /** On some platforms USBTX/USBRX overlaps with serial on D1/D0 pins and this may interrupt the communication.
00023  *  You can comment it and use an LCD display to print the values or store them on an SD card etc.
00024  */
00025 Serial pc(USBTX, USBRX);
00026 
00027 /**
00028  * D1 - TX pin (RX on the WiFi side)
00029  * D0 - RX pin (TX on the WiFi side)
00030  * NC - Reset pin; use D5 otherwise the shield might get into reset loop
00031  * LED1 - TCP status pin
00032  * "ssid" - hostspot name
00033  * "password" - hotspot passowrd
00034  * security method - NONE, WEP_128, WPA, WPA2
00035  */
00036 WiflyInterface eth(D1, D0, D5, LED1, "hotspot", "", NONE);
00037 
00038 int main()
00039 {
00040     wait(3);
00041     
00042     // Initialize the interface.
00043     // If no param is passed to init() then DHCP will be used on connect()
00044     int s = eth.init();
00045     if (s != NULL) {
00046         printf(">>> Could not initialise. Halting!\r\n");
00047         exit(0);
00048     }
00049 
00050     printf(">>> Get IP address...\r\n");
00051     while (1) {
00052         s = eth.connect(); // Connect to network
00053 
00054         if (s == false || s < 0) {
00055             printf(">>> Could not connect to network. Retrying!\r\n");
00056             wait(3);
00057         } else {
00058             break;
00059         }
00060     }
00061     //printf(">>> Got IP address: %s\n", eth.getIPAddress());
00062 
00063     // Prepare the http request to mbed.org
00064     char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
00065     TCPSocketConnection sock;
00066     sock.connect("developer.mbed.org", 80);    
00067     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00068     //printf(">>> Sent request to mbed.org\n");
00069 
00070     // Read the response
00071     char buffer[300];
00072     int ret;
00073     while (true) {
00074         ret = sock.receive(buffer, sizeof(buffer)-1);
00075         if (ret <= 0)
00076             break;
00077         buffer[ret] = '\0';
00078         printf(">>> Received %d chars from mbed.org:\r\n%s\r\n", ret, buffer);
00079     }
00080     sock.close();
00081     
00082     // Disconnect from network
00083     eth.disconnect();
00084     return 0;
00085 }