Dreamforce 2013 MiniHack Thermostat Challenge - remotes

Dependencies:   C12832_lcd EthernetInterface-ansond-patched HTTPClient-thermostat-remotes LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Fork of df-2013-minihack-thermostat-complete by MBED_DEMOS

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Sun Nov 10 02:18:02 2013 +0000
Parent:
2:340477adb161
Commit message:
switched json parser

Changed in this revision

MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
SocketIO.lib Show annotated file Show diff for this revision Revisions of this file
Thermostat-Location.h Show annotated file Show diff for this revision Revisions of this file
Thermostat.cpp 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
picojson.lib Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Sun Nov 10 02:18:02 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
--- a/SocketIO.lib	Fri Nov 08 20:10:52 2013 +0000
+++ b/SocketIO.lib	Sun Nov 10 02:18:02 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/ansond/code/SocketIO/#b32005b69b5c
+http://mbed.org/users/ansond/code/SocketIO/#8fe60d9ca3bf
--- a/Thermostat-Location.h	Fri Nov 08 20:10:52 2013 +0000
+++ b/Thermostat-Location.h	Sun Nov 10 02:18:02 2013 +0000
@@ -26,8 +26,8 @@
 
 // update our location coordinates
 void Thermostat::updateCoordinates() {
-    this->m_latitude = DEFAULT_LATITUDE;
-    this->m_longitude = DEFAULT_LONGITUDE;
+    this->m_latitude = latitudes[location_index];
+    this->m_longitude = longitudes[location_index];
     
     this->display("Location: Latitude:%2.4f Longitude:%2.4f",this->m_latitude,this->m_longitude);
     this->display_lcd("Location Coordinates\r\nLatitude: %2.4f\r\nLongitude: %2.4f",this->m_latitude,this->m_longitude);
--- a/Thermostat.cpp	Fri Nov 08 20:10:52 2013 +0000
+++ b/Thermostat.cpp	Sun Nov 10 02:18:02 2013 +0000
@@ -44,16 +44,25 @@
 
 // DreamForce 2013 Tunables START
 
-//
-// Our default Latitude and Longitude
-//
-#define DEFAULT_LATITUDE                     37.7842
-#define DEFAULT_LONGITUDE                   -122.4016  
+// set our location
+int location_index = 0;
 
-//
-// Name our endpoint something unique
-//
-#define DEFAULT_ENDPOINT_NAME               "DreamForce Thermostat"
+// Remoted Thermostats
+// Thermostat 1:   Austin TX       30.2500   –97.7500
+// Thermostat 2:   Phoenix AZ      33.4500   -112.0667
+// Thermostat 3:   Dallas TX       32.7758   -96.7967
+// Thermostat 4:   Miami FL        25.7877   -80.2241
+// Thermostat 5:   Chicago IL      41.8819   –87.6278
+// Thermostat 6:   Memphis TN      35.1174   -89.9711
+// Thermostat 7:   Seattle WA      47.6097   –122.3331
+// Thermostat 8:   New York NY     40.6700   –73.9400
+// Thermostat 9:   Raleigh NC      35.8189   –78.6447
+// Thermostat 10:  Moscone         37.7842   –122.4016
+float latitudes[10]  = {   30.2500,   33.4500,  32.7758,  25.7877,  41.8819,  35.1174,   47.6097,  40.6700,  35.8189,   37.7842};
+float longitudes[10] = {  -97.7500, -112.0667, -96.7967, -80.2241, -87.6278, -89.9711, -122.3331, -73.9400, -78.6447, -122.4016};
+char *locations[10]   = { "DF Thermostat-Austin TX", "DF Thermostat-Phoenix AZ", "DF Thermostat-Dallas TX", "DF Thermostat-Miami FL", 
+                          "DF Thermostat-Chicago IL", "DF Thermostat-Memphis TN", "DF Thermostat-Seattle WA", 
+                          "DF Thermostat-New York NY", "DF Thermostat-Raleigh NC", "DF Thermostat-Moscone Center"};
 
 // DreamForce 2013 Tunables END
 
@@ -61,7 +70,7 @@
 #define DEFAULT_MAIN_LOOP_WAIT              2.0     // 2 seconds
 
 // JSON parsing support
-#include "picojson.h"
+#include "MbedJSONValue.h"
 
 //
 // Accelerometer Support
@@ -85,7 +94,7 @@
 // Thermostat SocketIO Support
 //
 #include "ThermostatSocketIO.h"
-ThermostatSocketIO socketio(DEFAULT_ENDPOINT_NAME);
+ThermostatSocketIO socketio(locations[location_index]);
 
 // Include LED Utils
 #include "Thermostat-LEDUtils.h"
@@ -180,27 +189,24 @@
 // Control Message Format: 5:::{"name":"control-device","args":[{"hello":"world"}]}
 //
 void Thermostat::parseAndActOnControlMessage(char *json) {
-   picojson::value v;
-      
+   MbedJSONValue result;
+    
    if (json != NULL && strlen(json) > 0 && strstr(json,"{") != NULL) {
        // move past the socket.io header
        char *json_proper = strstr(json,"{");
        
        // parse the packet
-       string err = picojson::parse(v, json_proper, json_proper + strlen(json_proper));
-       
-       // the args value is an array of json values
-       picojson::array list = v.get("args").get<picojson::array>();
-       
+       parse(result,json_proper);
+              
        // loop through the array and parse/process each element
-       for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) {
+       for(int i=0;i<result["args"].size();++i) {        
             // Toggle LEDs
-            if ((*iter).get("led1") != NULL) led1.write(this->translateLEDStatus((*iter).get("led1").get<string>().c_str(),(int)led1));
-            if ((*iter).get("led2") != NULL) led2.write(this->translateLEDStatus((*iter).get("led2").get<string>().c_str(),(int)led2));
-            if ((*iter).get("led3") != NULL) led3.write(this->translateLEDStatus((*iter).get("led3").get<string>().c_str(),(int)led3));
-            if ((*iter).get("led4") != NULL) led4.write(this->translateLEDStatus((*iter).get("led4").get<string>().c_str(),(int)led4));
-            if ((*iter).get("reset") != NULL) this->resetDeviceStatus();
-            if ((*iter).get("blink") != NULL) this->blinkAllLEDs();
+            if (result["args"][i]["led1"].get<string>().size() > 0) led1.write(this->translateLEDStatus(result["args"][i]["led1"].get<string>().c_str(),(int)led1));
+            if (result["args"][i]["led2"].get<string>().size() > 0) led2.write(this->translateLEDStatus(result["args"][i]["led2"].get<string>().c_str(),(int)led2));
+            if (result["args"][i]["led3"].get<string>().size() > 0) led3.write(this->translateLEDStatus(result["args"][i]["led3"].get<string>().c_str(),(int)led3));
+            if (result["args"][i]["led4"].get<string>().size() > 0) led4.write(this->translateLEDStatus(result["args"][i]["led4"].get<string>().c_str(),(int)led4));
+            if (result["args"][i]["reset"].get<string>().size() > 0) this->resetDeviceStatus();
+            if (result["args"][i]["blink"].get<string>().size() > 0) this->blinkAllLEDs();
             
             //
             // 2013 DreamForce MiniHack - add code to look for a "text" message - send its contents to the LCD panel
@@ -208,7 +214,7 @@
             //
             // Answer:
             //
-            if ((*iter).get("text") != NULL) this->displayTextMessage((*iter).get("text").get<string>().c_str()); 
+            if (result["args"][i]["text"].get<string>().size() > 0) this->displayTextMessage(result["args"][i]["text"].get<string>().c_str()); 
         }
    }
 }
@@ -412,6 +418,29 @@
             }
         }
         
+        // if not connected... reconnect
+        if (!socketio.is_connected()){
+            // announce
+            this->display("Re-connecting...");
+            this->display_lcd("Re-connecting...");
+            
+            // re-connect
+            if (this->connectWebSocketService()) {
+                // announce
+                this->display("Reconnect success");
+                this->display_lcd("Reconnect: SUCCESS");
+                this->turnRGBLEDGreen();
+                this->resetAllLEDs();
+            }
+            else {
+                // announce
+                this->display("Reconnect failure");
+                this->display_lcd("Reconnect: FAILED");
+                this->gracefullyDisconnect();
+                this->turnRGBLEDRed();
+            }
+        }
+        
         // if we are connected, send our status
         if (socketio.is_connected()) {
             // send status
--- a/main.cpp	Fri Nov 08 20:10:52 2013 +0000
+++ b/main.cpp	Sun Nov 10 02:18:02 2013 +0000
@@ -5,6 +5,17 @@
 Serial pc(USBTX, USBRX);
 Thermostat *thermostat = NULL;
 
+// useful for KEIL Debugging
+extern "C" void mbed_mac_address(char * mac) {
+  // define your own MAC Address
+  mac[0] = 0x00;  
+  mac[1] = 0x01;  
+  mac[2] = 0x02;  
+  mac[3] = 0x03;  
+  mac[4] = 0x04;  
+  mac[5] = 0x05;           
+};
+
 // check for exit
 void checkForExit() {
     if (pc.readable()) {
@@ -18,7 +29,7 @@
     }
 }
 
-int main() {
+int main(int argc, char *argv[]) {
     thermostat = new Thermostat();
     if (thermostat != NULL) thermostat->runDemo();
 }
--- a/picojson.lib	Fri Nov 08 20:10:52 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/users/mimil/code/picojson/#2bb500b021e2