Websocket Hello World over a wifi network

Dependencies:   EthernetNetIf mbed DNSResolver

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Wed Feb 01 17:24:14 2012 +0000
Parent:
7:1c874f214a98
Child:
9:7b9912c6d812
Commit message:

Changed in this revision

ADXL345.lib Show diff for this revision Revisions of this file
Websocket.lib Show annotated file Show diff for this revision Revisions of this file
Wifly.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.bld Show annotated file Show diff for this revision Revisions of this file
--- a/ADXL345.lib	Tue Oct 25 06:54:31 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/aberk/code/ADXL345/#bd8f0f20f433
--- a/Websocket.lib	Tue Oct 25 06:54:31 2011 +0000
+++ b/Websocket.lib	Wed Feb 01 17:24:14 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/samux/code/Websocket/#ca8c5ad59850
+http://mbed.org/users/samux/code/Websocket/#f4aac491ea26
--- a/Wifly.lib	Tue Oct 25 06:54:31 2011 +0000
+++ b/Wifly.lib	Wed Feb 01 17:24:14 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/samux/code/Wifly/#92ee976db7b9
+http://mbed.org/users/samux/code/Wifly/#56f3c4da1ea8
--- a/main.cpp	Tue Oct 25 06:54:31 2011 +0000
+++ b/main.cpp	Wed Feb 01 17:24:14 2012 +0000
@@ -1,64 +1,43 @@
-#include "mbed.h"
-#include "Wifly.h"
-#include "Websocket.h"
-#include "ADXL345.h"
-
-Serial pc(USBTX, USBRX);
-
-ADXL345 accelerometer(p5, p6, p7, p8);
-
-//Here, we create an instance, with pins 9 and 10 connecting to the 
-//WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the 
-//"mbed" network, password "password", and we are using WPA.
-Wifly wifly(p9, p10, p21, "mbed", "password", true);
-
-//Here, we create a Websocket instance in 'wo' (write) mode
-//on the 'samux' channel
-Websocket ws("ws://sockets.mbed.org/ws/samux/wo", &wifly);
-
-int main() {
-    char json_str[100];
-
-    int readings[3] = {0, 0, 0};
-
-    pc.printf("Starting ADXL345 test...\r\n");
-    pc.printf("Device ID is: 0x%02x\r\n", accelerometer.getDevId());
-
-    //Go into standby mode to configure the device.
-    accelerometer.setPowerControl(0x00);
-
-    //Full resolution, +/-16g, 4mg/LSB.
-    accelerometer.setDataFormatControl(0x0B);
-
-    //3.2kHz data rate.
-    accelerometer.setDataRate(ADXL345_3200HZ);
-
-    //Measurement mode.
-    accelerometer.setPowerControl(0x08);
-
-
-    while (1) {
-        while (1) {
-
-            while (!wifly.join())  //we connect to the network
-                wifly.reset();
-
-            if (!ws.connect())    //we connect to the server
-                wifly.reset();
-            else
-                break;
-        }
-
-        while (1) {
-            wait(0.1);
-
-            //we read accelerometers values
-            accelerometer.getOutput(readings);
-
-            //Here, we format the string we will be sending to the server
-            //the format we are sending in is JSON
-            sprintf(json_str, "{\"id\":\"wifly_acc\",\"ax\":\"%d\",\"ay\":\"%d\",\"az\":\"%d\"}", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
-            ws.send(json_str);  //And we send the string
-        }
-    }
+#include "mbed.h"
+#include "Wifly.h"
+#include "Websocket.h"
+
+DigitalOut l1(LED1);
+
+//Here, we create an instance, with pins 9 and 10 connecting to the
+//WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the
+//"mbed" network, password "password", and we are using WPA.
+Wifly wifly(p9, p10, p21, "mbed", "mbedapm2011", true);
+
+//Here, we create a Websocket instance in 'rw' (read-write) mode
+//on the 'samux' channel
+Websocket ws("ws://sockets.mbed.org/ws/samux/rw", &wifly);
+
+
+int main() {
+    char recv[40];
+
+    while (1) {
+
+        //we connect the network
+        while (!wifly.join()) {
+            wifly.reset();
+        }
+
+        //we connect to the websocket server
+        while (!ws.connect());
+
+        while (1) {
+            wait(0.5);
+
+            //Send Hello world
+            ws.send("Hello World! over Wifi");
+
+            // if a message is available, print it
+            if (ws.read(recv)) {
+                // show that we receive messages
+                l1 = !l1;
+            }
+        }
+    }
 }
\ No newline at end of file
--- a/mbed.bld	Tue Oct 25 06:54:31 2011 +0000
+++ b/mbed.bld	Wed Feb 01 17:24:14 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
+http://mbed.org/users/mbed_official/code/mbed/builds/078e4b97a13e