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:
62:03ba6e82c290
Parent:
61:4eda981730ae
Child:
63:5af876b95f10
--- a/emmaCode.cpp	Sat Sep 26 09:43:09 2015 +0000
+++ b/emmaCode.cpp	Sat Sep 26 13:06:47 2015 +0000
@@ -951,10 +951,10 @@
     if(ethConnected) {
         DBG.printf("emmaModeRegister - eth\r\n");
         wait(2);
-        TFT.locate(0,0);
-        TFT.printf("                                             ");
-        TFT.locate(0,0);
-        TFT.printf(" emmaModeRegister");
+        //TFT.locate(0,0);
+        //TFT.printf("                                             ");
+        //TFT.locate(0,0);
+        //TFT.printf(" emmaModeRegister");
         
         //set connBody, connHost, connPort, connData
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
@@ -1019,8 +1019,16 @@
                         regKey = val;
                         DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
                         
-                        TFT.locate(0,20);
-                        TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
+                        //TFT.locate(0,20);
+                        //TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
+                        TFT.cls();
+                        TFT.locate(5,5);
+                        TFT.set_font((unsigned char*) Lato19x19);
+                        TFT.printf(" Input kode & tunggu ya...");
+                        TFT.locate(100,100);
+                        TFT.set_font((unsigned char*) LatoBlack39x38);
+                        TFT.printf("%s",regKey.c_str());
+                        TFT.fillarc(165,120,90,10,0,360,Green);
                         
                         emmaGetRegKey = true;
                     }
@@ -1029,8 +1037,8 @@
         }
         
         //calculate hmac
-        for(int i=0; i<sizeof(r); i++) {
-            r[i]=0; }
+        //for(int i=0; i<sizeof(r); i++) {
+        //    r[i]=0; }
         sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
         hmac = calculateMD5(r);
         DBG.printf("hmac:%s\r\n",hmac.c_str());
@@ -1054,15 +1062,17 @@
         }
         
         //verify registration
+        TFT.set_font((unsigned char*) Lato19x19);
         while(!emmaRegistered && loop < 12){
             str.clear();
             str = ethREST(connHost,connPort,connData);
             DBG.printf("rsp vrf:%s\r\n",str.c_str());
             
-            TFT.locate(0,40);
-            TFT.printf("                              ");
-            TFT.locate(0,40);
-            TFT.printf(" wait:%d\r\n",loop);
+            //TFT.locate(0,200);
+            //TFT.printf("                              ");
+            //TFT.locate(10,200);
+            //TFT.printf(" wait:%d\r\n",loop);
+            TFT.fillarc(165,120,90,10,0,(loop*30)+30,colorDarkGray);
                 
             //check verification
             if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
@@ -1074,9 +1084,8 @@
                 if(jsonValue.hasMember("user")) {
                     string val = jsonValue["user"].get<std::string>();
                     DBG.printf("%s is registered\r\n",val.c_str());
-                    TFT.locate(0,40);
-                    TFT.printf("                              ");
-                    TFT.locate(0,40);
+                    TFT.cls();
+                    TFT.locate(5,5);
                     TFT.printf(" %s is registered\r\n",val.c_str());
                     emmaRegistered = true;
                 }
@@ -1086,14 +1095,21 @@
         }
             
         //check whether registration success
+        TFT.cls();
         if(emmaRegistered) {
             DBG.printf("registration successful\r\n");
-            TFT.locate(0,60);
-            TFT.printf(" registration successful. please restart.\r\n");
+            lcdDrawSmile();
+            TFT.locate(0,200);
+            TFT.printf("                              ");
+            TFT.locate(10,200);
+            TFT.printf("Sukses! Tolong restart EMMA");
         } else {
             DBG.printf("registration unsuccessful\r\n");
-            TFT.locate(0,60);
-            TFT.printf(" registration unsuccessful. please restart.\r\n");
+            lcdDrawFrown();
+            TFT.locate(0,200);
+            TFT.printf("                              ");
+            TFT.locate(10,200);
+            TFT.printf("Gagal! Tolong restart EMMA");
         }
         while(1);
         
@@ -1311,8 +1327,16 @@
                         regKey = val;
                         DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
                             
-                        TFT.locate(0,20);
-                        TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
+                        //TFT.locate(0,20);
+                        //TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
+                        TFT.cls();
+                        TFT.locate(5,5);
+                        TFT.set_font((unsigned char*) Lato19x19);
+                        TFT.printf(" Input kode & tunggu ya...");
+                        TFT.locate(100,100);
+                        TFT.set_font((unsigned char*) LatoBlack39x38);
+                        TFT.printf("%s",regKey.c_str());
+                        TFT.fillarc(165,120,90,10,0,360,Green);
                             
                         emmaGetRegKey = true;
                     }
@@ -1327,15 +1351,17 @@
         DBG.printf("hmac:%s\r\n",hmac.c_str());
         
         //verify registration
+        TFT.set_font((unsigned char*) Lato19x19);
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
         sprintf(s,"POST /%s/api/controller/verify 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(!emmaRegistered && loop < 12) {
             str = gprsREST(restSERVER,connPort,s);
             DBG.printf("rsp vrf:%s\r\n",str.c_str());
-            TFT.locate(0,40);
-            TFT.printf("                              ");
-            TFT.locate(0,40);
-            TFT.printf(" wait:%d\r\n",loop);
+            //TFT.locate(0,200);
+            //TFT.printf("                              ");
+            //TFT.locate(10,200);
+            //TFT.printf(" wait:%d\r\n",loop);
+            TFT.fillarc(165,120,90,10,0,(loop*30)+30,colorDarkGray);
             
             //check verification
             if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
@@ -1347,9 +1373,8 @@
                 if(jsonValue.hasMember("user")) {
                     string val = jsonValue["user"].get<std::string>();
                     DBG.printf(" %s is registered\r\n",val.c_str());
-                    TFT.locate(0,40);
-                    TFT.printf("                              ");
-                    TFT.locate(0,40);
+                    TFT.cls();
+                    TFT.locate(5,5);
                     TFT.printf(" %s is registered\r\n",val.c_str());
                     emmaRegistered = true;
                 }
@@ -1359,21 +1384,34 @@
         }
         
         //check whether registration success
+        TFT.cls();
         if(emmaRegistered) {
             DBG.printf("registration successful\r\n");
-            TFT.locate(0,60);
-            TFT.printf(" registration successful. please restart.\r\n");
+            lcdDrawSmile();
+            TFT.locate(0,200);
+            TFT.printf("                              ");
+            TFT.locate(10,200);
+            TFT.printf("Sukses! Tolong restart EMMA");
         } else {
             DBG.printf("registration unsuccessful\r\n");
-            TFT.locate(0,60);
-            TFT.printf(" registration unsuccessful. please restart.\r\n");
+            lcdDrawFrown();
+            TFT.locate(0,200);
+            TFT.printf("                              ");
+            TFT.locate(10,200);
+            TFT.printf("Gagal! Tolong restart EMMA");
         }
         while(1);
         
     } else {
         DBG.printf("no eth, wifi, or gprs is connected\r\n");
-        TFT.locate(0,60);
-        TFT.printf(" no iface connected. please restart.\r\n");
+        //TFT.locate(0,60);
+        //TFT.printf(" no iface connected. please restart.\r\n");
+        TFT.cls();
+        lcdDrawFrown();
+        TFT.locate(10,170);
+        TFT.printf("Tidak ada interface");
+        TFT.locate(10,200);
+        TFT.printf("Tolong restart EMMA");
     }
 }
 
@@ -4187,81 +4225,6 @@
     } else {
         return gprsRxBuf;    
     }
-    
-    
-    /*
-    while(1) {
-        sim900.printf("AT+CIPMUX=1\r\n");
-        gprsRcvReply(r,3000);
-        //DBG.printf("rspn[CIPMUX]:%s\r\n",r);
-        DBG.printf("AT+CIPMUX\r\n");
-        str = r;
-        if(str.find("OK") != std::string::npos) {
-            break;    
-        }
-        if(n>=3) {
-            return "ERROR";    
-        }
-        n++;
-    }
-    
-    if(!APN.compare("Telkomsel")) {
-        strcpy(s,"AT+CSTT=\"telkomsel\",\"wap\",\"wap123\"\r\n");
-    } else if(!APN.compare("Indosat")) {
-        strcpy(s,"AT+CSTT=\"indosatgprs\",\"indosat\",\"indosat\"\r\n");
-    } else if(!APN.compare("XL")) {
-        strcpy(s,"AT+CSTT=\"www.xlgprs.net\",\"xlgprs\",\"proxl\"\r\n");
-    } else if(!APN.compare("3")) {
-        strcpy(s,"AT+CSTT=\"3gprs\",\"3gprs\",\"3gprs\"\r\n");
-    } else {
-        return "ERROR";    
-    }
-    
-    n=0;
-    while(1) {
-        sim900.printf("%s",s);
-        gprsRcvReply(r,5000);
-        //DBG.printf("rspn[CSTT]:%s\r\n",r);
-        DBG.printf("AT+CSTT\r\n");
-        str = r;
-        if(str.find("OK") != std::string::npos) {
-            break;    
-        }
-        if(n>=3) {
-            return "ERROR";    
-        }
-        n++;
-    }
-    
-    n=0;
-    while(1) {
-        sim900.printf("AT+CIICR\r\n");
-        gprsRcvReply(r,3000);
-        //DBG.printf("rspn[CIICR]:%s\r\n",r);
-        DBG.printf("AT+CIICR\r\n");
-        str = r;
-        if(str.find("OK") != std::string::npos) {
-            break;    
-        }
-        if(n>=3) {
-            return "ERROR";    
-        }
-        n++;
-    }
-    
-    while(1) {
-        sim900.printf("AT+CIFSR\r\n");
-        gprsRcvReply(r,3000);
-        //DBG.printf("rspn[CIFSR]:%s\r\n",r);
-        DBG.printf("AT+CIFSR\r\n");
-        str = r;
-        if(str.find("ERROR") != std::string::npos) {
-            return "ERROR";    
-        } else {
-            return str;    
-        }
-    }
-    */
 }
 string gprsREST(string host, int port, string data) {
     char r[128];
@@ -4279,7 +4242,7 @@
         if(str.find("CONNECT OK") != std::string::npos) {
             break;    
         }
-        if(n>5) {
+        if(n>=3) {
             return "ERROR:CIPSTART"; //unable to start connection    
         }
         n++;
@@ -4319,7 +4282,7 @@
         if(str.find("CLOSE OK") != std::string::npos) {
             break;    
         }
-        if(n>3) {
+        if(n>=3) {
             //return "ERROR:CIPCLOSE"; //unable to close connection
             return rLong;   //assumption if unable to close connection because it already been closed
         }
@@ -4338,7 +4301,6 @@
     
     //AT+CIPSTART
     waitAnimation = true;
-    
     while(waitAnimation) {
         sim900.printf("AT+CIPSTART=1,\"TCP\",\"%s\",%d\r\n",host.c_str(),port);
         for(int i=0; i<2880; i+=4) {
@@ -4347,17 +4309,68 @@
             TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray);
             TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray);
             
-            if(rxBuf.find("CONNECT OK") != std::string::npos) {
+            if(gprsRxBuf.find("CONNECT OK") != std::string::npos) {
                 waitAnimation = false;        
             }
         }
-        if(n>5) {
+        if(n>=3) {
             return "ERROR:CIPSTART"; //unable to start connection
         }
         n++;
     }
     
-    return "CONNECT OK";    //should be deleted
+    //return "CONNECT OK";    //should be deleted
+    
+    //AT+CIPSEND
+    gprsRxBuf.clear();
+    sim900.printf("AT+CIPSEND=1,%d\r\n",data.length());
+    for(int i=0; i<960; i+=4) {     //960 is ~3sec
+        TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray);
+        TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue);
+        TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray);
+        TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray);
+    }
+    if(gprsRxBuf.find(">") != std::string::npos) {
+        gprsRxBuf.clear();
+        sim900.printf("%s",data.c_str());
+        for(int i=0; i<2880; i+=4) {     //2880 is ~10sec
+            TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray);
+            TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue);
+            TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray);
+            TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray);
+        }
+    } else {
+        return "ERROR:>";   //unable to send data
+    }
+    
+    //close connection (case for get request)
+    if(gprsRxBuf.find("CLOSED") != std::string::npos) {
+        return gprsRxBuf;
+    }
+    
+    //close connection
+    waitAnimation = true;
+    n=0;
+    gprsRxBuf.clear();
+    while(waitAnimation) {
+        sim900.printf("AT+CIPCLOSE=1,1\r\n");
+        for(int i=0; i<960; i+=4) {     //960 is ~3sec
+            TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray);
+            TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue);
+            TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray);
+            TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray);
+
+            if(gprsRxBuf.find("CLOSE OK") != std::string::npos) {
+                waitAnimation = false;        
+            }
+        }
+        if(n>=3) {
+            //return "ERROR:CIPCLOSE"; //unable to close connection
+            return gprsRxBuf;   //assumption if unable to close connection because it already been closed
+        }
+        n++;
+    }
+    return gprsRxBuf;
     
     /*
     while(1) {
@@ -4650,9 +4663,9 @@
 }
 
 void lcdDrawFrown(void) {
-    TFT.fillarc(120, 40, 20, 10, 110, 250, Red);    //left eye
-    TFT.fillarc(200, 40, 20, 10, 110, 250, Red);    //right eye
-    TFT.fillarc(160, 200, 85, 10, 315, 45, Red);     //mouth
+    TFT.fillarc(120, 40, 20, 10, 110, 250, colorDarkGray);    //left eye
+    TFT.fillarc(200, 40, 20, 10, 110, 250, colorDarkGray);    //right eye
+    TFT.fillarc(160, 200, 85, 10, 315, 45, colorDarkGray);     //mouth
 }
 
 //void lcdPrintRestart(void) {
@@ -4815,7 +4828,8 @@
             //test connection
             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);
+            //str = gprsREST(restSERVER,connPort,cmd);
+            str = gprsRESTAnimate(restSERVER,connPort,cmd);
             DBG.printf("str:%s\r\n",str.c_str());
             if(str.find("\"status\":\"OK\"") != std::string::npos) {
                 gprsConnected = true;