HTTP(GET/POST) client see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependencies:   GSwifi mbed

Committer:
gsfan
Date:
Mon May 20 01:32:13 2013 +0000
Revision:
1:eddcf7c7f8a1
Parent:
0:940529a06f88
fix;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 0:940529a06f88 1 /*
gsfan 1:eddcf7c7f8a1 2 * HTTP(GET/POST) client
gsfan 0:940529a06f88 3 */
gsfan 0:940529a06f88 4
gsfan 0:940529a06f88 5 #include "mbed.h"
gsfan 0:940529a06f88 6 #include "GSwifi.h"
gsfan 0:940529a06f88 7
gsfan 0:940529a06f88 8 #define HTTP_HOST "www.mbed.org"
gsfan 0:940529a06f88 9 #define HTTP_PORT 80
gsfan 0:940529a06f88 10 #define HTTP_URI "/"
gsfan 1:eddcf7c7f8a1 11 #define HTTP_BODY "KEY=VALUE&key=value"
gsfan 0:940529a06f88 12
gsfan 0:940529a06f88 13 #define SECURE GSwifi::GSSEC_WPA_PSK
gsfan 0:940529a06f88 14 #define SSID "SSID"
gsfan 0:940529a06f88 15 #define PASS "PASSPHRASE"
gsfan 0:940529a06f88 16
gsfan 0:940529a06f88 17 GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
gsfan 0:940529a06f88 18 //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
gsfan 0:940529a06f88 19
gsfan 0:940529a06f88 20 Serial pc(USBTX, USBRX);
gsfan 0:940529a06f88 21 DigitalOut led1(LED1), led2(LED2);
gsfan 0:940529a06f88 22
gsfan 0:940529a06f88 23 extern "C"
gsfan 0:940529a06f88 24 void HardFault_Handler() {
gsfan 0:940529a06f88 25 printf("Hard Fault!\r\n");
gsfan 0:940529a06f88 26 gs.disconnect();
gsfan 0:940529a06f88 27 exit(-1);
gsfan 0:940529a06f88 28 }
gsfan 0:940529a06f88 29
gsfan 0:940529a06f88 30 void onGsReceive (int cid, int len) {
gsfan 0:940529a06f88 31 int i;
gsfan 0:940529a06f88 32 char buf[GS_DATA_SIZE + 1];
gsfan 0:940529a06f88 33
gsfan 0:940529a06f88 34 led2 = 1;
gsfan 0:940529a06f88 35 i = gs.recv(cid, buf, len);
gsfan 0:940529a06f88 36 buf[i] = 0;
gsfan 0:940529a06f88 37 pc.printf(buf);
gsfan 0:940529a06f88 38 }
gsfan 0:940529a06f88 39
gsfan 0:940529a06f88 40 int main () {
gsfan 0:940529a06f88 41 int r;
gsfan 0:940529a06f88 42 IpAddr ipaddr, netmask, gateway, nameserver;
gsfan 0:940529a06f88 43 Host host;
gsfan 0:940529a06f88 44
gsfan 0:940529a06f88 45 led1 = 1;
gsfan 0:940529a06f88 46 pc.baud(115200);
gsfan 0:940529a06f88 47
gsfan 0:940529a06f88 48 pc.printf("connecting...\r\n");
gsfan 0:940529a06f88 49 if (gs.connect(SECURE, SSID, PASS)) {
gsfan 0:940529a06f88 50 return -1;
gsfan 0:940529a06f88 51 }
gsfan 0:940529a06f88 52 gs.getAddress(ipaddr, netmask, gateway, nameserver);
gsfan 0:940529a06f88 53 pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
gsfan 0:940529a06f88 54
gsfan 0:940529a06f88 55 pc.printf("httpGet\r\n");
gsfan 0:940529a06f88 56 host.setName(HTTP_HOST);
gsfan 0:940529a06f88 57 host.setPort(HTTP_PORT);
gsfan 0:940529a06f88 58 r = gs.httpGet(host, HTTP_URI, 0, &onGsReceive);
gsfan 1:eddcf7c7f8a1 59 // r = gs.httpPost(host, HTTP_URI, HTTP_BODY, 0, &onGsReceive);
gsfan 0:940529a06f88 60 if (r >= 0) {
gsfan 0:940529a06f88 61 for (;;) {
gsfan 0:940529a06f88 62 gs.poll();
gsfan 0:940529a06f88 63 if (! gs.isConnected(r)) break;
gsfan 0:940529a06f88 64
gsfan 0:940529a06f88 65 wait_ms(50);
gsfan 0:940529a06f88 66 led1 = !led1;
gsfan 0:940529a06f88 67 led2 = 0;
gsfan 0:940529a06f88 68 }
gsfan 0:940529a06f88 69 }
gsfan 0:940529a06f88 70
gsfan 0:940529a06f88 71 pc.printf("exit\r\n");
gsfan 0:940529a06f88 72 }