gs fan
/
GSwifi_websocket
see http://mbed.org/users/gsfan/code/GSwifi/
Revision 0:7826480a54f0, committed 2013-02-26
- Comitter:
- gsfan
- Date:
- Tue Feb 26 03:33:34 2013 +0000
- Commit message:
- 1st build
Changed in this revision
diff -r 000000000000 -r 7826480a54f0 GSwifi.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GSwifi.lib Tue Feb 26 03:33:34 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/gsfan/code/GSwifi/#f3e9053d9b46
diff -r 000000000000 -r 7826480a54f0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Feb 26 03:33:34 2013 +0000 @@ -0,0 +1,89 @@ +/* + * Websocket for GSwifi + * + * mbed WebSocket server / GSwifi client + * html access http://sockets.mbed.org/ws/UserName/test/ + * + * GSwifi WebSocket server / other client + * GSwifi_conf.h: #define GS_USE_WEBSOCKET + * ws access ws://IP address/ws/hoge + */ + +#include "mbed.h" +#include "GSwifi.h" + +#define PORT 80 + +#define WS_HOST "sockets.mbed.org" +#define WS_URI "/ws/UserName/test/rw" + +#define SECURE GSwifi::GSSEC_WPA_PSK +#define SSID "SSID" +#define PASS "PASSPHRASE" + +GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control) +//GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm + +LocalFileSystem local("local"); +Serial pc(USBTX, USBRX); +DigitalOut led1(LED1), led2(LED2); + +void ws_server (int cid, GSwifi::GS_httpd *gshttpd) { + + pc.printf("WS_S %d: %s ? %s '%s' %d\r\n", cid, gshttpd->file, gshttpd->query, gshttpd->buf, gshttpd->len); + + gs.wsSend(cid, gshttpd->buf, gshttpd->len); +} + +void ws_client (int cid, int len) { + int n; + char buf[len + 1]; + + n = gs.recv(cid, buf, sizeof(buf)); + buf[n] = 0; + pc.printf("WS_C %d: %s %d\r\n", cid, buf, n); +} + +int main () { + IpAddr ipaddr, netmask, gateway, nameserver; + Host host; + int cid; + + led1 = 1; + pc.baud(115200); + + pc.printf("connecting...\r\n"); + if (gs.connect(SECURE, SSID, PASS)) { + return -1; + } + gs.getAddress(ipaddr, netmask, gateway, nameserver); + pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); + + led2 = 1; + pc.printf("httpd\r\n"); + gs.httpd(PORT); + gs.attach_httpd("/ws/", &ws_server); + gs.attach_httpd("/", "/local/"); + + host.setName(WS_HOST); + host.setPort(PORT); + cid = gs.wsOpen(host, WS_URI, &ws_client); + if (cid < 0) return -1; + printf("ws open\r\n"); + + for (;;) { + gs.poll(); + + if (pc.readable()) { + char buf[] = "MBED= "; + buf[5] = pc.getc(); + if (gs.wsSend(cid, buf, 6, "MASK")) { + printf("ws error\r\n"); + } + } + + wait_ms(50); + led1 = !led1; + led2 = 0; + } +}
diff -r 000000000000 -r 7826480a54f0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Feb 26 03:33:34 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/3d0ef94e36ec \ No newline at end of file