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:
32:78633fb9b2a1
Parent:
31:e2abded4d85f
Child:
33:232069745431
Child:
34:8555d471b880
--- a/emmaCode.cpp	Fri Aug 07 07:40:23 2015 +0000
+++ b/emmaCode.cpp	Mon Aug 10 07:04:52 2015 +0000
@@ -324,7 +324,7 @@
         useProxy = false;    
     }
     //testing purpose
-    useProxy = false;
+    //useProxy = false;
     DBG.printf("proxy:%d\r\n",useProxy);
     
     //check available interface
@@ -650,15 +650,15 @@
             sscanf(proxyPORT.c_str(),"%d",&connPort);
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"GET http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            sprintf(s,"GET http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,emmaUID.c_str(),hmac.c_str(),REST_SERVER_HOST);
             connData = s;
         } else {
             DBG.printf("no proxy\r\n");
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
+            connHost = REST_SERVER_HOST;
+            connPort = REST_SERVER_PORT;
             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(),EMMA_SERVER_HOST);
+            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(),REST_SERVER_HOST);
             connData = s;
         }
         
@@ -718,13 +718,13 @@
             DBG.printf("use proxy\r\n");
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"GET http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            sprintf(s,"GET http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str(),REST_SERVER_HOST);
             connData = s;
         } else {
             DBG.printf("no proxy\r\n");
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"GET /emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            sprintf(s,"GET /emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),regKey.c_str(),hmac.c_str(),REST_SERVER_HOST);
             connData = s;
         }
         
@@ -771,8 +771,8 @@
             connHost = proxySERVER;
             sscanf(proxyPORT.c_str(),"%d",&connPort);
         } else {
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
+            connHost = REST_SERVER_HOST;
+            connPort = REST_SERVER_PORT;
         }
         TFT.locate(0,0);
         TFT.printf(" emmaModeRegister");
@@ -780,28 +780,37 @@
         //rest begin
         if(!rest.begin(connHost.c_str(),connPort,false)) {
             DBG.printf("EMMA: fail to setup rest");
+            TFT.locate(0,20);
+            TFT.printf(" fail setup connection");
+            TFT.locate(0,40);
+            TFT.printf(" check your wifi connection or");
+            TFT.locate(0,60);
+            TFT.printf(" you should setup proxy!");
             while(1);    
         }
         //wifiConnected = true;   //with custom firmware, wifi module should connect automatically
         
         esp.process();
         if(wifiConnected) {
-            //set connData
+            //check proxy
             if(useProxy) {
-                for(int i=0; i<sizeof(s); i++) {
-                    s[i]=0; }
-                sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str());
-                connData = s;
+                sprintf(r,"http://%s:%d/emma/api/controller/register",REST_SERVER_HOST,REST_SERVER_PORT);
+                //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",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST,REST_SERVER_PORT,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",REST_SERVER_HOST,REST_SERVER_PORT,76);
+                //sprintf(r,"http://%s:%d/emma/coba",REST_SERVER_HOST,REST_SERVER_POR);
             } else {
-                for(int i=0; i<sizeof(s); i++) {
-                    s[i]=0; }
-                sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
-                connData = s;
+                sprintf(r,"/emma/api/controller/register");
             }
          
             //register
             while(!emmaGetRegKey) {
-                rest.get(connData.c_str());
+                sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+                DBG.printf("s:%s\r\n",s);
+                //string iniStr = "{\"uid\":\"005300553533510334313732\",\"hmac\":\"45fc6a4447989a9434d1f21087a78061\"}";
+                //DBG.printf("s:%s\r\n",iniStr.c_str());
+                
+                //rest.post(r,iniStr.c_str());
+                rest.post(r,s);
                 for(int i=0; i<sizeof(s); i++) {
                     s[i]=0; }
                 rest.getResponse(s,sizeof(s));
@@ -848,6 +857,7 @@
                         }
                     }
                 }
+                wait(1);
             }
                 
             //calculate hmac
@@ -857,24 +867,17 @@
             hmac = calculateMD5(r);
             DBG.printf("hmac:%s\r\n",hmac.c_str());
             
-            //set connData
+            //check proxy
             if(useProxy) {
-                for(int i=0; i<sizeof(s); i++) {
-                    s[i]=0; }
-                sprintf(s,"http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str());
-                connData = s;
+                sprintf(r,"http://%s:%d/emma/api/controller/verify",REST_SERVER_HOST,REST_SERVER_PORT);
             } else {
-                for(int i=0; i<sizeof(s); i++) {
-                    s[i]=0; }
-                sprintf(s,"/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
-                connData = s;
+                sprintf(r,"/emma/api/controller/verify");
             }
                 
             //verify registration
             while(!emmaRegistered && loop < 12){
-                rest.get(connData.c_str());
-                for(int i=0; i<sizeof(s); i++) {
-                    s[i]=0; }
+                sprintf(s,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
+                rest.post(r,s);
                 rest.getResponse(s,sizeof(s));
                 DBG.printf("rsp vrf:%s\r\n",s);
                 TFT.locate(0,40);
@@ -1145,7 +1148,7 @@
         //ADE.CVAhLSB = 0;
         
         //init rest to server
-        if(rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) {
+        if(rest.begin(REST_SERVER_HOST,REST_SERVER_PORT,false)) {
             DBG.printf("rest to server is created\r\n");
             TFT.locate(0,40);
             TFT.printf("                                        ");
@@ -1740,7 +1743,7 @@
     if(useProxy) {
         for(int i=0; i<sizeof(s); i++) {
             s[i]=0; }
-        //sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str());
+        //sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",REST_SERVER_HOST,REST_SERVER_PORT,emmaUID.c_str(),hmac.c_str());
         sprintf(s,"http://192.168.128.69/emmaController/firmware/firmwareParameter");
         connData = s;
     } else {
@@ -2399,10 +2402,10 @@
             sscanf(proxyPORT.c_str(),"%d",&connPort);
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
+            sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST);
         } else {
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
+            connHost = REST_SERVER_HOST;
+            connPort = REST_SERVER_PORT;
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
             sprintf(s,"/emma/api/controller/test");
@@ -2437,10 +2440,10 @@
             sscanf(proxyPORT.c_str(),"%d",&connPort);
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"GET http://%s:%d/emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
+            sprintf(s,"GET http://%s:%d/emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST);
         } else {
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
+            connHost = REST_SERVER_HOST;
+            connPort = REST_SERVER_PORT;
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
             strcpy(s,"GET /emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n");
@@ -2488,10 +2491,10 @@
             sscanf(proxyPORT.c_str(),"%d",&connPort);
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"GET http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
+            sprintf(s,"GET http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST);
         } else {
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
+            connHost = REST_SERVER_HOST;
+            connPort = REST_SERVER_PORT;
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
             strcpy(s,"GET /emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n");
@@ -2531,7 +2534,7 @@
     while(!esp.ready());
     
     //rest begin
-    if(!rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) {
+    if(!rest.begin(REST_SERVER_HOST,REST_SERVER_PORT,false)) {
         DBG.printf("EMMA: fail to setup rest\r\n");
         TFT.locate(0,20);
         TFT.printf("EMMA: fail to setup rest");
@@ -2564,10 +2567,10 @@
             sscanf(proxyPORT.c_str(),"%d",&connPort);
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
+            sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST);
         } else {
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
+            connHost = REST_SERVER_HOST;
+            connPort = REST_SERVER_PORT;
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
             sprintf(s,"/emma/api/controller/test");