Work with nodejs server: https://github.com/bjo3rn/idd-examples/tree/ben-node-demo/web/nodeServerHTTPSocket

Dependencies:   HTTPClient WebSocketClient cc3000_hostdriver_mbedsocket mbed

Fork of CC3000_demo by Ben Zhang

Files at this revision

API Documentation at this revision

Comitter:
nebgnahz
Date:
Wed Nov 05 08:49:02 2014 +0000
Parent:
17:97c355870263
Commit message:
Simple WiFi demo.

Changed in this revision

WebSocketClient.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
diff -r 97c355870263 -r 922b1ad5c826 WebSocketClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Wed Nov 05 08:49:02 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
diff -r 97c355870263 -r 922b1ad5c826 main.cpp
--- a/main.cpp	Mon Oct 20 17:10:46 2014 +0000
+++ b/main.cpp	Wed Nov 05 08:49:02 2014 +0000
@@ -13,10 +13,15 @@
  * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
  */
 
-#include "mbed.h"
+#include <string>
+
 #include "cc3000.h"
+#include "mbed.h"
 #include "HTTPClient.h"
 
+const string POLLING_URL = "http://universe.eecs.berkeley.edu:8080/value";
+const string UPDATE_URL = "http://universe.eecs.berkeley.edu:8080/update?value=";
+
 // KL25Z wifi connection
 // we need to define connection pins for:
 // - IRQ      => (pin D3)
@@ -38,48 +43,72 @@
 
 // setup the serial connection, and LEDs
 Serial pc(USBTX, USBRX);
-DigitalOut led_red(LED_RED);
 DigitalOut led_green(LED_GREEN);
+PwmOut led_red(LED_RED);
+
+Ticker reportSensorTicker;
+bool shouldReadSensor;
+int sensorData;
+
+void readSensor() {
+    // avoid doing much work here (in the interrupt).
+    // put the actual sensor reading in the main function
+    shouldReadSensor = true;
+}
 
 int main() {
-  // by default, it's red
-  led_red = 0;
-  led_green = 1;
-
-  // print message to indicate the program has started
-  pc.printf("CC3000 Sample Program\r\n");
-  wifi.init();
+    pc.printf("CC3000 Nodejs Demo Starts\r\n");
+    // by default, we make the LED red
+    led_red = 0;
+    led_green = 1;
 
-  while(1) {
-    // continuosly check connection status
-    if(wifi.is_connected() == false) {
-      // try to connect
-      if (wifi.connect() == -1) {
-        pc.printf("Failed to connect."
-                  "Please verify connection details and try again.\r\n");
-      } else {
-        pc.printf("IP address: %s \r\n", wifi.getIPAddress());
+    // reset the readsensor bool to false so we don't read the data.
+    shouldReadSensor = false;
+    sensorData = 0;
+    // Every 5 second, we sample the sensor and report the data.
+    reportSensorTicker.attach(&readSensor, 5.0); 
+    
+    wifi.init();
+
+    while(1) {
+        // continuosly check connection status
+        if(wifi.is_connected() == false) {
+            // try to connect
+            if (wifi.connect() == -1) {
+                pc.printf("Failed to connect."
+                          "Please verify connection details and try again.\r\n");
+                continue;
+            } else {
 
-        //once connected, turn green LED on and red LED off
-        led_red = 1;
-        led_green = 0;
-      }
-    } else {
-      // get input url and then return the value
-      pc.printf("> ");
-      pc.scanf("%s", url);
+                pc.printf("IP address: %s \r\n", wifi.getIPAddress());
+                // once connected, turn green LED on and red LED off
+                led_red = 1;
+                led_green = 0;
+            }
+        }
 
-      int ret = http.get(url, str, 128);
-      if (!ret) {
-        pc.printf("Requested %s\r\n", url);
-        pc.printf("Page fetched successfully - read %d characters\r\n",
-                  strlen(str));
-        pc.printf("Result: %s\r\n", str);
-      } else {
-        pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
-                  ret,
-                  http.getHTTPResponseCode());
-      }
+        // update the value when shouldReadSensor is true          
+        if (shouldReadSensor == true) {
+            shouldReadSensor = false;
+            sensorData += 10;
+            char url[128];
+            snprintf(url, sizeof(url), "%s%d", UPDATE_URL.c_str(), sensorData);
+            int ret = http.get(url, str, 128);
+            if (!ret) {
+                pc.printf("sending sensorData %d, response: %s\r\n", sensorData, str);
+            } else {
+                pc.printf("[PUSH] Error - ret = %d - HTTP %d\r\n", ret, http.getHTTPResponseCode());
+            }
+        }
+        
+        // everytime we do HTTP polling
+        int ret = http.get(POLLING_URL.c_str(), str, 128);
+        if (!ret) {
+            int value = atoi(str);
+            led_red = 1.0 * value / 255;
+        } else {
+            pc.printf("[POLL] Error - ret = %d - HTTP %d\r\n", ret, http.getHTTPResponseCode());
+        }
+        wait(0.1);
     }
-  }
 }