able to subscribe for >10hrs and still running

Dependencies:   ADE7758_v1 Crypto DHT11 MQTT MbedJSONValue SDFileSystem SPI_TFT_ILI9341 SWSPI SetRTC TFT_fonts Touch W5500Interface mbed-rtos mbed-src tuanpm

Fork of PB_emma_controller_mbed_src by Emma

Revision:
21:33bd8b82560f
Parent:
20:ea14f175bbb4
Child:
22:e18d361bf688
--- a/emmaCode.cpp	Wed Jul 22 13:17:57 2015 +0000
+++ b/emmaCode.cpp	Thu Jul 23 04:22:55 2015 +0000
@@ -29,6 +29,9 @@
 TouchScreenADS7843 TP(PB_10, PB_0, PB_1, PB_2, &TFT);    //mosi, miso, sclk, irq, tft
 //InterruptIn tpIRQ(PB_2);
 
+//init dht sensor
+DHT11 d(PD_2);
+
 //emma settings
 string emmaUID;
 string hmac;
@@ -885,11 +888,13 @@
     char r[32];
     char s[512];
     int loop=0;
+    int trial=0;
     //string hmac;
     string str;
     time_t seconds;
     Timer t;
     Timer tNodes;
+    Timer tPanel;
     
     TFT.locate(0,0);
     TFT.printf(" please wait");
@@ -995,7 +1000,8 @@
                         DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
                         
                         //execute command
-                        int trial=0;
+                        //int trial=0;
+                        trial = 0;
                         string execStatus="failed";
                     
                         while(1) {
@@ -1175,6 +1181,7 @@
         
         t.start();
         tNodes.start();
+        tPanel.start();
         wait(1);
         while(1) {
             checkRxBuffer();
@@ -1254,9 +1261,53 @@
                 loop++;    
             }
             
+            //panel environment
             checkRxBuffer();
-            //checkVoltagePower();
+            if(tPanel.read() > 60.0f) {
+                int dTemp=0;
+                int dHum=0;
+                int dGas=0;
+                
+                DBG.printf("getPanelEnvironment\r\n");
+                
+                //get environment sensor
+                trial=0;
+                while(1) {
+                    if(trial>=2) {  //two times trial
+                        break;
+                    }
+                    if(d.readData() == DHT11::OK) {
+                        dTemp = d.readTemperature();
+                        dHum = d.readHumidity();
+                        break;    
+                    }
+                    trial++;
+                    wait(3);    
+                }
+                
+                //send environment sensor
+                seconds = time(NULL);
+                strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
+                emmaUID.c_str(),hmac.c_str(),q,dTemp,dHum,dGas);
+                DBG.printf("dataEnvironment:\r\n%s\r\n",s);
+                rest.post("/emma/api/controller/environment",s);
+                wait(2);
+                str = rxBuf;
+                if(str.rfind("/environment") != std::string::npos) {
+                    str.erase(str.begin(),str.begin()+str.rfind("/environment"));
+                    if(str.find("\"status\":\"success\"") != std::string::npos) {
+                        DBG.printf("send panel environment success\r\n");
+                    } else {
+                        DBG.printf("send panel environment failed\r\n");
+                    }
+                }
+                checkRxBuffer();
+                tPanel.reset();
+            }
+            
             //nodeTemp
+            checkRxBuffer();
             if(tNodes.read() > 120.0f) {
                 DBG.printf("getNodesTemperature\r\n");
                 
@@ -1291,25 +1342,23 @@
                         //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
                         rest.post("/emma/api/controller/nodetemp",s);
                         wait(2);
-                        temp = rxBuf;
-                        if(temp.rfind("/nodetemp") != std::string::npos) {
-                            temp.erase(temp.begin(),temp.begin()+temp.rfind("/nodetemp"));
-                            if(temp.find("\"status\":\"success\"") != std::string::npos) {
+                        str = rxBuf;
+                        if(str.rfind("/nodetemp") != std::string::npos) {
+                            str.erase(str.begin(),str.begin()+str.rfind("/nodetemp"));
+                            if(str.find("\"status\":\"success\"") != std::string::npos) {
                                 DBG.printf("send nodeTemp success\r\n");
                             } else {
                                 DBG.printf("send nodeTemp failed\r\n");
                             }
                         }
                         checkRxBuffer();
-                        //checkVoltagePower();
                     }    
                 }
                 tNodes.reset();
             }
-                
+            
+            //command
             checkRxBuffer();
-            //checkVoltagePower();
-            //command
             if(newCommand) {
                 DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
                 TFT.locate(0,160);
@@ -1355,7 +1404,7 @@
                         }
                     
                         //execution process
-                        int trial;
+                        //int trial;
                         string execResult = "failed";
                         if(idx != NODES_INVALID) {
                             DBG.printf("index found at %d\r\n",idx);
@@ -1418,7 +1467,7 @@
                     
                         wait(2);       
                         //send execution result
-                        DBG.printf("send execution result\r\n");
+                        //DBG.printf("send execution result\r\n");
                         sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\",\"name\":\"%s\"}",
                         emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execResult.c_str(),commandName.c_str());
                     
@@ -1657,13 +1706,13 @@
             
             
             //start check voltage and power
-            AVrms = ADE.VRMS(PHASE_A) * 0.000128f; //0.000158;   //constants are from calculateVRMS function
-            BVrms = ADE.VRMS(PHASE_B) * 0.000127f; //0.000157;
-            CVrms = ADE.VRMS(PHASE_C) * 0.000126f; //0.000156;
+            AVrms = ADE.VRMS(PHASE_A) * 0.000124f;      //0.000158;     //constants are from calculateVRMS function
+            BVrms = ADE.VRMS(PHASE_B) * 0.000123f;
+            CVrms = ADE.VRMS(PHASE_C) * 0.000122f;
     
-            AIrms = ADE.IRMS(PHASE_A) * 0.0000125f;  //constants are from calculateIRMS function
-            BIrms = ADE.IRMS(PHASE_B) * 0.0000123f;
-            CIrms = ADE.IRMS(PHASE_C) * 0.0000124f;
+            AIrms = ADE.IRMS(PHASE_A) * 0.00001006f;    //0.0000125f;   //constants are from calculateIRMS function
+            BIrms = ADE.IRMS(PHASE_B) * 0.00001005f;
+            CIrms = ADE.IRMS(PHASE_C) * 0.00001004f;
     
             AWatt = AVrms * AIrms;
             BWatt = BVrms * BIrms;
@@ -1681,7 +1730,7 @@
         DBG.printf("energyThread-finish\r\n");
     }
 }
-void checkVoltagePower() {
+void checkVoltagePower(void) {
     //check if voltage or power violates threshold
     char q[32];
     char s[256];
@@ -1706,9 +1755,9 @@
     CWatt = CVrms * CIrms;
     */
     
-    DBG.printf("Vrms of each phase:%.2f - %.2f - %.2f\r\n", AVrms, BVrms, CVrms);
-    DBG.printf("Watt of each phase:%.2f - %.2f - %.2f\r\n", AWatt, BWatt, CWatt);
-    wait(1);
+    //DBG.printf("Vrms of each phase:%.2f - %.2f - %.2f\r\n", AVrms, BVrms, CVrms);
+    //DBG.printf("Watt of each phase:%.2f - %.2f - %.2f\r\n", AWatt, BWatt, CWatt);
+    //wait(1);
     
     //get time
     seconds = time(NULL);