:)

Dependencies:   MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2

Files at this revision

API Documentation at this revision

Comitter:
jn80842
Date:
Thu Dec 11 03:08:30 2014 +0000
Parent:
19:fea62e9c1ac4
Child:
21:4ec999be140a
Commit message:
fix flaky websocket behavior

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Dec 10 06:47:50 2014 +0000
+++ b/main.cpp	Thu Dec 11 03:08:30 2014 +0000
@@ -35,7 +35,8 @@
 
 //Scanner stuff
 
-DebounceIn scanbutton(PTC13);
+//DebounceIn scanbutton(PTC13);
+DebounceIn scanbutton(D7);
 
 DigitalOut A_in(PTB10);
 DigitalOut B_in(PTB11);
@@ -88,7 +89,7 @@
 
 
 mbed_cc3000::cc3000 wifi(D8, D9, D10, SPI(D11, D12, D13), // spi goes mosi, miso, clk
-                        "SSID", "PASSWORD", WPA2, false);
+                        "EECS-PSK", "Thequickbrown", WPA2, false);
 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
 
 
@@ -186,7 +187,7 @@
             in_val = adc.read();
    
             
-            if ((in_val < voltages[sn]-0.006) || (in_val > voltages[sn]+0.006)){
+            if ((in_val < voltages[sn]-0.0008) || (in_val > voltages[sn]+0.0008)){
                 voltages[sn] = 100.0;
             
             }
@@ -270,32 +271,41 @@
     int written = 0;
 
     wifi.init();
-    Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
+    char * writable;
+   // Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
 
     //Osci
     int loopcount = 0, pressed = 0;
     float begintime = 0, endtime = 0, elapsed = 0;
+    
+    int datatosend = 0;
+
 
 
     while(1) {
-        
-        if (wifi.is_connected() == false) {
-            pc.printf("trying to connect to wifi\r\n");
-            if (wifi.connect() == -1) {
-                pc.printf("Failed to connect\r\n");
+             if (wifi.is_connected() == false) {
+                pc.printf("trying to connect to wifi\r\n");
+                if (wifi.connect() == -1) {
+                    pc.printf("Failed to connect\r\n");
+                } else {
+                    pc.printf("IP address: %s \r\n", wifi.getIPAddress());
+                }
             } else {
-                pc.printf("IP address: %s \r\n", wifi.getIPAddress());
+                if (datatosend) {
+                    pc.printf("json in queue to be sent out\r\n");
+                    Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
+                    ws.connect();
+                    if (ws.send(writable) != -1) {
+                        datatosend = 0;
+                        pc.printf("we have sent a message!\r\n");
+                        delete writable;
+                    }
+                }
+
             }
-        } else if (ws.is_connected() == false) {  
-            pc.printf("connecting to ws\r\n");                  
-            ws.connect();
-        }
 
-        pc.printf("Okay took care of wifi stuff\r\n");
 
         
-        
-        
         //Display
         if (moved ==1) {
             ledbar_left.clear();
@@ -348,7 +358,7 @@
         }
 
         selected = (colselect * 24) + (rowselect+1);
-        pc.printf("selected is %d\r\n",selected);
+     //   pc.printf("selected is %d\r\n",selected);
         
         //Implementing scanning
 
@@ -406,18 +416,15 @@
                 
                 
                 // send data to websocket
-                if (wifi.is_connected()) {
-                    std::string json = build_json(vddval,selected,clientdata);
-                    char * writable = new char[json.size() + 1];
-                    std::copy(json.begin(), json.end(), writable);
-                    writable[json.size()] = '\0';
-                    pc.printf(writable);
-                    pc.printf("\r\n");
-                    pc.printf("ws is connected ? %d\r\n", ws.is_connected());                    ws.send(writable);
-                    delete[] writable;
-                }
-                
-                
+                datatosend = 1;
+                std::string json = build_json(vddval,selected,clientdata);
+               // delete writable;
+                writable = new char[json.size() + 1];
+                std::copy(json.begin(), json.end(), writable);
+                writable[json.size()] = '\0';
+                pc.printf(writable);
+                pc.printf("\r\n");
+
                 written = 1;
             } else if (oldselected != selected) {
                 lcd_write_selected_info(selected);
@@ -430,7 +437,6 @@
 
 
 
-    pc.printf("end of while loop!\r\n");