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:
50:50d98fe6cc15
Parent:
49:d11a1914ee8b
Child:
51:ad6410dcaf52
--- a/emmaCode.cpp	Thu Sep 03 07:10:35 2015 +0000
+++ b/emmaCode.cpp	Thu Sep 03 11:19:42 2015 +0000
@@ -45,14 +45,14 @@
 string mqttKEY;
 string mqttSECRET;
 string mqttSERVER;
-string mqttPORT;
+int mqttPORT;
 string restSERVER;
-string restPORT;
+int restPORT;
 string wifiSSID;
 string wifiPASS;
 string gprsAPN;
 string proxySERVER;
-string proxyPORT;
+int proxyPORT;
 string proxyAUTH;
 int powerPHASE;
 
@@ -302,7 +302,6 @@
     
     //read settings
     emmaUID = readSetting("emmaUID");
-    //emmaUID = "066eff575349896767073038";
     DBG.printf("emmaUID:%s\r\n",emmaUID.c_str());
     
     //calculate hmac
@@ -323,21 +322,24 @@
     DBG.printf("mqttSECRET:%s\r\n",mqttSECRET.c_str());
     mqttSERVER = readSetting("mqttSERVER");
     DBG.printf("mqttSERVER:%s\r\n",mqttSERVER.c_str());
-    mqttPORT = readSetting("mqttPORT");
-    DBG.printf("mqttPORT:%s\r\n",mqttPORT.c_str());
+    str = readSetting("mqttPORT");
+    sscanf(str.c_str(),"%d",&mqttPORT);
+    DBG.printf("mqttPORT:%d\r\n",mqttPORT);
     
     restSERVER = readSetting("restSERVER");
     DBG.printf("restSERVER:%s\r\n",restSERVER.c_str());
-    restPORT = readSetting("restPORT");
-    DBG.printf("restPORT:%s\r\n",restPORT.c_str());
+    str = readSetting("restPORT");
+    sscanf(str.c_str(),"%d",&restPORT);
+    DBG.printf("restPORT:%d\r\n",restPORT);
     
     gprsAPN = readSetting("gprsAPN");
     DBG.printf("gprsAPN:%s\r\n",gprsAPN.c_str());
     
     proxySERVER = readSetting("proxySERVER");
     DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str());
-    proxyPORT = readSetting("proxyPORT");
-    DBG.printf("proxyPORT:%s\r\n",proxyPORT.c_str());
+    str = readSetting("proxyPORT");
+    sscanf(str.c_str(),"%d",&proxyPORT);
+    DBG.printf("proxyPORT:%s\r\n",proxyPORT);
     proxyAUTH = readSetting("proxyAUTH");
     DBG.printf("proxyAUTH:%s\r\n",proxyAUTH.c_str());
     wifiSSID = readSetting("wifiSSID");
@@ -356,7 +358,7 @@
     DBG.printf("mode:%d\r\n",mode);
     
     //check proxy
-    if(!proxySERVER.empty() && !proxyPORT.empty() && !proxyAUTH.empty()) {
+    if(!proxySERVER.empty() && !proxyAUTH.empty()) {
         useProxy = true;
     } else {
         useProxy = false;    
@@ -730,18 +732,15 @@
         if(useProxy) {
             DBG.printf("use proxy\r\n");
             connHost = proxySERVER;
-            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            connPort = proxyPORT;
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            //sprintf(s,"GET http://%s:%s/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
-            //sprintf(s,"POST http://%s:%s/emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: 76\n\n{\"uid\":\"%s\",\"hmac\":\"%s\"}\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),emmaUID.c_str(),hmac.c_str());
-            sprintf(s,"POST http://%s:%s/%s/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
+            sprintf(s,"POST http://%s:%d/%s/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
             connData = s;
         } else {
             DBG.printf("no proxy\r\n");
             connHost = restSERVER;
-            //connPort = restPORT;
-            sscanf(restPORT.c_str(),"%d",&connPort);
+            connPort = restPORT;
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
             //sprintf(s,"GET /emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
@@ -813,8 +812,7 @@
             DBG.printf("use proxy\r\n");
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            //sprintf(s,"GET http://%s:%s/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),regKey.c_str(),hmac.c_str(),restSERVER.c_str());
-            sprintf(s,"POST http://%s:%s/%s/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
+            sprintf(s,"POST http://%s:%d/%s/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
             connData = s;
         } else {
             DBG.printf("no proxy\r\n");
@@ -879,11 +877,10 @@
         //set connHost, connPort
         if(useProxy) {
             connHost = proxySERVER;
-            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            connPort = proxyPORT;
         } else {
             connHost = restSERVER;
-            //connPort = restPORT;
-            sscanf(restPORT.c_str(),"%d",&connPort);
+            connPort = restPORT;
         }
         TFT.locate(0,0);
         TFT.printf(" emmaModeRegister");
@@ -905,10 +902,7 @@
         if(wifiConnected) {
             //check proxy
             if(useProxy) {
-                sprintf(r,"http://%s:%s/%s/api/controller/register",restSERVER.c_str(),restPORT.c_str(),appNAME.c_str());
-                //sprintf(r,"http://%s:%d/emma/api/controller/register HTTP/1.0\nHost: %s:%d\nProxy-Authorization: Basic %s\nCache-Control: no-cache\nContent-Length: %d\n\n",restSERVER,restPORT,restSERVER,restPORT,proxyAUTH.c_str(),76);
-                //sprintf(r,"http://%s:%d/emma/api/controller/register HTTP/1.0\nAccept: */*\nAccept-Encoding: gzip,deflate\nContent-Length: %d\r\n\r\n",restSERVER,restPORT,76);
-                //sprintf(r,"http://%s:%d/emma/coba",restSERVER,REST_SERVER_PORT);
+                sprintf(r,"http://%s:%d/%s/api/controller/register",restSERVER.c_str(),restPORT,appNAME.c_str());
             } else {
                 sprintf(r,"/%s/api/controller/register",appNAME.c_str());
             }
@@ -980,7 +974,7 @@
             
             //check proxy
             if(useProxy) {
-                sprintf(r,"http://%s:%s/%s/api/controller/verify",restSERVER.c_str(),restPORT.c_str(),appNAME.c_str());
+                sprintf(r,"http://%s:%d/%s/api/controller/verify",restSERVER.c_str(),restPORT,appNAME.c_str());
             } else {
                 sprintf(r,"/%s/api/controller/verify",appNAME.c_str());
             }
@@ -1034,7 +1028,7 @@
         DBG.printf("emmaModeRegister - gprs\r\n");
         
         //register
-        sscanf(restPORT.c_str(),"%d",&connPort);
+        connPort = restPORT;
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         sprintf(s,"POST /%s/api/controller/register HTTP/1.1\nHost: %s:%d\nContent-Length: %d\n\n%s\n\n%c",appNAME.c_str(),restSERVER.c_str(),connPort,connBodyLen,connBody,26);
         while(!emmaGetRegKey) {
@@ -1213,7 +1207,7 @@
         //get list of nodes from server
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         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);
+        connPort = restPORT;
         str.clear();
         str = ethREST(restSERVER,connPort,s);
         //DBG.printf("str:%s\r\n",str.c_str());
@@ -1337,8 +1331,8 @@
                             
                             //DBG.printf("dataEnergy:\r\n%s\r\n",connBody);
                             
+                            connPort = restPORT;
                             str.clear();
-                            sscanf(restPORT.c_str(),"%d",&connPort);
                             str = ethREST(restSERVER,connPort,s);
                             //DBG.printf("str:%s\r\n",str.c_str());
                             
@@ -1422,8 +1416,8 @@
                     
                     //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
                     
+                    connPort = restPORT;
                     str.clear();
-                    sscanf(restPORT.c_str(),"%d",&connPort);
                     str = ethREST(restSERVER,connPort,s);
                     //DBG.printf("str:%s\r\n",str.c_str());
                     
@@ -1484,8 +1478,8 @@
                     
                             //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
                             
+                            connPort = restPORT;
                             str.clear();
-                            sscanf(restPORT.c_str(),"%d",&connPort);
                             str = ethREST(restSERVER,connPort,s);
                             //DBG.printf("str:%s\r\n",str.c_str());
                             
@@ -1503,7 +1497,7 @@
             //command
             connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
             sprintf(s,"POST /%s/api/controller/command 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);
+            connPort = restPORT;
             str = ethREST(restSERVER,connPort,s);
             //DBG.printf("str:%s\r\n",str.c_str());
             
@@ -1629,7 +1623,7 @@
                             emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str());
                             
                             sprintf(s,"POST /%s/api/controller/result 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);
+                            connPort = restPORT;
                     
                             trial=0;
                             while(1) {
@@ -1711,7 +1705,7 @@
         //end start beforehand
         
         //init rest to server
-        sscanf(restPORT.c_str(),"%d",&connPort);
+        connPort = restPORT;
         if(rest.begin(restSERVER.c_str(),connPort,false)) {
             DBG.printf("rest to server is created\r\n");
             TFT.locate(0,40);
@@ -2201,7 +2195,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 node ip address based on node mac address
                             //string nodeIP;
@@ -2222,13 +2216,17 @@
                             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) {
@@ -2278,11 +2276,6 @@
                     
                             wait(2);       
                             //send execution result
-                            //DBG.printf("send execution result\r\n");
-                        
-                            //calculate hmacCmd
-                            //for(int j=0; j<sizeof(p); j++) {
-                            //    p[j]=0; }
                             sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());   
                             hmacCmd = calculateMD5(p);
                         
@@ -2308,107 +2301,10 @@
                                     }
                                 }
                                 //checkRxBuffer();
+                                rxBuf.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
@@ -2435,7 +2331,7 @@
         //get list of nodes from server
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         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);
+        connPort = restPORT;
         str.clear();
         str = gprsREST(restSERVER,connPort,s);
         //DBG.printf("str:%s\r\n",str.c_str());
@@ -2510,7 +2406,7 @@
         tNodes.start();
         wait(1);
         while(1) {
-            //checkVoltagePower();  //need revision to support gprs
+            checkVoltagePower();  //need revision to support gprs
             
             //panelEnergy, panelVoltage, and panelPower
             if(tPanelEnergy.read() > 30.0f) {
@@ -2562,8 +2458,8 @@
                             
                             //DBG.printf("dataEnergy:\r\n%s\r\n",connBody);
                             
+                            connPort = restPORT;
                             str.clear();
-                            sscanf(restPORT.c_str(),"%d",&connPort);
                             str = gprsREST(restSERVER,connPort,s);
                             //DBG.printf("str:%s\r\n",str.c_str());
                             
@@ -2647,8 +2543,8 @@
                     
                     //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
                     
+                    connPort = restPORT;
                     str.clear();
-                    sscanf(restPORT.c_str(),"%d",&connPort);
                     str = gprsREST(restSERVER,connPort,s);
                     //DBG.printf("str:%s\r\n",str.c_str());
                     
@@ -2710,8 +2606,8 @@
                     
                             //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
                             
+                            connPort = restPORT;
                             str.clear();
-                            sscanf(restPORT.c_str(),"%d",&connPort);
                             str = gprsREST(restSERVER,connPort,s);
                             //DBG.printf("str:%s\r\n",str.c_str());
                             
@@ -2729,7 +2625,7 @@
             //command
             connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
             sprintf(s,"POST /%s/api/controller/command 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);
+            connPort = restPORT;
             str = gprsREST(restSERVER,connPort,s);
             //DBG.printf("str:%s\r\n",str.c_str());
             
@@ -2855,7 +2751,7 @@
                             emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str());
                             
                             sprintf(s,"POST /%s/api/controller/result 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);
+                            connPort = restPORT;
                     
                             trial=0;
                             while(1) {
@@ -3215,7 +3111,7 @@
     hmacTime = calculateMD5(p);
     
     //read connPort
-    sscanf(restPORT.c_str(),"%d",&connPort);
+    connPort = restPORT;
     
     if(vrmsTHL > AVrms) {
         alertType = 1;
@@ -3257,9 +3153,11 @@
     } else if (AWatt > wattTHH) {
         alertType = 4;
     }
+    
+    connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
+    emmaUID.c_str(),hmacTime.c_str(),q,AWatt,alertType);
+    
     if(alertType != 0 && ethConnected && allowAlertAWatt) {
-        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
-        emmaUID.c_str(),hmacTime.c_str(),q,AWatt,alertType);
         sprintf(s,"POST /%s/api/controller/alert/1 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
         str.clear();
         str = ethREST(restSERVER,connPort,s);
@@ -3290,9 +3188,11 @@
     } else if (BVrms > vrmsTHH) {
         alertType = 2;
     }
+    
+    connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
+    emmaUID.c_str(),hmacTime.c_str(),q,BVrms,alertType);
+    
     if(alertType != 0 && ethConnected && allowAlertBVrms) {
-        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
-        emmaUID.c_str(),hmacTime.c_str(),q,BVrms,alertType);
         sprintf(s,"POST /%s/api/controller/alert/2 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
         str.clear();
         str = ethREST(restSERVER,connPort,s);
@@ -3323,9 +3223,11 @@
     } else if (BWatt > wattTHH) {
         alertType = 4;
     }
+    
+    connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
+    emmaUID.c_str(),hmacTime.c_str(),q,BWatt,alertType);
+    
     if(alertType != 0 && ethConnected && allowAlertBWatt) {
-        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
-        emmaUID.c_str(),hmacTime.c_str(),q,BWatt,alertType);
         sprintf(s,"POST /%s/api/controller/alert/2 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
         str.clear();
         str = ethREST(restSERVER,connPort,s);
@@ -3356,9 +3258,11 @@
     } else if (CVrms > vrmsTHH) {
         alertType = 2;
     }
+    
+    connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
+    emmaUID.c_str(),hmacTime.c_str(),q,CVrms,alertType);
+    
     if(alertType != 0 && ethConnected && allowAlertCVrms) {
-        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
-        emmaUID.c_str(),hmacTime.c_str(),q,CVrms,alertType);
         sprintf(s,"POST /%s/api/controller/alert/3 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
         str.clear();
         str = ethREST(restSERVER,connPort,s);
@@ -3389,9 +3293,11 @@
     } else if (CWatt > wattTHH) {
         alertType = 4;
     }
+    
+    connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
+    emmaUID.c_str(),hmacTime.c_str(),q,CWatt,alertType);
+    
     if(alertType != 0 && ethConnected && allowAlertCWatt) {
-        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
-        emmaUID.c_str(),hmacTime.c_str(),q,CWatt,alertType);
         sprintf(s,"POST /%s/api/controller/alert/3 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
         str.clear();
         str = ethREST(restSERVER,connPort,s);
@@ -3954,13 +3860,13 @@
     if(ethAvailable) {
         if(useProxy) {
             connHost = proxySERVER;
-            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            connPort = proxyPORT;
             //for(int i=0; i<sizeof(s); i++) {
             //    s[i]=0; }
-            sprintf(s,"GET http://%s:%s/%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),appNAME.c_str(),restSERVER.c_str());
+            sprintf(s,"GET http://%s:%d/%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str());
         } else {
             connHost = restSERVER;
-            sscanf(restPORT.c_str(),"%d",&connPort);
+            connPort = restPORT;
             //for(int i=0; i<sizeof(s); i++) {
             //    s[i]=0; }
             sprintf(s,"GET /%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",appNAME.c_str(),restSERVER.c_str());
@@ -4030,13 +3936,13 @@
         _ESP.printf("MODE=B");
         if(useProxy) {
             connHost = proxySERVER;
-            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            connPort = proxyPORT;
             //for(int i=0; i<sizeof(s); i++) {
             //    s[i]=0; }
-            sprintf(s,"http://%s:%s/%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),appNAME.c_str(),restSERVER.c_str());
+            sprintf(s,"http://%s:%d/%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str());
         } else {
             connHost = restSERVER;
-            sscanf(restPORT.c_str(),"%d",&connPort);
+            connPort = restPORT;
             //for(int i=0; i<sizeof(s); i++) {
             //    s[i]=0; }
             sprintf(s,"/%s/api/controller/test",appNAME.c_str());
@@ -4081,7 +3987,7 @@
             str.erase(str.end()-2,str.end());
             DBG.printf("IP Addr:%s\r\n",str.c_str());
             //test connection
-            sscanf(restPORT.c_str(),"%d",&connPort);
+            connPort = restPORT;
             sprintf(cmd,"GET /%s/api/controller/test HTTP/1.0\nHost: %s:%d\n\n\n\n%c",appNAME.c_str(),restSERVER.c_str(),connPort,26);
             str = gprsREST(restSERVER,connPort,cmd);
             DBG.printf("str:%s\r\n",str.c_str());