see http://mbed.org/users/gsfan/code/GSwifi/

Dependencies:   GSwifi mbed

Committer:
gsfan
Date:
Tue Feb 26 03:33:34 2013 +0000
Revision:
0:7826480a54f0
1st build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 0:7826480a54f0 1 /*
gsfan 0:7826480a54f0 2 * Websocket for GSwifi
gsfan 0:7826480a54f0 3 *
gsfan 0:7826480a54f0 4 * mbed WebSocket server / GSwifi client
gsfan 0:7826480a54f0 5 * html access http://sockets.mbed.org/ws/UserName/test/
gsfan 0:7826480a54f0 6 *
gsfan 0:7826480a54f0 7 * GSwifi WebSocket server / other client
gsfan 0:7826480a54f0 8 * GSwifi_conf.h: #define GS_USE_WEBSOCKET
gsfan 0:7826480a54f0 9 * ws access ws://IP address/ws/hoge
gsfan 0:7826480a54f0 10 */
gsfan 0:7826480a54f0 11
gsfan 0:7826480a54f0 12 #include "mbed.h"
gsfan 0:7826480a54f0 13 #include "GSwifi.h"
gsfan 0:7826480a54f0 14
gsfan 0:7826480a54f0 15 #define PORT 80
gsfan 0:7826480a54f0 16
gsfan 0:7826480a54f0 17 #define WS_HOST "sockets.mbed.org"
gsfan 0:7826480a54f0 18 #define WS_URI "/ws/UserName/test/rw"
gsfan 0:7826480a54f0 19
gsfan 0:7826480a54f0 20 #define SECURE GSwifi::GSSEC_WPA_PSK
gsfan 0:7826480a54f0 21 #define SSID "SSID"
gsfan 0:7826480a54f0 22 #define PASS "PASSPHRASE"
gsfan 0:7826480a54f0 23
gsfan 0:7826480a54f0 24 GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
gsfan 0:7826480a54f0 25 //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
gsfan 0:7826480a54f0 26
gsfan 0:7826480a54f0 27 LocalFileSystem local("local");
gsfan 0:7826480a54f0 28 Serial pc(USBTX, USBRX);
gsfan 0:7826480a54f0 29 DigitalOut led1(LED1), led2(LED2);
gsfan 0:7826480a54f0 30
gsfan 0:7826480a54f0 31 void ws_server (int cid, GSwifi::GS_httpd *gshttpd) {
gsfan 0:7826480a54f0 32
gsfan 0:7826480a54f0 33 pc.printf("WS_S %d: %s ? %s '%s' %d\r\n", cid, gshttpd->file, gshttpd->query, gshttpd->buf, gshttpd->len);
gsfan 0:7826480a54f0 34
gsfan 0:7826480a54f0 35 gs.wsSend(cid, gshttpd->buf, gshttpd->len);
gsfan 0:7826480a54f0 36 }
gsfan 0:7826480a54f0 37
gsfan 0:7826480a54f0 38 void ws_client (int cid, int len) {
gsfan 0:7826480a54f0 39 int n;
gsfan 0:7826480a54f0 40 char buf[len + 1];
gsfan 0:7826480a54f0 41
gsfan 0:7826480a54f0 42 n = gs.recv(cid, buf, sizeof(buf));
gsfan 0:7826480a54f0 43 buf[n] = 0;
gsfan 0:7826480a54f0 44 pc.printf("WS_C %d: %s %d\r\n", cid, buf, n);
gsfan 0:7826480a54f0 45 }
gsfan 0:7826480a54f0 46
gsfan 0:7826480a54f0 47 int main () {
gsfan 0:7826480a54f0 48 IpAddr ipaddr, netmask, gateway, nameserver;
gsfan 0:7826480a54f0 49 Host host;
gsfan 0:7826480a54f0 50 int cid;
gsfan 0:7826480a54f0 51
gsfan 0:7826480a54f0 52 led1 = 1;
gsfan 0:7826480a54f0 53 pc.baud(115200);
gsfan 0:7826480a54f0 54
gsfan 0:7826480a54f0 55 pc.printf("connecting...\r\n");
gsfan 0:7826480a54f0 56 if (gs.connect(SECURE, SSID, PASS)) {
gsfan 0:7826480a54f0 57 return -1;
gsfan 0:7826480a54f0 58 }
gsfan 0:7826480a54f0 59 gs.getAddress(ipaddr, netmask, gateway, nameserver);
gsfan 0:7826480a54f0 60 pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
gsfan 0:7826480a54f0 61
gsfan 0:7826480a54f0 62 led2 = 1;
gsfan 0:7826480a54f0 63 pc.printf("httpd\r\n");
gsfan 0:7826480a54f0 64 gs.httpd(PORT);
gsfan 0:7826480a54f0 65 gs.attach_httpd("/ws/", &ws_server);
gsfan 0:7826480a54f0 66 gs.attach_httpd("/", "/local/");
gsfan 0:7826480a54f0 67
gsfan 0:7826480a54f0 68 host.setName(WS_HOST);
gsfan 0:7826480a54f0 69 host.setPort(PORT);
gsfan 0:7826480a54f0 70 cid = gs.wsOpen(host, WS_URI, &ws_client);
gsfan 0:7826480a54f0 71 if (cid < 0) return -1;
gsfan 0:7826480a54f0 72 printf("ws open\r\n");
gsfan 0:7826480a54f0 73
gsfan 0:7826480a54f0 74 for (;;) {
gsfan 0:7826480a54f0 75 gs.poll();
gsfan 0:7826480a54f0 76
gsfan 0:7826480a54f0 77 if (pc.readable()) {
gsfan 0:7826480a54f0 78 char buf[] = "MBED= ";
gsfan 0:7826480a54f0 79 buf[5] = pc.getc();
gsfan 0:7826480a54f0 80 if (gs.wsSend(cid, buf, 6, "MASK")) {
gsfan 0:7826480a54f0 81 printf("ws error\r\n");
gsfan 0:7826480a54f0 82 }
gsfan 0:7826480a54f0 83 }
gsfan 0:7826480a54f0 84
gsfan 0:7826480a54f0 85 wait_ms(50);
gsfan 0:7826480a54f0 86 led1 = !led1;
gsfan 0:7826480a54f0 87 led2 = 0;
gsfan 0:7826480a54f0 88 }
gsfan 0:7826480a54f0 89 }