gs fan / Mbed 2 deprecated GSwifiInterface_WebSocket

Dependencies:   GSwifiInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "GSwifiInterface.h"
00003 
00004 #define SEC  GSwifi::SEC_WPA_PSK
00005 #define SSID "SSID"
00006 #define PASS "PASSPHRASE"
00007 
00008 #define WS_SERVER "sockets.mbed.org"
00009 #define WS_URI    "/ws/username/rw"
00010 
00011 #ifndef CFG_ENABLE_WEBSOCKET
00012 #error Please enable "#define CFG_ENABLE_WEBSOCKET" in "GSwifi_conf.h"
00013 #endif
00014 
00015 Serial pc(USBTX, USBRX);
00016 
00017 int main() {
00018     GSwifiInterface gs(p13, p14, p12, P0_22, p20, NC);
00019 //    GSwifiInterface gs(p13, p14, NC, NC, p20, NC);
00020     pc.baud(115200);
00021     printf("WebSocket Client...\r\n");
00022     gs.init(); //Use DHCP
00023     if (gs.connect(SEC, SSID, PASS)) exit(-1); // join the network
00024     printf("IP Address is %s\r\n", gs.getIPAddress());
00025 
00026     int cid = gs.wsOpen (WS_SERVER, 80, WS_URI);
00027     if (cid < 0) {
00028         printf("error: wsOpen\r\n");
00029         return -1;
00030     }
00031 
00032     printf("WebSocket ready\r\n");
00033     for (;;) {
00034         gs.poll();
00035 
00036         if (pc.readable()) {
00037             char c;
00038             char buf[2];
00039             c = pc.getc();
00040             if (c == 0x1b) break; // ESC
00041             pc.printf("send: %c\r\n", c);
00042             buf[0] = c;
00043             gs.wsSend(cid, buf, 1, "MASK");
00044         }
00045 
00046         if (gs.readable(cid)) {
00047            int i, n;
00048            char buf[40];
00049            n = gs.recv(cid, buf, sizeof(buf));
00050            printf("recv: ");
00051            for (i = 0; i < n; i ++) {
00052                printf(" %02x", buf[i]);
00053            }
00054            printf("\r\n");
00055         }
00056 
00057         if (!gs.isConnected(cid)) {
00058             break;
00059         }
00060     }
00061 
00062     gs.dissociate();
00063     return 0;
00064 }