example websocket server

Dependencies:   EthernetInterface WebSocketClient WiflyInterface mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
wangstabill
Date:
Thu Sep 06 19:00:02 2012 +0000
Commit message:
intial commit

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.lib Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 90270ce862ec EthernetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Sep 06 19:00:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#a0ee3ae75cfa
diff -r 000000000000 -r 90270ce862ec WebSocketClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Thu Sep 06 19:00:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#86e89a0369b9
diff -r 000000000000 -r 90270ce862ec WiflyInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiflyInterface.lib	Thu Sep 06 19:00:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/WiflyInterface/#fb4494783863
diff -r 000000000000 -r 90270ce862ec main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 06 19:00:02 2012 +0000
@@ -0,0 +1,89 @@
+#include "mbed.h"
+//#include "EthernetInterface.h"
+#include "WiflyInterface.h"
+#include "Websocket.h"
+
+#define WS_SERVER_IP_ADDR "ws://192.168.42.61:8080/"
+#define SSID "mbed"
+#define PASSPHRASE "6033937142"
+
+static DigitalOut led1(LED1);
+static DigitalOut led2(LED2);
+static DigitalOut led3(LED3);
+static DigitalOut led4(LED4);
+
+static int count = 0;
+static char buffer[2];
+
+/* wifly object where:
+*     - p13 and p14 are for the serial communication
+*     - p22 is for the reset pin
+*     - p21 is for the connection status
+*     - SSID is the ssid of the network
+*     - PASSPHRASE is the password
+*     - WPA is the security
+*/
+WiflyInterface wifly(p13, p14, p22, p21, SSID, PASSPHRASE, WPA);
+
+void static clear_leds () {
+  led1 = led2 = led3 = led4 = 0;
+}
+
+void static counter () {
+  if (count == 99) {
+    count = 0;
+  }
+  count++;
+  sprintf(buffer, "%d", count);
+}
+
+/*static int connect_eth () {
+  EthernetInterface eth;
+  // Use DHCP
+  eth.init();
+  eth.connect();
+  // screen /dev/tty.usbmodem*
+  //printf("IP Address is %s\n\r", eth.getIPAddress());
+  return 0;
+}*/
+
+static int connect_wifi () {
+  wifly.init(); // use DHCP
+  while (!wifly.connect()); // join the network
+  return 0;
+}
+
+static Websocket connect_ws (char* ip_addr) {
+  // Viewable at: http://sockets.mbed.org/demo/viewer
+  //Websocket ws("ws://sockets.mbed.org:443/ws/demo/wo");
+
+  Websocket ws(ip_addr);
+  //printf("WS Constructed\n\r");
+  while(!ws.connect());
+  //printf("WS Connected\n\r");
+  return ws;
+}
+
+static Websocket connect (char* ip_addr) {
+  clear_leds();
+  //led1 = !connect_eth();
+  led1 = !connect_wifi();
+  Websocket ws = connect_ws(ip_addr);
+  led2 = 1;
+  return ws;
+}
+
+int main () {
+  Websocket ws = connect(WS_SERVER_IP_ADDR);
+  
+  while (1) {
+    counter();
+    if (ws.is_connected()) {
+      //printf("Sending...%s\n\r", buffer);
+      ws.send(buffer);
+    } else {
+      clear_leds();
+    }
+    //wait(0.1);
+  }
+}
\ No newline at end of file
diff -r 000000000000 -r 90270ce862ec mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Sep 06 19:00:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
diff -r 000000000000 -r 90270ce862ec mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 06 19:00:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/10b9abbe79a6
\ No newline at end of file