TCP client see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Fork of GSwifi_tcpserver by
main.cpp@3:7ec5f2741abf, 2012-10-27 (annotated)
- Committer:
- gsfan
- Date:
- Sat Oct 27 00:43:28 2012 +0000
- Revision:
- 3:7ec5f2741abf
- Parent:
- 2:e22671f88297
- Child:
- 4:e6192ed22442
1st build
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 0:f4c1a7a895ee | 1 | #include "mbed.h" |
gsfan | 0:f4c1a7a895ee | 2 | #include "GSwifi.h" |
gsfan | 0:f4c1a7a895ee | 3 | |
gsfan | 2:e22671f88297 | 4 | #define HOST "test.example.com" |
gsfan | 0:f4c1a7a895ee | 5 | #define PORT 10080 |
gsfan | 0:f4c1a7a895ee | 6 | |
gsfan | 0:f4c1a7a895ee | 7 | #define SECURE GSSEC_WPA_PSK |
gsfan | 0:f4c1a7a895ee | 8 | #define SSID "SSID" |
gsfan | 1:fdc165a4df04 | 9 | #define PASS "passkey" |
gsfan | 0:f4c1a7a895ee | 10 | |
gsfan | 1:fdc165a4df04 | 11 | GSwifi gs(p13, p14); // TX, RX (no flow control) |
gsfan | 1:fdc165a4df04 | 12 | // GSwifi gs(p13, p14, p12, P0_22); // TX, RX, CTS, RTS |
gsfan | 0:f4c1a7a895ee | 13 | Serial pc(USBTX, USBRX); |
gsfan | 0:f4c1a7a895ee | 14 | DigitalOut led1(LED1), led2(LED2); |
gsfan | 0:f4c1a7a895ee | 15 | |
gsfan | 0:f4c1a7a895ee | 16 | void onGsReceive (int cid, int len) { |
gsfan | 0:f4c1a7a895ee | 17 | int i; |
gsfan | 0:f4c1a7a895ee | 18 | char buf[100]; |
gsfan | 0:f4c1a7a895ee | 19 | Host host; |
gsfan | 0:f4c1a7a895ee | 20 | |
gsfan | 0:f4c1a7a895ee | 21 | led2 = 1; |
gsfan | 0:f4c1a7a895ee | 22 | |
gsfan | 0:f4c1a7a895ee | 23 | i = gs.recv(cid, buf, sizeof(buf)); |
gsfan | 0:f4c1a7a895ee | 24 | pc.printf("recv %d\r\n", i); |
gsfan | 0:f4c1a7a895ee | 25 | } |
gsfan | 0:f4c1a7a895ee | 26 | |
gsfan | 0:f4c1a7a895ee | 27 | int main () { |
gsfan | 0:f4c1a7a895ee | 28 | IpAddr ipaddr, netmask, gateway, nameserver; |
gsfan | 2:e22671f88297 | 29 | Host host; |
gsfan | 2:e22671f88297 | 30 | int cid; |
gsfan | 0:f4c1a7a895ee | 31 | |
gsfan | 0:f4c1a7a895ee | 32 | led1 = 1; |
gsfan | 0:f4c1a7a895ee | 33 | pc.baud(115200); |
gsfan | 0:f4c1a7a895ee | 34 | |
gsfan | 0:f4c1a7a895ee | 35 | pc.printf("connect\r\n"); |
gsfan | 0:f4c1a7a895ee | 36 | if (gs.connect(SECURE, SSID, PASS)) { |
gsfan | 0:f4c1a7a895ee | 37 | return -1; |
gsfan | 0:f4c1a7a895ee | 38 | } |
gsfan | 0:f4c1a7a895ee | 39 | gs.getAddress(ipaddr, netmask, gateway, nameserver); |
gsfan | 0:f4c1a7a895ee | 40 | pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
gsfan | 0:f4c1a7a895ee | 41 | |
gsfan | 2:e22671f88297 | 42 | pc.printf("connection\r\n"); |
gsfan | 2:e22671f88297 | 43 | host.setName(HOST); |
gsfan | 2:e22671f88297 | 44 | host.setPort(PORT); |
gsfan | 3:7ec5f2741abf | 45 | cid = gs.open(host, GSPROT_TCP, &onGsReceive); // TCP client |
gsfan | 3:7ec5f2741abf | 46 | // cid = gs.open(host, GSPROT_TCP, &onGsReceive); // UDP client |
gsfan | 2:e22671f88297 | 47 | if (cid < 0) { |
gsfan | 2:e22671f88297 | 48 | return -1; |
gsfan | 2:e22671f88297 | 49 | } |
gsfan | 0:f4c1a7a895ee | 50 | |
gsfan | 0:f4c1a7a895ee | 51 | for (;;) { |
gsfan | 0:f4c1a7a895ee | 52 | gs.poll(); |
gsfan | 0:f4c1a7a895ee | 53 | |
gsfan | 2:e22671f88297 | 54 | gs.send(cid, "Hello", 5); |
gsfan | 2:e22671f88297 | 55 | wait_ms(500); |
gsfan | 0:f4c1a7a895ee | 56 | led1 = !led1; |
gsfan | 0:f4c1a7a895ee | 57 | led2 = 0; |
gsfan | 0:f4c1a7a895ee | 58 | } |
gsfan | 0:f4c1a7a895ee | 59 | } |