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
main.cpp@11:e26d01206ed4, 2015-03-18 (annotated)
- Committer:
- screamer
- Date:
- Wed Mar 18 15:11:40 2015 +0000
- Revision:
- 11:e26d01206ed4
- Parent:
- 8:f8bb043cf183
Update the host url to developer.mbed.org and update the mbed lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 7:5aef8fc8996d | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
screamer | 7:5aef8fc8996d | 2 | * |
screamer | 7:5aef8fc8996d | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
screamer | 7:5aef8fc8996d | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
screamer | 7:5aef8fc8996d | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
screamer | 7:5aef8fc8996d | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
screamer | 7:5aef8fc8996d | 7 | * Software is furnished to do so, subject to the following conditions: |
screamer | 7:5aef8fc8996d | 8 | * |
screamer | 7:5aef8fc8996d | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
screamer | 7:5aef8fc8996d | 10 | * substantial portions of the Software. |
screamer | 7:5aef8fc8996d | 11 | * |
screamer | 7:5aef8fc8996d | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
screamer | 7:5aef8fc8996d | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
screamer | 7:5aef8fc8996d | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
screamer | 7:5aef8fc8996d | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
screamer | 7:5aef8fc8996d | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
screamer | 7:5aef8fc8996d | 17 | */ |
screamer | 7:5aef8fc8996d | 18 | |
screamer | 0:f2524261196f | 19 | #include "mbed.h" |
screamer | 0:f2524261196f | 20 | #include "WiflyInterface.h" |
screamer | 0:f2524261196f | 21 | |
screamer | 4:c45472e3e931 | 22 | /** On some platforms USBTX/USBRX overlaps with serial on D1/D0 pins and this may interrupt the communication. |
screamer | 4:c45472e3e931 | 23 | * You can comment it and use an LCD display to print the values or store them on an SD card etc. |
screamer | 2:61d5dde0c4b4 | 24 | */ |
screamer | 4:c45472e3e931 | 25 | Serial pc(USBTX, USBRX); |
screamer | 1:ced62d2a69f9 | 26 | |
screamer | 0:f2524261196f | 27 | /** |
screamer | 0:f2524261196f | 28 | * D1 - TX pin (RX on the WiFi side) |
screamer | 0:f2524261196f | 29 | * D0 - RX pin (TX on the WiFi side) |
screamer | 1:ced62d2a69f9 | 30 | * NC - Reset pin; use D5 otherwise the shield might get into reset loop |
screamer | 0:f2524261196f | 31 | * LED1 - TCP status pin |
screamer | 0:f2524261196f | 32 | * "ssid" - hostspot name |
screamer | 0:f2524261196f | 33 | * "password" - hotspot passowrd |
screamer | 0:f2524261196f | 34 | * security method - NONE, WEP_128, WPA, WPA2 |
screamer | 0:f2524261196f | 35 | */ |
screamer | 1:ced62d2a69f9 | 36 | WiflyInterface eth(D1, D0, D5, LED1, "hotspot", "", NONE); |
screamer | 0:f2524261196f | 37 | |
screamer | 0:f2524261196f | 38 | int main() |
screamer | 0:f2524261196f | 39 | { |
screamer | 1:ced62d2a69f9 | 40 | wait(3); |
screamer | 1:ced62d2a69f9 | 41 | |
screamer | 1:ced62d2a69f9 | 42 | // Initialize the interface. |
screamer | 1:ced62d2a69f9 | 43 | // If no param is passed to init() then DHCP will be used on connect() |
screamer | 1:ced62d2a69f9 | 44 | int s = eth.init(); |
screamer | 1:ced62d2a69f9 | 45 | if (s != NULL) { |
screamer | 8:f8bb043cf183 | 46 | printf(">>> Could not initialise. Halting!\r\n"); |
screamer | 0:f2524261196f | 47 | exit(0); |
screamer | 0:f2524261196f | 48 | } |
screamer | 0:f2524261196f | 49 | |
screamer | 8:f8bb043cf183 | 50 | printf(">>> Get IP address...\r\n"); |
screamer | 0:f2524261196f | 51 | while (1) { |
screamer | 1:ced62d2a69f9 | 52 | s = eth.connect(); // Connect to network |
screamer | 0:f2524261196f | 53 | |
screamer | 1:ced62d2a69f9 | 54 | if (s == false || s < 0) { |
screamer | 8:f8bb043cf183 | 55 | printf(">>> Could not connect to network. Retrying!\r\n"); |
screamer | 0:f2524261196f | 56 | wait(3); |
screamer | 0:f2524261196f | 57 | } else { |
screamer | 1:ced62d2a69f9 | 58 | break; |
screamer | 0:f2524261196f | 59 | } |
screamer | 0:f2524261196f | 60 | } |
screamer | 6:f80c278ca79c | 61 | //printf(">>> Got IP address: %s\n", eth.getIPAddress()); |
screamer | 1:ced62d2a69f9 | 62 | |
screamer | 1:ced62d2a69f9 | 63 | // Prepare the http request to mbed.org |
screamer | 1:ced62d2a69f9 | 64 | char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; |
screamer | 1:ced62d2a69f9 | 65 | TCPSocketConnection sock; |
screamer | 11:e26d01206ed4 | 66 | sock.connect("developer.mbed.org", 80); |
screamer | 1:ced62d2a69f9 | 67 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
screamer | 6:f80c278ca79c | 68 | //printf(">>> Sent request to mbed.org\n"); |
screamer | 1:ced62d2a69f9 | 69 | |
screamer | 1:ced62d2a69f9 | 70 | // Read the response |
screamer | 1:ced62d2a69f9 | 71 | char buffer[300]; |
screamer | 1:ced62d2a69f9 | 72 | int ret; |
screamer | 1:ced62d2a69f9 | 73 | while (true) { |
screamer | 1:ced62d2a69f9 | 74 | ret = sock.receive(buffer, sizeof(buffer)-1); |
screamer | 1:ced62d2a69f9 | 75 | if (ret <= 0) |
screamer | 1:ced62d2a69f9 | 76 | break; |
screamer | 1:ced62d2a69f9 | 77 | buffer[ret] = '\0'; |
screamer | 8:f8bb043cf183 | 78 | printf(">>> Received %d chars from mbed.org:\r\n%s\r\n", ret, buffer); |
screamer | 1:ced62d2a69f9 | 79 | } |
screamer | 1:ced62d2a69f9 | 80 | sock.close(); |
screamer | 1:ced62d2a69f9 | 81 | |
screamer | 1:ced62d2a69f9 | 82 | // Disconnect from network |
screamer | 1:ced62d2a69f9 | 83 | eth.disconnect(); |
screamer | 1:ced62d2a69f9 | 84 | return 0; |
screamer | 0:f2524261196f | 85 | } |