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

Committer:
screamer
Date:
Tue May 13 16:45:06 2014 +0000
Revision:
0:b7a3f482f82c
Child:
1:1204f875bde4
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:b7a3f482f82c 1 #include "mbed.h"
screamer 0:b7a3f482f82c 2 #include "GPRSInterface.h"
screamer 0:b7a3f482f82c 3
screamer 0:b7a3f482f82c 4 /** On many platforms USBTX/USBRX overlap with serial on D1/D0 pins and enabling the below will interrupt the communication.
screamer 0:b7a3f482f82c 5 * You can use an LCD display to print the values or store them on an SD card etc.
screamer 0:b7a3f482f82c 6 */
screamer 0:b7a3f482f82c 7 //Serial pc(USBTX, USBRX);
screamer 0:b7a3f482f82c 8
screamer 0:b7a3f482f82c 9 /**
screamer 0:b7a3f482f82c 10 * D1 - TX pin (RX on the WiFi side)
screamer 0:b7a3f482f82c 11 * D0 - RX pin (TX on the WiFi side)
screamer 0:b7a3f482f82c 12 * 115200 - Baud rate
screamer 0:b7a3f482f82c 13 * "apn" - APN name
screamer 0:b7a3f482f82c 14 * "username" - APN username
screamer 0:b7a3f482f82c 15 * "password" - APN passowrd
screamer 0:b7a3f482f82c 16 */
screamer 0:b7a3f482f82c 17 GPRSInterface eth(D1, D0, 115200, "apn", "username", "password");
screamer 0:b7a3f482f82c 18
screamer 0:b7a3f482f82c 19 int main()
screamer 0:b7a3f482f82c 20 {
screamer 0:b7a3f482f82c 21 wait(3);
screamer 0:b7a3f482f82c 22
screamer 0:b7a3f482f82c 23 // Initialize the interface.
screamer 0:b7a3f482f82c 24 // If no param is passed to init() then DHCP will be used on connect()
screamer 0:b7a3f482f82c 25 int s = eth.init();
screamer 0:b7a3f482f82c 26 if (s != NULL) {
screamer 0:b7a3f482f82c 27 printf(">>> Could not initialise. Halting!\n");
screamer 0:b7a3f482f82c 28 exit(0);
screamer 0:b7a3f482f82c 29 }
screamer 0:b7a3f482f82c 30
screamer 0:b7a3f482f82c 31 printf(">>> Get IP address...\n");
screamer 0:b7a3f482f82c 32 while (1) {
screamer 0:b7a3f482f82c 33 s = eth.connect(); // Connect to network
screamer 0:b7a3f482f82c 34
screamer 0:b7a3f482f82c 35 if (s == false || s < 0) {
screamer 0:b7a3f482f82c 36 printf(">>> Could not connect to network. Retrying!\n");
screamer 0:b7a3f482f82c 37 wait(3);
screamer 0:b7a3f482f82c 38 } else {
screamer 0:b7a3f482f82c 39 break;
screamer 0:b7a3f482f82c 40 }
screamer 0:b7a3f482f82c 41 }
screamer 0:b7a3f482f82c 42 printf(">>> Got IP address: %s\n", eth.getIPAddress());
screamer 0:b7a3f482f82c 43
screamer 0:b7a3f482f82c 44 // Prepare the http request to mbed.org
screamer 0:b7a3f482f82c 45 char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
screamer 0:b7a3f482f82c 46 TCPSocketConnection sock;
screamer 0:b7a3f482f82c 47 sock.connect("mbed.org", 80);
screamer 0:b7a3f482f82c 48 sock.send_all(http_cmd, sizeof(http_cmd)-1);
screamer 0:b7a3f482f82c 49 printf(">>> Sent request to mbed.org\n");
screamer 0:b7a3f482f82c 50
screamer 0:b7a3f482f82c 51 // Read the response
screamer 0:b7a3f482f82c 52 char buffer[300];
screamer 0:b7a3f482f82c 53 int ret;
screamer 0:b7a3f482f82c 54 while (true) {
screamer 0:b7a3f482f82c 55 ret = sock.receive(buffer, sizeof(buffer)-1);
screamer 0:b7a3f482f82c 56 if (ret <= 0)
screamer 0:b7a3f482f82c 57 break;
screamer 0:b7a3f482f82c 58 buffer[ret] = '\0';
screamer 0:b7a3f482f82c 59 printf(">>> Received %d chars from mbed.org:\n%s\n", ret, buffer);
screamer 0:b7a3f482f82c 60 }
screamer 0:b7a3f482f82c 61 sock.close();
screamer 0:b7a3f482f82c 62
screamer 0:b7a3f482f82c 63 // Disconnect from network
screamer 0:b7a3f482f82c 64 eth.disconnect();
screamer 0:b7a3f482f82c 65 return 0;
screamer 0:b7a3f482f82c 66 }