Jim Jun / Mbed 2 deprecated WebSocketClient_Chat_WIZwiki-W7500

Dependencies:   WIZnetInterface WebSocketClient mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "Websocket.h"
00004 
00005 Serial pc(USBTX, USBRX); // tx, rx
00006 
00007 int main()
00008 {
00009     char send[256];
00010     char recv[256];
00011     int i = 0;
00012  
00013     printf("Wait a second...\r\n");
00014     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xAB}; 
00015     EthernetInterface eth;
00016     eth.init(mac_addr); //Use DHCP
00017     eth.connect();
00018     printf("IP Address is %s\n\r", eth.getIPAddress());
00019  
00020     Websocket ws("ws://192.168.0.2:8888/ws");
00021     ws.connect();
00022 
00023     memset(send, 0, 256);
00024     memset(recv, 0, 256);
00025  
00026     while (1)
00027     {
00028         if (ws.read(recv))
00029         {
00030             pc.printf("%s\r\n", recv);
00031         }
00032         
00033         if(pc.readable())
00034         {
00035             send[i] = pc.getc();
00036             if (send[i] == 0x0d)
00037             {
00038                 send[i] = 0x00;
00039                 i = 0;
00040                 ws.send(send);
00041                 memset(send, 0, 256);
00042             }
00043             else if (send[i] == 0x08)
00044             {
00045                 i--;
00046             }
00047             else if ((send[i] < 0x20) || (send[i] >= 0x7F))
00048             {
00049             }
00050             else
00051             {
00052                 i++;
00053             }
00054         }
00055     }
00056 }