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:
63:5af876b95f10
Parent:
62:03ba6e82c290
Child:
64:2409502c3e32
--- a/emmaCode.cpp	Sat Sep 26 13:06:47 2015 +0000
+++ b/emmaCode.cpp	Sat Sep 26 16:52:33 2015 +0000
@@ -33,6 +33,7 @@
 SPI_TFT_ILI9341 TFT(PA_7, PA_6, PA_5, PA_4, PC_5, PC_4,"TFT");  //mosi, miso, sclk, cs, reset, dc
 //init touch screen - without cs pin
 TouchScreenADS7843 TP(PB_10, PB_0, PB_1, PB_2, &TFT);    //mosi, miso, sclk, irq, tft
+InterruptIn tpIRQ(PB_2);
 
 //init dht sensor
 DHT11 d(PD_2);
@@ -127,6 +128,7 @@
 float AIrms, BIrms, CIrms;
 float AWatt, BWatt, CWatt;
 float XWattHr,XVrms,XWatt;
+int panelTemp = 25; //default
 
 //alert variables
 bool allowAlertAVrms = true;
@@ -158,6 +160,7 @@
 uint16_t colorLightGray = TFT.color565(192,192,192);
 uint16_t colorGray = TFT.color565(127,127,127);
 uint16_t colorDarkGray = TFT.color565(64,64,64);
+uint16_t colorLightGreen = TFT.color565(50,255,150);
 
 /*start lcd and touch*/
 int emmaModeSelection(void) {   //circle
@@ -933,7 +936,7 @@
     Timer t;
     
     TFT.locate(0,0);
-    TFT.printf(" please wait...");
+    TFT.printf(" Mohon tunggu...");
     
     //check connected interface
     isEthConnected();
@@ -1437,7 +1440,7 @@
     Timer tNodes;
     
     TFT.locate(0,0);
-    TFT.printf(" please wait");
+    TFT.printf(" Mohon tunggu...");
     
     //check connected interface
     isEthConnected();
@@ -1451,19 +1454,20 @@
     DBG.printf("wifiConnected:%d\r\n",wifiConnected);
     DBG.printf("gprsConnected:%d\r\n",gprsConnected);
     
-    TFT.locate(0,0);
-    TFT.printf(" emmaModeOperation");
+    //TFT.locate(0,0);
+    //TFT.printf(" emmaModeOperation");
     
-    TFT.locate(0,20);
-    TFT.printf(" Interface:");
+    //TFT.locate(0,20);
+    //TFT.printf(" Interface:");
     
-    TFT.locate(75,20);
+    TFT.cls();
+    TFT.locate(0,0);
     if(ethConnected) {
-        TFT.printf("ETH");       
+        TFT.printf(" ETH");       
     } else if(wifiConnected) {
-        TFT.printf("WiFi");
+        TFT.printf(" WiFi");
     } else if(gprsConnected) {
-        TFT.printf("GPRS");    
+        TFT.printf(" GPRS");    
     } else {
         TFT.printf("N/A");    
     }
@@ -1605,22 +1609,36 @@
             //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);
-                TFT.printf("                                                  ");
-                TFT.locate(0,60);
-                TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
+                //TFT.locate(0,60);
+                //TFT.printf("                                                  ");
+                //TFT.locate(0,60);
+                //TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
                 
                 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
-                TFT.locate(0,80);
-                TFT.printf("                                                  ");
-                TFT.locate(0,80);
-                TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
+                //TFT.locate(0,80);
+                //TFT.printf("                                                  ");
+                //TFT.locate(0,80);
+                //TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
                 
                 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
-                TFT.locate(0,100);
-                TFT.printf("                                                  ");
-                TFT.locate(0,100);
-                TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
+                //TFT.locate(0,100);
+                //TFT.printf("                                                  ");
+                //TFT.locate(0,100);
+                //TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
+                
+                //display energy and temp
+                TFT.locate(45,80);
+                TFT.printf("Energy");
+                TFT.fillarc(80, 120, 70, 10, 0, 360, colorLightGreen);
+                TFT.fillarc(80, 120, 70, 10, (loop%12)*30, (loop%12)*30+30, colorDarkGray);
+                TFT.locate(40,110);
+                TFT.printf("%.1f kWh",AWattHr+BWattHr+CWattHr);
+        
+                TFT.locate(210,80);
+                TFT.printf("Temp");
+                TFT.fillarc(240, 120, 70, 10, 0, 360, colorLightGreen);
+                TFT.locate(215,110);
+                TFT.printf("%d C",panelTemp);
                 
                 if(newEnergyData) {
                     //for(int i=1; i<4; i++) {
@@ -1666,12 +1684,12 @@
                             
                                 DBG.printf("send channel: %d success\r\n",i);
                                 TFT.foreground(Green);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
-                                TFT.locate(0,120);
-                                TFT.printf("send ch%d success",i);
+                                TFT.locate(0,200);
+                                TFT.printf(" send ch%d success",i);
                                 wait(1);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);
                             } else {
@@ -1682,12 +1700,12 @@
                                 
                                 DBG.printf("send channel: %d failed\r\n",i);
                                 TFT.foreground(Red);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
-                                TFT.locate(0,120);
-                                TFT.printf("send ch%d failed",i);
+                                TFT.locate(0,200);
+                                TFT.printf(" send ch%d failed",i);
                                 wait(1);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);    
                             }
@@ -1716,6 +1734,7 @@
                     }
                     if(d.readData() == DHT11::OK) {
                         dTemp = d.readTemperature();
+                        panelTemp = dTemp;
                         dHum = d.readHumidity();
                         break;    
                     }
@@ -1830,10 +1849,10 @@
                     str.erase(str.begin()+str.rfind("}]")+2,str.end());
                 }
                 //DBG.printf("newCommand:\r\n%s\r\n",str.c_str());
-                TFT.locate(0,160);
+                TFT.locate(0,200);
                 TFT.printf("                                        ");
-                TFT.locate(0,160);
-                TFT.printf("newCommand");
+                TFT.locate(0,200);
+                TFT.printf(" newCommand");
                 
                 MbedJSONValue jsonValue;
                 parse(jsonValue,str.c_str());
@@ -1896,12 +1915,12 @@
                                         if(trial>=2) {   //two times trial
                                             DBG.printf("cmd is not executed\r\n");
                                             TFT.foreground(Red);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
-                                            TFT.locate(0,180);
-                                            TFT.printf("cmd is not executed");
+                                            TFT.locate(0,200);
+                                            TFT.printf(" cmd is not executed");
                                             wait(1);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
                                             TFT.foreground(White);
                                             break;    
@@ -1911,12 +1930,12 @@
                                         if(str.find("200 OK") != std::string::npos) {
                                             DBG.printf("cmd is executed\r\n");
                                             TFT.foreground(Green);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
-                                            TFT.locate(0,180);
-                                            TFT.printf("cmd is executed");
+                                            TFT.locate(0,200);
+                                            TFT.printf(" cmd is executed");
                                             wait(1);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
                                             TFT.foreground(White);
                                             execResult = "success";
@@ -1958,12 +1977,12 @@
                                 }
                             } else {
                                 TFT.foreground(Red);
-                                TFT.locate(0,180);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
-                                TFT.locate(0,180);
-                                TFT.printf("node is invalid");
+                                TFT.locate(0,200);
+                                TFT.printf(" node is invalid");
                                 wait(1);
-                                TFT.locate(0,180);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);    
                             }
@@ -1971,7 +1990,7 @@
                     }
                 }
                 //clear text on lcd
-                TFT.locate(0,160);
+                TFT.locate(0,200);
                 TFT.printf("                                        ");
             }
             osDelay(5000);   
@@ -2338,22 +2357,36 @@
             //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);
-                TFT.printf("                                                  ");
-                TFT.locate(0,60);
-                TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
+                //TFT.locate(0,60);
+                //TFT.printf("                                                  ");
+                //TFT.locate(0,60);
+                //TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
                 
                 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
-                TFT.locate(0,80);
-                TFT.printf("                                                  ");
-                TFT.locate(0,80);
-                TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
+                //TFT.locate(0,80);
+                //TFT.printf("                                                  ");
+                //TFT.locate(0,80);
+                //TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
                 
                 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
-                TFT.locate(0,100);
-                TFT.printf("                                                  ");
-                TFT.locate(0,100);
-                TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
+                //TFT.locate(0,100);
+                //TFT.printf("                                                  ");
+                //TFT.locate(0,100);
+                //TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
+                
+                //display energy and temp
+                TFT.locate(45,80);
+                TFT.printf("Energy");
+                TFT.fillarc(80, 120, 70, 10, 0, 360, colorLightGreen);
+                TFT.fillarc(80, 120, 70, 10, (loop%12)*30, (loop%12)*30+30, colorDarkGray);
+                TFT.locate(40,110);
+                TFT.printf("%.1f kWh",AWattHr+BWattHr+CWattHr);
+        
+                TFT.locate(210,80);
+                TFT.printf("Temp");
+                TFT.fillarc(240, 120, 70, 10, 0, 360, colorLightGreen);
+                TFT.locate(215,110);
+                TFT.printf("%d C",panelTemp);
                 
                 if(newEnergyData) {
                     //for(int i=1; i<4; i++) {
@@ -2400,12 +2433,12 @@
                             
                                 DBG.printf("send channel: %d success\r\n",i);
                                 TFT.foreground(Green);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
-                                TFT.locate(0,120);
-                                TFT.printf("send ch%d success",i);
+                                TFT.locate(0,200);
+                                TFT.printf(" send ch%d success",i);
                                 wait(1);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);    
                             } else {
@@ -2416,12 +2449,12 @@
                                 
                                 DBG.printf("send channel: %d failed\r\n",i);
                                 TFT.foreground(Red);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
-                                TFT.locate(0,120);
-                                TFT.printf("send ch%d failed",i);
+                                TFT.locate(0,200);
+                                TFT.printf(" send ch%d failed",i);
                                 wait(1);
-                                TFT.locate(0,120);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);    
                             }
@@ -2452,6 +2485,7 @@
                     }
                     if(d.readData() == DHT11::OK) {
                         dTemp = d.readTemperature();
+                        panelTemp = dTemp;
                         dHum = d.readHumidity();
                         break;    
                     }
@@ -2569,10 +2603,10 @@
             checkRxBuffer();
             if(newCommand) {
                 //DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
-                TFT.locate(0,160);
+                TFT.locate(0,200);
                 TFT.printf("                                        ");
-                TFT.locate(0,160);
-                TFT.printf("newCommand");
+                TFT.locate(0,200);
+                TFT.printf(" newCommand");
                 
                 MbedJSONValue jsonValue;
                 parse(jsonValue,globalCommand.c_str());
@@ -2641,10 +2675,10 @@
                                         if(trial>=2) {   //two times trial
                                             DBG.printf("cmd is not executed\r\n");
                                             TFT.foreground(Red);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
-                                            TFT.locate(0,180);
-                                            TFT.printf("cmd is not executed");
+                                            TFT.locate(0,200);
+                                            TFT.printf(" cmd is not executed");
                                             wait(1);
                                             TFT.locate(0,180);
                                             TFT.printf("                                        ");
@@ -2656,12 +2690,12 @@
                                         if(rxBuf.find("REST: status = 200") != std::string::npos) {
                                             DBG.printf("cmd is executed\r\n");
                                             TFT.foreground(Green);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
-                                            TFT.locate(0,180);
-                                            TFT.printf("cmd is executed");
+                                            TFT.locate(0,200);
+                                            TFT.printf(" cmd is executed");
                                             wait(1);
-                                            TFT.locate(0,180);
+                                            TFT.locate(0,200);
                                             TFT.printf("                                        ");
                                             TFT.foreground(White);
                                             execResult = "success";
@@ -2703,12 +2737,12 @@
                                 }
                             } else {
                                 TFT.foreground(Red);
-                                TFT.locate(0,180);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
-                                TFT.locate(0,180);
-                                TFT.printf("node is invalid");
+                                TFT.locate(0,200);
+                                TFT.printf(" node is invalid");
                                 wait(1);
-                                TFT.locate(0,180);
+                                TFT.locate(0,200);
                                 TFT.printf("                                        ");
                                 TFT.foreground(White);    
                             }
@@ -2716,7 +2750,7 @@
                     }
                 }
                 //clear text on lcd
-                TFT.locate(0,160);
+                TFT.locate(0,200);
                 TFT.printf("                                        ");
                 
                 newCommand = false;