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:
30:08d290158fa7
Parent:
29:4d94a03d38e0
Child:
31:e2abded4d85f
--- a/emmaCode.cpp	Tue Aug 04 03:26:35 2015 +0000
+++ b/emmaCode.cpp	Thu Aug 06 04:38:09 2015 +0000
@@ -904,6 +904,7 @@
     char q[32];
     char r[32];
     char s[512];
+    int dbgLoop=0;
     int loop=0;
     int trial=0;
     //string hmac;
@@ -1054,10 +1055,11 @@
     } else if(wifiConnected) {
         DBG.printf("emmaModeOperation - wifi\r\n");
         
-        //start new log indicator
+        //start new log and dbg indicator
         seconds = time(NULL);
         strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
         DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++++++++++++"));
+        DBG.printf("newDbg:%d\r\n",writeDbg(q,"++++++++++++++++++++"));
     
         //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work
         //_ESP.printf("MODE=B");
@@ -1237,7 +1239,7 @@
             
             //whether espFreeMemory occurs
             if(espFreeMemory) {
-                //logging purpose
+                //logging
                 seconds = time(NULL);
                 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
                 DBG.printf("logFreeMemory:%d\r\n",writeLog(q,rxLog.c_str()));
@@ -1247,46 +1249,22 @@
             
             //whether espDHCPClientStart occurs
             if(espDHCPClientStart) {
-                //logging purpose
+                //logging
                 seconds = time(NULL);
                 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
                 DBG.printf("logDHCPClientStart:%d\r\n",writeLog(q,rxLogA.c_str()));
                 
                 _ESP.printf("MODE=B");
                 wait(2);
-                    
-                //init rest to server
-                /*
-                if(rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) {
-                    DBG.printf("rest to server is created\r\n");
-                    TFT.locate(0,40);
-                    TFT.printf("                                        ");
-                    TFT.locate(0,40);
-                    TFT.printf("rest to server is created");
-                } else {
-                    DBG.printf("rest to server is NOT created\r\n");
-                    TFT.locate(0,40);
-                    TFT.printf("                                        ");
-                    TFT.locate(0,40);
-                    TFT.printf("rest to server is NOT created");
-                }
-                    
-                //init rest to remotes
-                for(int i=0; i<NODES_MAX; i++) {
-                    if(!nodes[i].ipAddr.empty()) {
-                        DBG.printf("restConn nodes[%d] is created\r\n",i);
-                        nodes[i].restConn->begin(nodes[i].ipAddr.c_str(),16038,false);
-                        wait(1);
-                    } else {
-                        DBG.printf("restConn nodes[%d] is NOT created\r\n",i);
-                        wait(1);
-                    }
-                }
-                */
-                
                 espDHCPClientStart = false;    
             }
             
+            //debugging
+            seconds = time(NULL);
+            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+            sprintf(r,"dbgPE[%d]",dbgLoop);
+            DBG.printf("dbgPE:%d\r\n",writeDbg(q,r));
+            
             //panelEnergy, panelVoltage, and panelPower
             if(tPanelEnergy.read() > 30.0f) {
                 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
@@ -1387,6 +1365,12 @@
                 loop++;    
             }
             
+            //debugging
+            seconds = time(NULL);
+            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+            sprintf(r,"dbgPEnv[%d]",dbgLoop);
+            DBG.printf("dbgPEnv:%d\r\n",writeDbg(q,r));
+            
             //panel environment
             checkRxBuffer();
             if(tPanel.read() > 900.0f) {    //900 is 15 minutes
@@ -1449,6 +1433,12 @@
                 tPanel.reset();
             }
             
+            //debugging
+            seconds = time(NULL);
+            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+            sprintf(r,"dbgNT[%d]",dbgLoop);
+            DBG.printf("dbgNT:%d\r\n",writeDbg(q,r));
+            
             //nodeTemp
             checkRxBuffer();
             if(tNodes.read() > 900.0f) {    //900 is 15 minutes
@@ -1507,6 +1497,12 @@
                 tNodes.reset();
             }
             
+            //debugging
+            seconds = time(NULL);
+            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+            sprintf(r,"dbgCmd[%d]",dbgLoop);
+            DBG.printf("dbgCmd:%d\r\n",writeDbg(q,r));
+            
             //command
             rest.get("/emma/api/controller/command");
             checkRxBuffer();
@@ -1657,6 +1653,10 @@
                 
                 newCommand = false;
             }
+            
+            //debugging
+            dbgLoop++;
+            
             osDelay(5000);    
         }
     }
@@ -2188,6 +2188,7 @@
         }
     }
     fclose(fp);
+    free(fp);
     return strS;    
 }
 
@@ -2200,8 +2201,11 @@
     if(fp != NULL) {
         fprintf(fp,value.c_str());
         fclose(fp);
+        free(fp);
         return true;
     }
+    fclose(fp);
+    free(fp);
     return false;
 }
 /*end emma settings*/
@@ -2236,6 +2240,7 @@
         }
     }
     fclose(fp);
+    free(fp);
     return strS;
 }
 
@@ -2268,6 +2273,7 @@
         }
     }
     fclose(fp);
+    free(fp);
     return strS;    
 }
 
@@ -2287,6 +2293,7 @@
         }    
     }
     closedir(d);
+    free(d);
     return nd;
 }
 
@@ -2618,8 +2625,11 @@
             fprintf(fp,"%c",number);
         }
         fclose(fp);
+        free(fp);
         return true;
     }
+    fclose(fp);
+    free(fp);
     return false;
 }
 
@@ -2632,8 +2642,11 @@
     if(fp != NULL) {
         fprintf(fp,"");
         fclose(fp);
+        free(fp);
         return true;
     }
+    fclose(fp);
+    free(fp);
     return false;
 }
 
@@ -2648,8 +2661,30 @@
     if(fp != NULL) {
         fprintf(fp,logAll.c_str());
         fclose(fp);
+        free(fp);
         return true;
     }
+    fclose(fp);
+    free(fp);
+    return false;    
+}
+
+bool writeDbg(string dbgTime, string dbgData) {
+    FILE *fp;
+    char s[255];
+    string dbgAll;
+    dbgAll = dbgTime + "\t" + dbgData + "\r\n";
+    
+    sprintf(s,"/sd/dbg.txt");
+    fp = fopen(s,"a");
+    if(fp != NULL) {
+        fprintf(fp,dbgAll.c_str());
+        fclose(fp);
+        free(fp);
+        return true;
+    }
+    fclose(fp);
+    free(fp);
     return false;    
 }
 /*end emma private function*/
\ No newline at end of file