emma controller code in production board v1

Dependencies:   ADE7758_v1 Crypto DHT11 MQTT MbedJSONValueEmma SDFileSystem TFT_ILI9341 SWSPI SetRTC TFT_fonts Touch W5500Interface mbed-rtos mbed-src SoftSerial

Fork of emma_controller_energy by Emma

Revision:
52:cbe6d1d1efdc
Parent:
51:ad6410dcaf52
Child:
53:7c23208feca1
--- a/emmaCode.cpp	Fri Sep 04 07:29:52 2015 +0000
+++ b/emmaCode.cpp	Thu Sep 10 05:41:32 2015 +0000
@@ -55,6 +55,11 @@
 int proxyPORT;
 string proxyAUTH;
 int powerPHASE;
+//threshold default values
+int vrmsTHL = 176;
+int vrmsTHH = 264;
+int wattTHL = 1760;
+int wattTHH = 2640;
 
 //nodes settings
 class NODES {
@@ -1270,7 +1275,7 @@
             checkVoltagePower();
             
             //set allowAlertXxxx to enable controller send alert
-            if(tAlert.read() > 300.0f) {    //300.0f is 5 minutes
+            if(tAlert.read() > 900.0f) {    //900.0f is 15 minutes
                 allowAlertAVrms = true;
                 allowAlertAWatt = true;
                 allowAlertBVrms = true;
@@ -1777,9 +1782,44 @@
         
         _ESP.attach(&rxInterrupt,Serial::RxIrq);
         
+        //get alert threshold from server
+        sprintf(p,"/%s/api/controller/threshold",appNAME.c_str());
+        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+        rest.post(p,s);
+        wait(2);
+        str = rxBuf;
+        if(str.rfind("[{\"vrmsTHL\"") != std::string::npos) {
+            DBG.printf("get threshold from server\r\n");
+            str.erase(str.begin(),str.begin()+str.rfind("[{\"vrmsTHL\"")+1);
+            str.erase(str.begin()+str.rfind("}]")+1,str.end());
+            //DBG.printf("strCrop:%s\r\n",str.c_str());
+            
+            MbedJSONValue jsonValue;
+            parse(jsonValue,str.c_str());
+            char *parameter[4] = {"vrmsTHL","vrmsTHH","wattTHL","wattTHH"};
+            
+            //check whether threshold valid
+            bool validTh = true;
+            for(int i=0; i<4; i++) {
+                validTh = validTh && jsonValue.hasMember(parameter[i]);
+            }    
+            DBG.printf("threshold validity:%d\r\n",validTh);
+            
+            if(validTh) {
+                vrmsTHL = jsonValue[parameter[0]].get<int>();
+                vrmsTHH = jsonValue[parameter[1]].get<int>();
+                wattTHL = jsonValue[parameter[2]].get<int>();
+                wattTHH = jsonValue[parameter[3]].get<int>();
+                DBG.printf("vrmsTHL:%d - vrmsTHH:%d - wattTHL:%d - wattTHH:%d\r\n",vrmsTHL,vrmsTHH,wattTHL,wattTHH);
+            }
+            
+        } else {
+            DBG.printf("no threshold from server\r\n");    
+        }
+        
         //get list of nodes from server (post)
         sprintf(p,"/%s/api/controller/wifinodes",appNAME.c_str());
-        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmacTime.c_str());
+        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         rest.post(p,s);
         wait(2);
         str = rxBuf;
@@ -1799,7 +1839,7 @@
             }
             //end special handler
             
-            DBG.printf("strCrop:%s\r\n",str.c_str());
+            //DBG.printf("strCrop:%s\r\n",str.c_str());
             
             MbedJSONValue jsonValue;
             parse(jsonValue,str.c_str());
@@ -1919,7 +1959,7 @@
             }
             
             //set allowAlertXxxx to enable controller send alert
-            if(tAlert.read() > 300.0f) {    //300.0f is 5 minutes
+            if(tAlert.read() > 900.0f) {    //300.0f is 15 minutes
                 allowAlertAVrms = true;
                 allowAlertAWatt = true;
                 allowAlertBVrms = true;
@@ -2409,7 +2449,7 @@
             checkVoltagePower();  //need revision to support gprs
             
             //set allowAlertXxxx to enable controller send alert
-            if(tAlert.read() > 300.0f) {    //300.0f is 5 minutes
+            if(tAlert.read() > 900.0f) {    //300.0f is 15 minutes
                 allowAlertAVrms = true;
                 allowAlertAWatt = true;
                 allowAlertBVrms = true;