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]

Revision:
3:f38ab577e979
Parent:
1:31e50fea8be8
Child:
4:ef7d3e5f4b10
--- a/main.cpp	Fri Aug 24 14:06:31 2012 +0000
+++ b/main.cpp	Fri Oct 25 05:24:39 2013 +0000
@@ -2,6 +2,9 @@
 #include "WiflyInterface.h"
 #include "Websocket.h"
 
+Ticker flash;
+DigitalOut led(LED1);
+void flashLED(void){led = !led;}
 
 /* wifly interface:
 *     - p9 and p10 are for the serial communication
@@ -11,18 +14,38 @@
 *     - "password" is the password
 *     - WPA is the security
 */
-WiflyInterface wifly(p9, p10, p19, p26, "mbed", "password", WPA);
+//WiflyInterface wifly(p9, p10, p30, p29, "mbed", "password", WPA);
+WiflyInterface wifly(p9, p10, p30, p29, "McGroves", "purnell44", WPA);
 
-int main() {
+int main() 
+{
+    flash.attach(&flashLED, 1.0f);
+    
     wifly.init(); //Use DHCP
-    while (!wifly.connect());
-    printf("IP Address is %s\n\r", wifly.getIPAddress());
+    while(!wifly.connect());
+    printf("IP Address: %s\n", wifly.getIPAddress());
 
-    Websocket ws("ws://sockets.mbed.org:443/ws/demo/wo");
+    // view @ http://sockets.mbed.org/demo/viewer
+    Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
     while (!ws.connect());
-
-    while (1) {
-            ws.send("WebSocket Hello World over Wifly");
-            wait(1.0);
+    char str[100];
+    
+    for(int i=0; i<0x7fffffff; ++i) {
+        // string with a message
+        sprintf(str, "%d WebSocket Hello World over Wifi", i);
+        ws.send(str);
+    
+        // clear the buffer and wait a sec...
+        memset(str, 0, 100);
+        wait(0.5f);
+    
+        // websocket server should echo whatever we sent it
+        if (ws.read(str)) {
+            printf("rcv'd: %s\n", str);
+        }
     }
+    ws.close();
+    wifly.disconnect();
+    
+    while(true);
 }
\ No newline at end of file