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