![](/media/cache/profiles/e5c6f944080958c264936693c43f8aaa.50x50_q85.jpg)
HTTP(GET/POST) client see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
main.cpp@0:940529a06f88, 2013-05-20 (annotated)
- Committer:
- gsfan
- Date:
- Mon May 20 01:29:00 2013 +0000
- Revision:
- 0:940529a06f88
- Child:
- 1:eddcf7c7f8a1
1st build
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 0:940529a06f88 | 1 | /* |
gsfan | 0:940529a06f88 | 2 | * HTTP client and NTP 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 | 0:940529a06f88 | 11 | |
gsfan | 0:940529a06f88 | 12 | #define SECURE GSwifi::GSSEC_WPA_PSK |
gsfan | 0:940529a06f88 | 13 | #define SSID "SSID" |
gsfan | 0:940529a06f88 | 14 | #define PASS "PASSPHRASE" |
gsfan | 0:940529a06f88 | 15 | |
gsfan | 0:940529a06f88 | 16 | GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control) |
gsfan | 0:940529a06f88 | 17 | //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm |
gsfan | 0:940529a06f88 | 18 | |
gsfan | 0:940529a06f88 | 19 | Serial pc(USBTX, USBRX); |
gsfan | 0:940529a06f88 | 20 | DigitalOut led1(LED1), led2(LED2); |
gsfan | 0:940529a06f88 | 21 | |
gsfan | 0:940529a06f88 | 22 | extern "C" |
gsfan | 0:940529a06f88 | 23 | void HardFault_Handler() { |
gsfan | 0:940529a06f88 | 24 | printf("Hard Fault!\r\n"); |
gsfan | 0:940529a06f88 | 25 | gs.disconnect(); |
gsfan | 0:940529a06f88 | 26 | exit(-1); |
gsfan | 0:940529a06f88 | 27 | } |
gsfan | 0:940529a06f88 | 28 | |
gsfan | 0:940529a06f88 | 29 | void onGsReceive (int cid, int len) { |
gsfan | 0:940529a06f88 | 30 | int i; |
gsfan | 0:940529a06f88 | 31 | char buf[GS_DATA_SIZE + 1]; |
gsfan | 0:940529a06f88 | 32 | |
gsfan | 0:940529a06f88 | 33 | led2 = 1; |
gsfan | 0:940529a06f88 | 34 | i = gs.recv(cid, buf, len); |
gsfan | 0:940529a06f88 | 35 | buf[i] = 0; |
gsfan | 0:940529a06f88 | 36 | pc.printf(buf); |
gsfan | 0:940529a06f88 | 37 | } |
gsfan | 0:940529a06f88 | 38 | |
gsfan | 0:940529a06f88 | 39 | int main () { |
gsfan | 0:940529a06f88 | 40 | int r; |
gsfan | 0:940529a06f88 | 41 | IpAddr ipaddr, netmask, gateway, nameserver; |
gsfan | 0:940529a06f88 | 42 | Host host; |
gsfan | 0:940529a06f88 | 43 | |
gsfan | 0:940529a06f88 | 44 | led1 = 1; |
gsfan | 0:940529a06f88 | 45 | pc.baud(115200); |
gsfan | 0:940529a06f88 | 46 | |
gsfan | 0:940529a06f88 | 47 | pc.printf("connecting...\r\n"); |
gsfan | 0:940529a06f88 | 48 | if (gs.connect(SECURE, SSID, PASS)) { |
gsfan | 0:940529a06f88 | 49 | return -1; |
gsfan | 0:940529a06f88 | 50 | } |
gsfan | 0:940529a06f88 | 51 | gs.getAddress(ipaddr, netmask, gateway, nameserver); |
gsfan | 0:940529a06f88 | 52 | pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
gsfan | 0:940529a06f88 | 53 | |
gsfan | 0:940529a06f88 | 54 | pc.printf("httpGet\r\n"); |
gsfan | 0:940529a06f88 | 55 | host.setName(HTTP_HOST); |
gsfan | 0:940529a06f88 | 56 | host.setPort(HTTP_PORT); |
gsfan | 0:940529a06f88 | 57 | r = gs.httpGet(host, HTTP_URI, 0, &onGsReceive); |
gsfan | 0:940529a06f88 | 58 | if (r >= 0) { |
gsfan | 0:940529a06f88 | 59 | for (;;) { |
gsfan | 0:940529a06f88 | 60 | gs.poll(); |
gsfan | 0:940529a06f88 | 61 | if (! gs.isConnected(r)) break; |
gsfan | 0:940529a06f88 | 62 | |
gsfan | 0:940529a06f88 | 63 | wait_ms(50); |
gsfan | 0:940529a06f88 | 64 | led1 = !led1; |
gsfan | 0:940529a06f88 | 65 | led2 = 0; |
gsfan | 0:940529a06f88 | 66 | } |
gsfan | 0:940529a06f88 | 67 | } |
gsfan | 0:940529a06f88 | 68 | |
gsfan | 0:940529a06f88 | 69 | pc.printf("exit\r\n"); |
gsfan | 0:940529a06f88 | 70 | } |