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:
46:86a0c2133130
Parent:
45:2406c2b6995a
Child:
47:97eadcb478b2
--- a/emmaCode.cpp	Wed Aug 26 10:00:39 2015 +0000
+++ b/emmaCode.cpp	Wed Aug 26 12:30:52 2015 +0000
@@ -1086,13 +1086,12 @@
         
         //get list of nodes from server
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
-        
-        //sprintf(s,"GET /emma/api/controller/remotes?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
-        sprintf(s,"POST /emma/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",restSERVER.c_str(),connBodyLen,connBody);
+        sprintf(s,"POST /%s/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
         sscanf(restPORT.c_str(),"%d",&connPort);
         str.clear();
         str = ethREST(restSERVER,connPort,s);
         //DBG.printf("str:%s\r\n",str.c_str());
+        
         if(str.rfind("[{\"type\"") != std::string::npos) {
             DBG.printf("get nodes from server\r\n");
             str.erase(str.begin(),str.begin()+str.rfind("[{\"type\""));
@@ -1150,7 +1149,6 @@
             //checkVoltagePower();  //need revision to support ethernet
             
             //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);
                 TFT.locate(0,60);
@@ -1192,22 +1190,20 @@
                             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);
                             
                             connBodyLen = sprintf(connBody,"{\"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",connBody);
+                            sprintf(s,"POST /%s/api/controller/energy/%d HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),i,restSERVER.c_str(),connBodyLen,connBody);
                             
-                            sprintf(r,"POST /emma/api/controller/energy/%d HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",i,restSERVER.c_str(),connBodyLen,connBody);
+                            //DBG.printf("dataEnergy:\r\n%s\r\n",connBody);
                             
                             str.clear();
                             sscanf(restPORT.c_str(),"%d",&connPort);
-                            str = ethREST(restSERVER,connPort,r);
-                            DBG.printf("str:%s",str.c_str());
+                            str = ethREST(restSERVER,connPort,s);
+                            //DBG.printf("str:%s\r\n",str.c_str());
+                            
                             if(str.find("\"status\":\"success\"") != std::string::npos) {
                                 //logging purpose
                                 seconds = time(NULL);
@@ -1243,16 +1239,128 @@
                             }
                         }
                     }
-                    
                     newEnergyData = false;
                 }
                 
                 tPanelEnergy.reset();
                 loop++;    
             }
-            */
             
             //panel environment
+            if(tPanel.read() > 900.0f) {    //900.0f is 15 minutes
+                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
+                if(dTemp!=0 && dHum!=0) {
+                    seconds = time(NULL);
+                    strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                
+                    //calculate hmacTime
+                    sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
+                    hmacTime = calculateMD5(p);
+                
+                    connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
+                    emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas);
+                    sprintf(s,"POST /%s/api/controller/environment HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
+                    
+                    //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
+                    
+                    str.clear();
+                    sscanf(restPORT.c_str(),"%d",&connPort);
+                    str = ethREST(restSERVER,connPort,s);
+                    //DBG.printf("str:%s\r\n",str.c_str());
+                    
+                    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");
+                    }
+                }
+                tPanel.reset();
+            }
+            
+            //node temp
+            if(tNodes.read() > 60.0f) {    //900 is 15 minutes
+                DBG.printf("getNodesTemperature\r\n");
+                
+                for(int i=0; i<NODES_MAX; i++) {
+                    if(!nodes[i].ipAddr.empty() && nodes[i].type == 1) {
+                        //get node's temp
+                        string temp;
+                        str.clear();
+                        str = ethREST(nodes[i].ipAddr,REMOTE_TCP_PORT,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
+                        //DBG.printf("str:%s\r\n",str.c_str());
+
+                        if(str.find("200 OK")) {
+                            if(str.rfind("temp=") != std::string::npos) {
+                                str.erase(str.begin(),str.begin()+str.rfind("temp=")+6);
+                                str.erase(str.begin()+str.find("\""),str.end());
+                                temp = str;
+                            } else {
+                                temp = "0"; //connect to node, but receive none
+                            }
+                        } else {
+                            temp = "0";     //not connected to node
+                        }
+                        DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
+                        
+                        //send node's temp
+                        if(temp != "0") {
+                            seconds = time(NULL);
+                            strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
+                        
+                            //calculate hmacTime
+                            sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
+                            hmacTime = calculateMD5(p);
+                            
+                            connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":%s,\"value\":%s}",
+                            emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
+                            sprintf(s,"POST /%s/api/controller/nodetemp HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
+                    
+                            //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
+                            
+                            str.clear();
+                            sscanf(restPORT.c_str(),"%d",&connPort);
+                            str = ethREST(restSERVER,connPort,s);
+                            //DBG.printf("str:%s\r\n",str.c_str());
+                            
+                            if(str.find("\"status\":\"success\"") != std::string::npos) {
+                                DBG.printf("send nodeTemp success\r\n");
+                            } else {
+                                DBG.printf("send nodeTemp failed\r\n");
+                            }
+                        }
+                    }    
+                }
+                tNodes.reset();
+            }
             
             //command
             connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
@@ -1300,7 +1408,7 @@
                         if(commandNType == "0") {       //switch on panel controller
                             DBG.printf("command for switch\r\n");
                         }
-                        else if(commandNType == "1") {  //ir&rf remote control
+                        else if(commandNType == "1" || commandNType == "2") {  //ir&rf remote control or wifi smart plug
                             DBG.printf("command for remote control\r\n");
                     
                             //get index of node list based on mac address
@@ -1316,14 +1424,18 @@
                             if(idx != NODES_INVALID) {
                                 DBG.printf("index found at %d\r\n",idx);
                         
-                                //get cmd string based on device type and command number
-                                string nodeCmd;
-                                nodeCmd = readNodeCmd(commandDType,commandCmd);
-                        
+                                if(commandNType == "1"){
+                                    //get cmd string based on device type and command number
+                                    string nodeCmd;
+                                    nodeCmd = readNodeCmd(commandDType,commandCmd);
+                                    sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
+                                } else if(commandNType == "2") {
+                                    sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str());
+                                }
+                                
                                 //execute command
                                 DBG.printf("executing command\r\n");
-                                sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
-                            
+                                
                                 trial=0;
                                 while(1) {
                                     if(trial>=2) {   //two times trial
@@ -1399,115 +1511,13 @@
                                 str.clear();
                                 trial++;
                             }
-                        } else if(commandNType == "2") {  //wifi smart plug
-                            /*
-                            DBG.printf("command for smart plug\r\n");
-                    
-                            //get index of node list based on mac address
-                            int idx = NODES_INVALID;
-                            for(int i=0; i<NODES_MAX; i++) {
-                                if(!nodes[i].macAddr.compare(commandNAddr)) {
-                                    idx = i;
-                                }
-                            }
-                    
-                            //execution process
-                            string execResult = "failed";
-                            if(idx != NODES_INVALID) {
-                                DBG.printf("index found at %d\r\n",idx);
-                                
-                                //execute command
-                                DBG.printf("executing command\r\n");
-                                sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str());
-                            
-                                trial=0;
-                                while(1) {
-                                    rxBuf.clear();
-                                    if(trial>=2) {   //two times trial
-                                        DBG.printf("cmd is not executed\r\n");
-                                        TFT.foreground(Red);
-                                        TFT.locate(0,180);
-                                        TFT.printf("                                        ");
-                                        TFT.locate(0,180);
-                                        TFT.printf("cmd is not executed");
-                                        wait(1);
-                                        TFT.locate(0,180);
-                                        TFT.printf("                                        ");
-                                        TFT.foreground(White);
-                                        break;    
-                                    }
-                                    nodes[idx].restConn->get("/",s);
-                                    wait(2);
-                                    if(rxBuf.find("REST: status = 200") != std::string::npos) {
-                                        DBG.printf("cmd is executed\r\n");
-                                        TFT.foreground(Green);
-                                        TFT.locate(0,180);
-                                        TFT.printf("                                        ");
-                                        TFT.locate(0,180);
-                                        TFT.printf("cmd is executed");
-                                        wait(1);
-                                        TFT.locate(0,180);
-                                        TFT.printf("                                        ");
-                                        TFT.foreground(White);
-                                        execResult = "success";
-                                        break;    
-                                    }
-                                    trial++;
-                                }    
-                            } else {
-                                TFT.foreground(Red);
-                                TFT.locate(0,180);
-                                TFT.printf("                                        ");
-                                TFT.locate(0,180);
-                                TFT.printf("node is invalid");
-                                wait(1);
-                                TFT.locate(0,180);
-                                TFT.printf("                                        ");
-                                TFT.foreground(White);    
-                            }
-                    
-                            wait(2);       
-                            //send execution result
-                            //DBG.printf("send execution result\r\n");
-                        
-                            //calculate hmacCmd
-                            sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());   
-                            hmacCmd = calculateMD5(p);
-                        
-                            sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
-                            emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str());
-                    
-                            trial=0;
-                            while(1) {
-                                if(trial>=2) {   //two times trial
-                                    DBG.printf("failed to send execution result\r\n");
-                                    break;    
-                                }
-                                sprintf(p,"/%s/api/controller/result",appNAME.c_str());
-                                rest.post(p,s);
-                                //rest.post("/emma/api/controller/result",s);   //working
-                                wait(2);
-                                str = rxBuf;
-                                if(str.rfind("/result") != std::string::npos) {
-                                    str.erase(str.begin(),str.begin()+str.rfind("/result"));
-                                    if(str.find("\"status\":\"success\"") != std::string::npos) {
-                                        DBG.printf("success to send execution result\r\n");
-                                        break;
-                                    }
-                                }
-                                //checkRxBuffer();
-                                trial++;
-                            }
-                            */
                         }
                     }
                 }
                 //clear text on lcd
                 TFT.locate(0,160);
                 TFT.printf("                                        ");
-                
             }
-            
             osDelay(5000);   
         }
     } else if(wifiConnected) {
@@ -2588,10 +2598,10 @@
 void emmaModeReserved(void) {
     char connBody[128];
     char s[256];
-    char buf[1024];
+    //char buf[1024];
     int connBodyLen;
     int i=0;
-    int ret=99;
+    //int ret=99;
     string str;
     Timer t;
     //TCPSocketConnection sock;