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:
27:562a95c120cc
Parent:
25:36c6a5db50ed
Child:
28:7561035e3df5
--- a/emmaCode.cpp	Sat Jul 25 11:59:35 2015 +0000
+++ b/emmaCode.cpp	Wed Jul 29 14:09:47 2015 +0000
@@ -8,7 +8,7 @@
 //init espduino - without ch_pd pin
 ESP esp(&_ESP, &DBG, ESP_BAUD);
 //init wifi mqtt
-ESPMQTT mqtt(&esp);
+//ESPMQTT mqtt(&esp);
 //init wifi rest
 REST rest(&esp);
 
@@ -94,9 +94,12 @@
 bool useProxy = false;
 bool newCommand = false;
 bool espFreeMemory = true;  //for after bootup initialization
+bool espDHCPClientStart = false;
 bool newEnergyData = false;
 string globalCommand;
 string rxBuf;
+string rxLog;
+string rxLogA;
 
 /*start lcd and touch*/
 int emmaModeSelection(void) {
@@ -885,7 +888,7 @@
 }
 
 void emmaModeOperation(void) {
-    char mqttClientId[32];
+    //char mqttClientId[32];
     char p[64];
     char q[32];
     char r[32];
@@ -1218,34 +1221,54 @@
             
             //whether espFreeMemory occurs
             if(espFreeMemory) {
-                //success if rxInterrupt is disabled
-                
-                //disable UART2
-                NVIC_DisableIRQ(USART2_IRQn);
+                //logging purpose
+                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()));
                 
-                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");
+                espFreeMemory = false;    
+            }
+            
+            //whether espDHCPClientStart occurs
+            if(espDHCPClientStart) {
+                //logging purpose
+                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("emma: success to setup mqtt");    
-                }
-                DBG.printf("emma: system started\r\n");
-        
-                t.start();
-                while(t.read_ms() < 5000) {
-                    esp.process();    
+                    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");
                 }
-                t.stop();
-                t.reset();
+                    
+                //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);
+                    }
+                }
+                */
                 
-                //enable UART2
-                NVIC_EnableIRQ(USART2_IRQn);
-
-                espFreeMemory = false;    
+                espDHCPClientStart = false;    
             }
             
             //panelEnergy, panelVoltage, and panelPower
@@ -1306,6 +1329,11 @@
                             rest.post(r,s);
                             wait(2);
                             if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
+                                //logging purpose
+                                seconds = time(NULL);
+                                strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                                DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData success"));
+                            
                                 DBG.printf("send channel: %d success\r\n",i);
                                 TFT.foreground(Green);
                                 TFT.locate(0,120);
@@ -1317,6 +1345,11 @@
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);    
                             } else {
+                                //logging purpose
+                                seconds = time(NULL);
+                                strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                                DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData failed"));
+                                
                                 DBG.printf("send channel: %d failed\r\n",i);
                                 TFT.foreground(Red);
                                 TFT.locate(0,120);
@@ -1382,8 +1415,16 @@
                     if(str.rfind("/environment") != std::string::npos) {
                         str.erase(str.begin(),str.begin()+str.rfind("/environment"));
                         if(str.find("\"status\":\"success\"") != std::string::npos) {
+                            //logging purpose
+                            seconds = time(NULL);
+                            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                            DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv success"));
                             DBG.printf("send panel environment success\r\n");
                         } else {
+                            //logging purpose
+                            seconds = time(NULL);
+                            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                            DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv failed"));
                             DBG.printf("send panel environment failed\r\n");
                         }
                     }
@@ -1451,6 +1492,7 @@
             }
             
             //command
+            rest.get("/emma/api/controller/command");
             checkRxBuffer();
             if(newCommand) {
                 DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
@@ -1937,7 +1979,7 @@
     DBG.printf("MQTT Connected\r\n");
     char mqttTopic[64];
     sprintf(mqttTopic,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str());
-    mqtt.subscribe(mqttTopic);
+    //mqtt.subscribe(mqttTopic);
 }
 void mqttDisconnected(void* response) {
     DBG.printf("MQTT Disconnected\r\n");    
@@ -1945,6 +1987,22 @@
 /*end wifi mqtt*/
 
 /*start wifi rest*/
+void wifiCb(void* response) {
+    uint32_t status;
+    RESPONSE res(response);
+    
+    if(res.getArgc() == 1) {
+        res.popArgs((uint8_t*)&status,4);
+        if(status == STATION_GOT_IP) {
+            DBG.printf("WIFI Connected\r\n");
+            //wifiConnected = true;
+        }
+        else {
+            //wifiConnected = false;   
+        }
+    }
+}
+
 void rxInterrupt(void) {
     char c;
     
@@ -1969,10 +2027,17 @@
     }
     
     //check free memory -> reinitialize mqtt connection
-    if(rxBuf.rfind("Free memory") != std::string::npos || rxBuf.rfind("dhcp client start") != std::string::npos) {
+    if(rxBuf.rfind("Free memory") != std::string::npos) {
+        rxLog = "Free memory-" + rxBuf;
         espFreeMemory = true;
     }
     
+    //check dhcp client start -> initialize all connection
+    if(rxBuf.rfind("dhcp client start") != std::string::npos) {
+        rxLogA = "dhcp client start-" + rxBuf;
+        espDHCPClientStart = true;
+    }
+    
     //clear rxBuf
     rxBuf.clear();
 }
@@ -2406,6 +2471,35 @@
     string str;
     Timer t;
     
+    esp.enable();
+    wait(1);
+    esp.reset();
+    wait(1);
+    while(!esp.ready());
+    
+    //rest begin
+    if(!rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) {
+        DBG.printf("EMMA: fail to setup rest\r\n");
+        TFT.locate(0,20);
+        TFT.printf("EMMA: fail to setup rest");
+        while(1);    
+    }
+    
+    //setup wifi
+    DBG.printf("EMMA: setup wifi\r\n");
+    TFT.locate(0,20);
+    TFT.printf("EMMA: setup wifi");
+    wait(1);
+    esp.wifiCb.attach(&wifiCb);
+    esp.wifiConnect("Tritronik Mobile","Tri12@11");
+    DBG.printf("EMMA: system started\r\n");
+    TFT.locate(0,20);
+    TFT.printf("EMMA: system started");
+    t.start();
+    while(t.read_ms() < 5000);
+    t.stop();
+    t.reset();
+    
     if(wifiAvailable) {
         _ESP.printf("MODE=B");
         if(useProxy) {
@@ -2421,19 +2515,20 @@
                 s[i]=0; }
             sprintf(s,"/emma/api/controller/test");
         }
-        wait(1);
+        wait(2);
         t.start();
         while(!esp.ready() && t.read_ms() < 5000);
         t.stop();
+        t.reset();
         if(rest.begin(connHost.c_str(),connPort,false)) {
-            //DBG.printf("rest begin\r\n");
+            DBG.printf("rest begin\r\n");
             esp.process();
             rest.get(s);
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
             rest.getResponse(s,sizeof(s));
             str = s;
-            //DBG.printf("response:%s\r\n",s);
+            DBG.printf("response:%s\r\n",s);
             if(str.find("OK") != std::string::npos) {
                 wifiConnected = true;
             }
@@ -2520,4 +2615,20 @@
     }
     return false;
 }
+
+bool writeLog(string logTime, string logData) {
+    FILE *fp;
+    char s[255];
+    string logAll;
+    logAll = logTime + "\t" + logData + "\r\n";
+    
+    sprintf(s,"/sd/log.txt");
+    fp = fopen(s,"a");
+    if(fp != NULL) {
+        fprintf(fp,logAll.c_str());
+        fclose(fp);
+        return true;
+    }
+    return false;    
+}
 /*end emma private function*/
\ No newline at end of file