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:
24:5d58515ba510
Parent:
23:fb369e171b7d
Child:
25:36c6a5db50ed
--- a/emmaCode.cpp	Thu Jul 23 07:15:51 2015 +0000
+++ b/emmaCode.cpp	Fri Jul 24 05:16:00 2015 +0000
@@ -93,6 +93,7 @@
 bool gprsConnected = false;
 bool useProxy = false;
 bool newCommand = false;
+bool espFreeMemory = true;  //for after bootup initialization
 bool newEnergyData = false;
 string globalCommand;
 string rxBuf;
@@ -897,8 +898,9 @@
     string str;
     time_t seconds;
     Timer t;
+    Timer tPanelEnergy;
+    Timer tPanel;
     Timer tNodes;
-    Timer tPanel;
     
     TFT.locate(0,0);
     TFT.printf(" please wait");
@@ -1043,6 +1045,7 @@
         //wait(1);
         //while(!esp.ready());
         
+        /*
         DBG.printf("emma: setup mqtt client\r\n");
         sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
     
@@ -1055,13 +1058,14 @@
             TFT.printf("emma: success to setup mqtt");    
         }
         DBG.printf("emma: system started\r\n");
+        */
         
-        t.start();
-        while(t.read_ms() < 5000) {
-            esp.process();    
-        }
-        t.stop();
-        t.reset();
+        //t.start();
+        //while(t.read_ms() < 5000) {
+        //    esp.process();    
+        //}
+        //t.stop();
+        //t.reset();
         
         //set ade7758 parameter
         ADE.begin();
@@ -1157,6 +1161,27 @@
             DBG.printf("no nodes from server\r\n");    
         }
         
+        /*
+        //success
+        DBG.printf("emma: setup mqtt client\r\n");
+        sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
+        if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
+            mqtt.connectedCb.attach(&mqttConnected);
+            mqtt.disconnectedCb.attach(&mqttDisconnected);
+            mqtt.connect(MQTT_HOST,MQTT_PORT,false);
+            DBG.printf("emma: success to setup mqtt\r\n");
+            TFT.locate(0,40);
+            TFT.printf("emma: success to setup mqtt");    
+        }
+        DBG.printf("emma: system started\r\n");
+        
+        t.start();
+        while(t.read_ms() < 5000) {
+            esp.process();    
+        }
+        t.stop();
+        t.reset();
+        */
         
         //preset nodes' macAddr and ipAddr
         //nodes[0].macAddr = "002629034222";
@@ -1183,16 +1208,48 @@
         //create thread
         osThreadCreate(osThread(energyThread),NULL);
         
-        t.start();
+        tPanelEnergy.start();
+        tPanel.start();
         tNodes.start();
-        tPanel.start();
         wait(1);
         while(1) {
             checkRxBuffer();
             checkVoltagePower();
             
+            //whether espFreeMemory occurs
+            if(espFreeMemory) {
+                //success if rxInterrupt is disabled
+                
+                //disable UART2
+                NVIC_DisableIRQ(USART2_IRQn);
+                
+                DBG.printf("emma: setup mqtt client\r\n");
+                sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
+                if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
+                    mqtt.connectedCb.attach(&mqttConnected);
+                    mqtt.disconnectedCb.attach(&mqttDisconnected);
+                    mqtt.connect(MQTT_HOST,MQTT_PORT,false);
+                    DBG.printf("emma: success to setup mqtt\r\n");
+                    TFT.locate(0,40);
+                    TFT.printf("emma: success to setup mqtt");    
+                }
+                DBG.printf("emma: system started\r\n");
+        
+                t.start();
+                while(t.read_ms() < 5000) {
+                    esp.process();    
+                }
+                t.stop();
+                t.reset();
+                
+                //enable UART2
+                NVIC_EnableIRQ(USART2_IRQn);
+
+                espFreeMemory = false;    
+            }
+            
             //panelEnergy, panelVoltage, and panelPower
-            if(t.read() > 30.0f) {
+            if(tPanelEnergy.read() > 30.0f) {
                 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
                 TFT.locate(0,60);
                 TFT.printf("                                                  ");
@@ -1228,52 +1285,56 @@
                             XVrms = CVrms;
                             XWatt = CWatt;
                         }
-                        sprintf(r,"/emma/api/controller/energy/%d",i);
-                        seconds = time(NULL);
-                        //for(int j=0; j<sizeof(q); j++) {
-                        //    q[j]=0; }
-                        strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                        
+                        if(XWattHr != 0.0f) {
                         
-                        //calculate hmacTime
-                        for(int j=0; j<sizeof(p); j++) {
-                            p[j]=0; }
-                        sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
-                        hmacTime = calculateMD5(p);
+                            sprintf(r,"/emma/api/controller/energy/%d",i);
+                            seconds = time(NULL);
+                            //for(int j=0; j<sizeof(q); j++) {
+                            //    q[j]=0; }
+                            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                        
+                            //calculate hmacTime
+                            for(int j=0; j<sizeof(p); j++) {
+                                p[j]=0; }
+                            sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
+                            hmacTime = calculateMD5(p);
                         
-                        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
-                        emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
-                        //DBG.printf("dataEnergy:\r\n%s\r\n",s);
-                        rest.post(r,s);
-                        wait(2);
-                        if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
-                            DBG.printf("send channel: %d success\r\n",i);
-                            TFT.foreground(Green);
-                            TFT.locate(0,120);
-                            TFT.printf("                                        ");
-                            TFT.locate(0,120);
-                            TFT.printf("send ch%d success",i);
-                            wait(1);
-                            TFT.locate(0,120);
-                            TFT.printf("                                        ");
-                            TFT.foreground(White);    
-                        } else {
-                            DBG.printf("send channel: %d failed\r\n",i);
-                            TFT.foreground(Red);
-                            TFT.locate(0,120);
-                            TFT.printf("                                        ");
-                            TFT.locate(0,120);
-                            TFT.printf("send ch%d failed",i);
-                            wait(1);
-                            TFT.locate(0,120);
-                            TFT.printf("                                        ");
-                            TFT.foreground(White);    
+                            sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
+                            emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
+                            //DBG.printf("dataEnergy:\r\n%s\r\n",s);
+                            rest.post(r,s);
+                            wait(2);
+                            if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
+                                DBG.printf("send channel: %d success\r\n",i);
+                                TFT.foreground(Green);
+                                TFT.locate(0,120);
+                                TFT.printf("                                        ");
+                                TFT.locate(0,120);
+                                TFT.printf("send ch%d success",i);
+                                wait(1);
+                                TFT.locate(0,120);
+                                TFT.printf("                                        ");
+                                TFT.foreground(White);    
+                            } else {
+                                DBG.printf("send channel: %d failed\r\n",i);
+                                TFT.foreground(Red);
+                                TFT.locate(0,120);
+                                TFT.printf("                                        ");
+                                TFT.locate(0,120);
+                                TFT.printf("send ch%d failed",i);
+                                wait(1);
+                                TFT.locate(0,120);
+                                TFT.printf("                                        ");
+                                TFT.foreground(White);    
+                            }
                         }
                     }
                     
                     newEnergyData = false;
                 }
                 
-                t.reset();
+                tPanelEnergy.reset();
                 loop++;    
             }
             
@@ -1302,30 +1363,32 @@
                 }
                 
                 //send environment sensor
-                seconds = time(NULL);
-                strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                if(dTemp!=0 && dHum!=0) {
+                    seconds = time(NULL);
+                    strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
                 
-                //calculate hmacTime
-                for(int j=0; j<sizeof(p); j++) {
-                    p[j]=0; }
-                sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
-                hmacTime = calculateMD5(p);
+                    //calculate hmacTime
+                    for(int j=0; j<sizeof(p); j++) {
+                        p[j]=0; }
+                    sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
+                    hmacTime = calculateMD5(p);
                 
-                sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
-                emmaUID.c_str(),hmacTime.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");
+                    sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
+                    emmaUID.c_str(),hmacTime.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();
                 }
-                checkRxBuffer();
                 tPanel.reset();
             }
             
@@ -1905,6 +1968,11 @@
         }
     }
     
+    //check free memory -> reinitialize mqtt connection
+    if(rxBuf.rfind("Free memory") != std::string::npos || rxBuf.rfind("dhcp client start") != std::string::npos) {
+        espFreeMemory = true;
+    }
+    
     //clear rxBuf
     rxBuf.clear();
 }