WebSocket Hello World using the wifly interface
Dependencies: WebSocketClient WiflyInterface mbed
Deprecated
This is an mbed 2 example. For an mbed OS 5 WiFi example, see:
[Repository '/teams/mbed-os-examples/code/mbed-os-example-wifi/docs/tip/' not found]
main.cpp@4:ef7d3e5f4b10, 2013-10-25 (annotated)
- Committer:
- sam_grove
- Date:
- Fri Oct 25 05:26:33 2013 +0000
- Revision:
- 4:ef7d3e5f4b10
- Parent:
- 3:f38ab577e979
Without my lab wifi credentials
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:31e50fea8be8 | 1 | #include "mbed.h" |
samux | 1:31e50fea8be8 | 2 | #include "WiflyInterface.h" |
samux | 1:31e50fea8be8 | 3 | #include "Websocket.h" |
samux | 1:31e50fea8be8 | 4 | |
sam_grove | 3:f38ab577e979 | 5 | Ticker flash; |
sam_grove | 3:f38ab577e979 | 6 | DigitalOut led(LED1); |
sam_grove | 3:f38ab577e979 | 7 | void flashLED(void){led = !led;} |
samux | 1:31e50fea8be8 | 8 | |
samux | 1:31e50fea8be8 | 9 | /* wifly interface: |
samux | 1:31e50fea8be8 | 10 | * - p9 and p10 are for the serial communication |
samux | 1:31e50fea8be8 | 11 | * - p19 is for the reset pin |
samux | 1:31e50fea8be8 | 12 | * - p26 is for the connection status |
samux | 1:31e50fea8be8 | 13 | * - "mbed" is the ssid of the network |
samux | 1:31e50fea8be8 | 14 | * - "password" is the password |
samux | 1:31e50fea8be8 | 15 | * - WPA is the security |
samux | 1:31e50fea8be8 | 16 | */ |
sam_grove | 4:ef7d3e5f4b10 | 17 | WiflyInterface wifly(p9, p10, p30, p29, "mbed", "password", WPA); |
samux | 1:31e50fea8be8 | 18 | |
sam_grove | 3:f38ab577e979 | 19 | int main() |
sam_grove | 3:f38ab577e979 | 20 | { |
sam_grove | 3:f38ab577e979 | 21 | flash.attach(&flashLED, 1.0f); |
sam_grove | 3:f38ab577e979 | 22 | |
samux | 1:31e50fea8be8 | 23 | wifly.init(); //Use DHCP |
sam_grove | 3:f38ab577e979 | 24 | while(!wifly.connect()); |
sam_grove | 3:f38ab577e979 | 25 | printf("IP Address: %s\n", wifly.getIPAddress()); |
samux | 1:31e50fea8be8 | 26 | |
sam_grove | 3:f38ab577e979 | 27 | // view @ http://sockets.mbed.org/demo/viewer |
sam_grove | 3:f38ab577e979 | 28 | Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); |
samux | 1:31e50fea8be8 | 29 | while (!ws.connect()); |
sam_grove | 3:f38ab577e979 | 30 | char str[100]; |
sam_grove | 3:f38ab577e979 | 31 | |
sam_grove | 3:f38ab577e979 | 32 | for(int i=0; i<0x7fffffff; ++i) { |
sam_grove | 3:f38ab577e979 | 33 | // string with a message |
sam_grove | 3:f38ab577e979 | 34 | sprintf(str, "%d WebSocket Hello World over Wifi", i); |
sam_grove | 3:f38ab577e979 | 35 | ws.send(str); |
sam_grove | 3:f38ab577e979 | 36 | |
sam_grove | 3:f38ab577e979 | 37 | // clear the buffer and wait a sec... |
sam_grove | 3:f38ab577e979 | 38 | memset(str, 0, 100); |
sam_grove | 3:f38ab577e979 | 39 | wait(0.5f); |
sam_grove | 3:f38ab577e979 | 40 | |
sam_grove | 3:f38ab577e979 | 41 | // websocket server should echo whatever we sent it |
sam_grove | 3:f38ab577e979 | 42 | if (ws.read(str)) { |
sam_grove | 3:f38ab577e979 | 43 | printf("rcv'd: %s\n", str); |
sam_grove | 3:f38ab577e979 | 44 | } |
samux | 1:31e50fea8be8 | 45 | } |
sam_grove | 3:f38ab577e979 | 46 | ws.close(); |
sam_grove | 3:f38ab577e979 | 47 | wifly.disconnect(); |
sam_grove | 3:f38ab577e979 | 48 | |
sam_grove | 3:f38ab577e979 | 49 | while(true); |
samux | 0:64c683256184 | 50 | } |