see: https://developer.mbed.org/users/phsfan/notebook/phsshield/

Dependencies:   a3gs mbed

Fork of PHSShield_httpGET by phs fan

Committer:
phsfan
Date:
Fri Mar 20 05:14:54 2015 +0000
Revision:
1:c2c4efc7a033
Parent:
0:15b79ec85118
Child:
2:12760b453533
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phsfan 0:15b79ec85118 1 #include "mbed.h"
phsfan 0:15b79ec85118 2 #include "a3gs.h"
phsfan 0:15b79ec85118 3
phsfan 0:15b79ec85118 4 Serial pc(USBTX, USBRX);
phsfan 0:15b79ec85118 5 DigitalOut myled(LED1);
phsfan 0:15b79ec85118 6
phsfan 0:15b79ec85118 7 A3GS a3gs(p28, p27, p29, p23, p11, 9600); // tx, rx, interrupt, power, regulator
phsfan 0:15b79ec85118 8
phsfan 0:15b79ec85118 9 const char *server = "developer.mbed.org";
phsfan 0:15b79ec85118 10 const char *path = "/media/uploads/phsfan/hello.txt";
phsfan 0:15b79ec85118 11 const char *head = "Accept: text/html\r\nConnection: Keep-Alive";
phsfan 0:15b79ec85118 12 int port = 80;
phsfan 0:15b79ec85118 13
phsfan 0:15b79ec85118 14 int main() {
phsfan 0:15b79ec85118 15 pc.baud(115200);
phsfan 1:c2c4efc7a033 16 pc.printf("*** PHS httpGET\r\n");
phsfan 0:15b79ec85118 17
phsfan 0:15b79ec85118 18 a3gs.start();
phsfan 0:15b79ec85118 19 if (a3gs.begin()) {
phsfan 0:15b79ec85118 20 error("Could not connect");
phsfan 0:15b79ec85118 21 }
phsfan 0:15b79ec85118 22 myled = 1;
phsfan 0:15b79ec85118 23
phsfan 0:15b79ec85118 24 char buf[a3gsMAX_RESULT_LENGTH + 1];
phsfan 0:15b79ec85118 25 if (a3gs.httpGET(server, port, path, buf, sizeof(buf) - 1, 0, head) == 0) {
phsfan 0:15b79ec85118 26 pc.printf("OK!\r\n");
phsfan 0:15b79ec85118 27 pc.printf(buf);
phsfan 0:15b79ec85118 28 for (;;) {
phsfan 0:15b79ec85118 29 int r = a3gs.read(buf, sizeof(buf) - 1);
phsfan 0:15b79ec85118 30 if (r > 0) {
phsfan 0:15b79ec85118 31 pc.printf(buf);
phsfan 0:15b79ec85118 32 } else {
phsfan 0:15b79ec85118 33 break;
phsfan 0:15b79ec85118 34 }
phsfan 0:15b79ec85118 35 }
phsfan 1:c2c4efc7a033 36 pc.printf("\r\n");
phsfan 0:15b79ec85118 37 } else {
phsfan 0:15b79ec85118 38 pc.printf("Can't get HTTP response from %s\r\n", server);
phsfan 0:15b79ec85118 39 }
phsfan 1:c2c4efc7a033 40
phsfan 1:c2c4efc7a033 41 wait(3);
phsfan 0:15b79ec85118 42 a3gs.disconnectTCP();
phsfan 0:15b79ec85118 43 pc.printf("bye\r\n");
phsfan 0:15b79ec85118 44 a3gs.end();
phsfan 0:15b79ec85118 45 a3gs.shutdown();
phsfan 0:15b79ec85118 46 myled = 0;
phsfan 0:15b79ec85118 47 }