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:
19:7e3e9332f719
Parent:
18:87f30ba9ddc5
Child:
20:ea14f175bbb4
--- a/emmaCode.cpp	Wed Jul 15 15:38:08 2015 +0000
+++ b/emmaCode.cpp	Wed Jul 22 11:44:36 2015 +0000
@@ -69,9 +69,12 @@
 //ade7758 variables
 uint16_t AWattHrValue, BWattHrValue, CWattHrValue;
 uint16_t AVAHrValue, BVAHrValue, CVAHrValue;
-long AWattHrSum = 0;
-long BWattHrSum = 0;    
-long CWattHrSum = 0;
+//long AWattHrSum = 0;
+//long BWattHrSum = 0;
+//long CWattHrSum = 0;
+uint32_t AWattHrSum = 0;
+uint32_t BWattHrSum = 0;
+uint32_t CWattHrSum = 0;
 float AWattHr, BWattHr, CWattHr;
 float AVrms, BVrms, CVrms;
 float AIrms, BIrms, CIrms;
@@ -888,6 +891,9 @@
     Timer t;
     Timer tNodes;
     
+    TFT.locate(0,0);
+    TFT.printf(" please wait");
+    
     //check connected interface
     //connectedIface();
     isEthConnected();
@@ -1172,9 +1178,9 @@
         wait(1);
         while(1) {
             checkRxBuffer();
-            checkVoltagePower();
+            //checkVoltagePower();
             //panelEnergy, panelVoltage, and panelPower
-            if(t.read_ms() > 60000) {
+            if(t.read() > 300.0f) {
                 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
                 TFT.locate(0,60);
                 TFT.printf("                                                  ");
@@ -1213,10 +1219,10 @@
                     seconds = time(NULL);
                     for(int j=0; j<sizeof(q); j++) {
                         q[j]=0; }
-                    strftime(q, 32, "%Y-%m-%d %H:%M:%S\r\n",localtime(&seconds));
+                    strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
                     sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
                     emmaUID.c_str(),hmac.c_str(),q,XWattHr,XVrms,XWatt);
-                    //DBG.printf("dataEnergy:%s\r\n",s);
+                    //DBG.printf("dataEnergy:\r\n%s\r\n",s);
                     rest.post(r,s);
                     wait(2);
                     if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
@@ -1248,9 +1254,9 @@
             }
             
             checkRxBuffer();
-            checkVoltagePower();
+            //checkVoltagePower();
             //nodeTemp
-            if(tNodes.read_ms() > 120000) {
+            if(tNodes.read() > 120.0f) {
                 DBG.printf("getNodesTemperature\r\n");
                 
                 for(int i=0; i<NODES_MAX; i++) {
@@ -1281,7 +1287,7 @@
                         strftime(q, 32, "%Y-%m-%d %H:%M:%S\r\n",localtime(&seconds));
                         sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":\"%s\",\"value\":%s}",
                         emmaUID.c_str(),hmac.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
-                        //DBG.printf("dataNodeTemp:%s\r\n",s);
+                        //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
                         rest.post("/emma/api/controller/nodetemp",s);
                         wait(2);
                         temp = rxBuf;
@@ -1294,12 +1300,14 @@
                             }
                         }
                         checkRxBuffer();
-                        checkVoltagePower();
+                        //checkVoltagePower();
                     }    
                 }
-            }    
+                tNodes.reset();
+            }
+                
             checkRxBuffer();
-            checkVoltagePower();
+            //checkVoltagePower();
             //command
             if(newCommand) {
                 DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
@@ -1627,6 +1635,36 @@
 
 /*start energy related*/
 void energyThread(void const*) {
+    Timer tEnergy;
+    
+    while(1) {
+        tEnergy.start();
+        DBG.printf("energyThread-start\r\n");
+        
+        AWattHrSum = 0;
+        BWattHrSum = 0;
+        CWattHrSum = 0;
+        
+        while(tEnergy.read() < 15*60.0) {
+            AWattHrValue = ADE.getWattHR(PHASE_A);
+            BWattHrValue = ADE.getWattHR(PHASE_B);
+            CWattHrValue = ADE.getWattHR(PHASE_C); 
+            
+            AWattHrSum += AWattHrValue;
+            BWattHrSum += BWattHrValue;
+            CWattHrSum += CWattHrValue;   
+        }
+        
+        AWattHr = AWattHrSum * 0.0000198f;
+        BWattHr = BWattHrSum * 0.0000197f;
+        CWattHr = CWattHrSum * 0.0000196f;
+        
+        tEnergy.stop();
+        tEnergy.reset();
+        DBG.printf("energyThread-finish\r\n");
+    }
+    
+    /*
     while(1) {
         //DBG.printf("insideEnergyThread\r\n");
         //osDelay(5000);
@@ -1649,19 +1687,8 @@
         CWattHr = CWattHrSum * ADE.CWhLSB;
         
         DBG.printf("energyThread-finish\r\n");
-        
-        //testing
-        /*
-        DBG.printf("thread called\r\n");
-        Timer t;
-        t.start();
-        while(t.read_ms() < 5000) {
-            t.stop();
-            t.reset();    
-        }
-        DBG.printf("thread ended\r\n");
-        */
     }
+    */
 }
 void checkVoltagePower() {
     //check if voltage or power violates threshold
@@ -1669,19 +1696,25 @@
     string str;
     
     //DBG.printf("checkVoltagePower-start\r\n");
-
-    AVrms = ADE.VRMS(PHASE_A) * 0.000128; //0.000158;   //constants are from calculateVRMS function
-    BVrms = ADE.VRMS(PHASE_B) * 0.000127; //0.000157;
-    CVrms = ADE.VRMS(PHASE_C) * 0.000126; //0.000156;
     
-    AIrms = ADE.IRMS(PHASE_A) * 0.0000125;  //constants are from calculateIRMS function
-    BIrms = ADE.IRMS(PHASE_B) * 0.0000123;
-    CIrms = ADE.IRMS(PHASE_C) * 0.0000124;
+    //vrms dan irms mungkin bisa ditaro di dalam routine energy calculation
+    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;
+    
+    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;
     
     AWatt = AVrms * AIrms;
     BWatt = BVrms * BIrms;
     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);
+    
+    /*
     if(AVrms > VRMSTHRESHOLD || AWatt > WATTTHRESHOLD) {
         DBG.printf("alert on ch1\r\n");
         sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"voltage\":%.2f,\"power\":%.2f}",
@@ -1733,6 +1766,7 @@
             }
         }
     }
+    */
     //DBG.printf("checkVoltagePower-finish\r\n");
 }
 /*end energy related*/