Chat by using websocket on WIZwiki-W7500 platform. Recently WIZwiki-W7500 platform was announced by WIZnet. So I implemented a firmware of WIZwiki-W7500 by changing UART definition because I can handle a source code easily.
Dependencies: WIZnetInterface WebSocketClient mbed
Chat by using websocket on WIZwiki-W7500 platform
http://www.instructables.com/id/Chat-by-using-websocket-on-WIZwiki-W7500-platform/
Websocket Server example
https://github.com/bingdo/ChatDemoWithWebSocket
main.cpp@0:050ec445ef5a, 2015-08-06 (annotated)
- Committer:
- bingdo
- Date:
- Thu Aug 06 05:55:03 2015 +0000
- Revision:
- 0:050ec445ef5a
Chat by using websocket on WIZwiki-W7500 platform.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bingdo | 0:050ec445ef5a | 1 | #include "mbed.h" |
bingdo | 0:050ec445ef5a | 2 | #include "EthernetInterface.h" |
bingdo | 0:050ec445ef5a | 3 | #include "Websocket.h" |
bingdo | 0:050ec445ef5a | 4 | |
bingdo | 0:050ec445ef5a | 5 | Serial pc(USBTX, USBRX); // tx, rx |
bingdo | 0:050ec445ef5a | 6 | |
bingdo | 0:050ec445ef5a | 7 | int main() |
bingdo | 0:050ec445ef5a | 8 | { |
bingdo | 0:050ec445ef5a | 9 | char send[256]; |
bingdo | 0:050ec445ef5a | 10 | char recv[256]; |
bingdo | 0:050ec445ef5a | 11 | int i = 0; |
bingdo | 0:050ec445ef5a | 12 | |
bingdo | 0:050ec445ef5a | 13 | printf("Wait a second...\r\n"); |
bingdo | 0:050ec445ef5a | 14 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xAB}; |
bingdo | 0:050ec445ef5a | 15 | EthernetInterface eth; |
bingdo | 0:050ec445ef5a | 16 | eth.init(mac_addr); //Use DHCP |
bingdo | 0:050ec445ef5a | 17 | eth.connect(); |
bingdo | 0:050ec445ef5a | 18 | printf("IP Address is %s\n\r", eth.getIPAddress()); |
bingdo | 0:050ec445ef5a | 19 | |
bingdo | 0:050ec445ef5a | 20 | Websocket ws("ws://192.168.0.2:8888/ws"); |
bingdo | 0:050ec445ef5a | 21 | ws.connect(); |
bingdo | 0:050ec445ef5a | 22 | |
bingdo | 0:050ec445ef5a | 23 | memset(send, 0, 256); |
bingdo | 0:050ec445ef5a | 24 | memset(recv, 0, 256); |
bingdo | 0:050ec445ef5a | 25 | |
bingdo | 0:050ec445ef5a | 26 | while (1) |
bingdo | 0:050ec445ef5a | 27 | { |
bingdo | 0:050ec445ef5a | 28 | if (ws.read(recv)) |
bingdo | 0:050ec445ef5a | 29 | { |
bingdo | 0:050ec445ef5a | 30 | pc.printf("%s\r\n", recv); |
bingdo | 0:050ec445ef5a | 31 | } |
bingdo | 0:050ec445ef5a | 32 | |
bingdo | 0:050ec445ef5a | 33 | if(pc.readable()) |
bingdo | 0:050ec445ef5a | 34 | { |
bingdo | 0:050ec445ef5a | 35 | send[i] = pc.getc(); |
bingdo | 0:050ec445ef5a | 36 | if (send[i] == 0x0d) |
bingdo | 0:050ec445ef5a | 37 | { |
bingdo | 0:050ec445ef5a | 38 | send[i] = 0x00; |
bingdo | 0:050ec445ef5a | 39 | i = 0; |
bingdo | 0:050ec445ef5a | 40 | ws.send(send); |
bingdo | 0:050ec445ef5a | 41 | memset(send, 0, 256); |
bingdo | 0:050ec445ef5a | 42 | } |
bingdo | 0:050ec445ef5a | 43 | else if (send[i] == 0x08) |
bingdo | 0:050ec445ef5a | 44 | { |
bingdo | 0:050ec445ef5a | 45 | i--; |
bingdo | 0:050ec445ef5a | 46 | } |
bingdo | 0:050ec445ef5a | 47 | else if ((send[i] < 0x20) || (send[i] >= 0x7F)) |
bingdo | 0:050ec445ef5a | 48 | { |
bingdo | 0:050ec445ef5a | 49 | } |
bingdo | 0:050ec445ef5a | 50 | else |
bingdo | 0:050ec445ef5a | 51 | { |
bingdo | 0:050ec445ef5a | 52 | i++; |
bingdo | 0:050ec445ef5a | 53 | } |
bingdo | 0:050ec445ef5a | 54 | } |
bingdo | 0:050ec445ef5a | 55 | } |
bingdo | 0:050ec445ef5a | 56 | } |