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