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
Diff: emmaCode.cpp
- Revision:
- 47:97eadcb478b2
- Parent:
- 46:86a0c2133130
- Child:
- 48:053e92b6dc1e
--- a/emmaCode.cpp Wed Aug 26 12:30:52 2015 +0000 +++ b/emmaCode.cpp Fri Aug 28 04:17:31 2015 +0000 @@ -89,6 +89,14 @@ float AWatt, BWatt, CWatt; float XWattHr,XVrms,XWatt; +//alert variables +bool allowAlertAVrms = true; +bool allowAlertAWatt = true; +bool allowAlertBVrms = true; +bool allowAlertBWatt = true; +bool allowAlertCVrms = true; +bool allowAlertCWatt = true; + //variables bool ethAvailable = false; bool wifiAvailable = false; @@ -455,6 +463,8 @@ if(ethAvailable) { DBG.printf("emmaModeSettings - eth\r\n"); + eth.init(); + eth.connect(); wait(2); TFT.locate(0,0); TFT.printf(" "); @@ -1031,6 +1041,7 @@ string str; time_t seconds; Timer t; + Timer tAlert; Timer tPanelEnergy; Timer tPanel; Timer tNodes; @@ -1141,12 +1152,24 @@ //create thread osThreadCreate(osThread(energyThread),NULL); + tAlert.start(); tPanelEnergy.start(); tPanel.start(); tNodes.start(); wait(1); while(1) { - //checkVoltagePower(); //need revision to support ethernet + checkVoltagePower(); //need revision to support ethernet + + //set allowAlertXxxx to enable controller send alert + if(tAlert.read() > 300.0f) { //300.0f is 5 minutes + allowAlertAVrms = true; + allowAlertAWatt = true; + allowAlertBVrms = true; + allowAlertBWatt = true; + allowAlertCVrms = true; + allowAlertCWatt = true; + tAlert.reset(); + } //panelEnergy, panelVoltage, and panelPower if(tPanelEnergy.read() > 30.0f) { @@ -1755,6 +1778,7 @@ //create thread osThreadCreate(osThread(energyThread),NULL); + tAlert.start(); tPanelEnergy.start(); tPanel.start(); tNodes.start(); @@ -1785,6 +1809,17 @@ espDHCPClientStart = false; } + //set allowAlertXxxx to enable controller send alert + if(tAlert.read() > 300.0f) { //300.0f is 5 minutes + allowAlertAVrms = true; + allowAlertAWatt = true; + allowAlertBVrms = true; + allowAlertBWatt = true; + allowAlertCVrms = true; + allowAlertCWatt = true; + tAlert.reset(); + } + //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); @@ -1887,7 +1922,7 @@ //panel environment checkRxBuffer(); - if(tPanel.read() > 900.0f) { //900 is 15 minutes + if(tPanel.read() > 900.0f) { //900.0f is 15 minutes int dTemp=0; int dHum=0; int dGas=0; @@ -2713,106 +2748,365 @@ DBG.printf("energyThread-finish\r\n"); } } + void checkVoltagePower(void) { //check if voltage or power violates threshold + char connBody[256]; char p[64]; char q[32]; char s[256]; + int connBodyLen=0; + int connPort=0; + int alertType=0; string hmacTime; string str; time_t seconds; //DBG.printf("checkVoltagePower-start\r\n"); - //vrms and irms might be placed inside energy calculation routine - /* - AVrms = ADE.VRMS(PHASE_A) * 0.000128f; //0.000158; //constants are from calculateVRMS function - BVrms = ADE.VRMS(PHASE_B) * 0.000127f; //0.000157; - CVrms = ADE.VRMS(PHASE_C) * 0.000126f; //0.000156; - - AIrms = ADE.IRMS(PHASE_A) * 0.0000125f; //constants are from calculateIRMS function - BIrms = ADE.IRMS(PHASE_B) * 0.0000123f; - CIrms = ADE.IRMS(PHASE_C) * 0.0000124f; - - AWatt = AVrms * AIrms; - BWatt = BVrms * BIrms; - CWatt = CVrms * CIrms; - */ - - //DBG.printf("Vrms of each phase:%.2f - %.2f - %.2f\r\n", AVrms, BVrms, CVrms); - //DBG.printf("Watt of each phase:%.2f - %.2f - %.2f\r\n", AWatt, BWatt, CWatt); - //wait(1); + //vrms and irms should be placed inside energy calculation routine //get time 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); - if(AVrms > VRMSTHRESHOLD || AWatt > WATTTHRESHOLD) { - DBG.printf("alert on ch1\r\n"); - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmacTime.c_str(),q,AVrms,AWatt); + //read connPort + sscanf(restPORT.c_str(),"%d",&connPort); + + if(vrmsTHL > AVrms) { + alertType = 1; + } else if (AVrms > vrmsTHH) { + alertType = 2; + } + + connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}", + emmaUID.c_str(),hmacTime.c_str(),q,AVrms,alertType); + + if(alertType != 0 && ethConnected && allowAlertAVrms) { + 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); + allowAlertAVrms = false; + DBG.printf("[ph1]vrms:%s\r\n",str.c_str()); + } else if(alertType != 0 && wifiConnected && allowAlertAVrms) { + sprintf(p,"/%s/api/controller/alert/1",appNAME.c_str()); + //DBG.printf("[ph1]:%s\r\n",connBody); + rxBuf.clear(); + rest.post(p,connBody); + wait(2); + str = rxBuf; + if(str.rfind("/alert/1") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.rfind("/alert/1")); + if(str.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("[ph1]vrms success\r\n"); + } else { + DBG.printf("[ph1]vrms failed\r\n"); + } + } + allowAlertAVrms = false; + } + + alertType = 0; + if(wattTHL > AWatt) { + alertType = 3; + } else if (AWatt > wattTHH) { + alertType = 4; + } + 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); + allowAlertAWatt = false; + DBG.printf("[ph1]watt:%s\r\n",str.c_str()); + } else if(alertType != 0 && wifiConnected && allowAlertAWatt) { sprintf(p,"/%s/api/controller/alert/1",appNAME.c_str()); - rest.post(p,s); - //rest.post("/emma/api/controller/alert/1",s); //working + //DBG.printf("[ph1]:%s\r\n",connBody); + + rxBuf.clear(); + rest.post(p,connBody); wait(2); str = rxBuf; if(str.rfind("/alert/1") != std::string::npos) { str.erase(str.begin(),str.begin()+str.rfind("/alert/1")); if(str.find("\"status\":\"success\"") != std::string::npos) { - DBG.printf("send alert ch1 success\r\n"); + DBG.printf("[ph1]watt success\r\n"); } else { - DBG.printf("send alert ch1 failed\r\n"); + DBG.printf("[ph1]watt failed\r\n"); } } + allowAlertAWatt = false; } - if(BVrms > VRMSTHRESHOLD || BWatt > WATTTHRESHOLD) { - DBG.printf("alert on ch2\r\n"); - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmacTime.c_str(),q,BVrms,BWatt); + alertType = 0; + if(vrmsTHL > BVrms) { + alertType = 1; + } else if (BVrms > vrmsTHH) { + alertType = 2; + } + 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); + allowAlertBVrms = false; + DBG.printf("[ph2]vrms:%s\r\n",str.c_str()); + } else if(alertType != 0 && wifiConnected && allowAlertBVrms) { sprintf(p,"/%s/api/controller/alert/2",appNAME.c_str()); - rest.post(p,s); - //rest.post("/emma/api/controller/alert/2",s); + //DBG.printf("[ph2]:%s\r\n",connBody); + + rxBuf.clear(); + rest.post(p,connBody); + wait(2); + str = rxBuf; + if(str.rfind("/alert/2") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.rfind("/alert/2")); + if(str.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("[ph2]vrms success\r\n"); + } else { + DBG.printf("[ph2]vrms failed\r\n"); + } + } + allowAlertBVrms = false; + } + + alertType = 0; + if(wattTHL > BWatt) { + alertType = 3; + } else if (BWatt > wattTHH) { + alertType = 4; + } + 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); + allowAlertBWatt = false; + DBG.printf("[ph2]watt:%s\r\n",str.c_str()); + } else if(alertType != 0 && wifiConnected && allowAlertBWatt) { + sprintf(p,"/%s/api/controller/alert/2",appNAME.c_str()); + //DBG.printf("[ph2]:%s\r\n",connBody); + + rxBuf.clear(); + rest.post(p,connBody); wait(2); str = rxBuf; if(str.rfind("/alert/2") != std::string::npos) { str.erase(str.begin(),str.begin()+str.rfind("/alert/2")); if(str.find("\"status\":\"success\"") != std::string::npos) { - DBG.printf("send alert ch2 success\r\n"); + DBG.printf("[ph2]watt success\r\n"); } else { - DBG.printf("send alert ch2 failed\r\n"); + DBG.printf("[ph2]watt failed\r\n"); } } + allowAlertBWatt = false; } - if(CVrms > VRMSTHRESHOLD || CWatt > WATTTHRESHOLD) { - DBG.printf("alert on ch3\r\n"); - DBG.printf("alert on ch3\r\n"); - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmacTime.c_str(),q,CVrms,CWatt); + alertType = 0; + if(vrmsTHL > CVrms) { + alertType = 1; + } else if (CVrms > vrmsTHH) { + alertType = 2; + } + 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); + allowAlertCVrms = false; + DBG.printf("[ph3]vrms:%s\r\n",str.c_str()); + } else if(alertType != 0 && wifiConnected && allowAlertCVrms) { sprintf(p,"/%s/api/controller/alert/3",appNAME.c_str()); - rest.post(p,s); - //rest.post("/emma/api/controller/alert/3",s); + //DBG.printf("[ph3]:%s\r\n",connBody); + + rxBuf.clear(); + rest.post(p,connBody); + wait(2); + str = rxBuf; + if(str.rfind("/alert/3") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.rfind("/alert/3")); + if(str.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("[ph3]vrms success\r\n"); + } else { + DBG.printf("[ph3]vrms failed\r\n"); + } + } + allowAlertCVrms = false; + } + + alertType = 0; + if(wattTHL > CWatt) { + alertType = 3; + } else if (CWatt > wattTHH) { + alertType = 4; + } + 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); + allowAlertCWatt = false; + DBG.printf("[ph3]watt:%s\r\n",str.c_str()); + } else if(alertType != 0 && wifiConnected && allowAlertCWatt) { + sprintf(p,"/%s/api/controller/alert/3",appNAME.c_str()); + //DBG.printf("[ph3]:%s\r\n",connBody); + + rxBuf.clear(); + rest.post(p,connBody); wait(2); str = rxBuf; if(str.rfind("/alert/3") != std::string::npos) { str.erase(str.begin(),str.begin()+str.rfind("/alert/3")); if(str.find("\"status\":\"success\"") != std::string::npos) { - DBG.printf("send alert ch3 success\r\n"); + DBG.printf("[ph3]watt success\r\n"); } else { - DBG.printf("send alert ch3 failed\r\n"); + DBG.printf("[ph3]watt failed\r\n"); } } + allowAlertCWatt = false; } //DBG.printf("checkVoltagePower-finish\r\n"); } + +/* +void checkVoltagePower(void) { + //check if voltage or power violates threshold + char connBody[256]; + char p[64]; + char q[32]; + char s[256]; + int connBodyLen=0; + int connPort=0; + string hmacTime; + string str; + time_t seconds; + + //DBG.printf("checkVoltagePower-start\r\n"); + + //vrms and irms should be placed inside energy calculation routine + + //get time + 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); + + if(AVrms > vrmsTHH || AWatt > wattTHH) { + DBG.printf("alert on ch1\r\n"); + + connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmacTime.c_str(),q,AVrms,AWatt); + 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); + DBG.printf("ch1req:%s\r\n",s); + sscanf(restPORT.c_str(),"%d",&connPort); + str.clear(); + str = ethREST(restSERVER,connPort,s); + DBG.printf("ch1:%s\r\n",str.c_str()); + + + //sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + //emmaUID.c_str(),hmacTime.c_str(),q,AVrms,AWatt); + + //sprintf(p,"/%s/api/controller/alert/1",appNAME.c_str()); + //DBG.printf("ch1:%s\r\n",s); + + //rest.post(p,s); + //wait(2); + //str = rxBuf; + //if(str.rfind("/alert/1") != std::string::npos) { + // str.erase(str.begin(),str.begin()+str.rfind("/alert/1")); + // if(str.find("\"status\":\"success\"") != std::string::npos) { + // DBG.printf("send alert ch1 success\r\n"); + // } else { + // DBG.printf("send alert ch1 failed\r\n"); + // } + //} + + + } + + if(BVrms > vrmsTHH || BWatt > wattTHH) { + DBG.printf("alert on ch2\r\n"); + + connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmacTime.c_str(),q,BVrms,BWatt); + 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); + DBG.printf("ch2req:%s\r\n",s); + sscanf(restPORT.c_str(),"%d",&connPort); + str.clear(); + str = ethREST(restSERVER,connPort,s); + DBG.printf("ch2:%s\r\n",str.c_str()); + + + //sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + //emmaUID.c_str(),hmacTime.c_str(),q,BVrms,BWatt); + + //sprintf(p,"/%s/api/controller/alert/2",appNAME.c_str()); + //DBG.printf("ch2:%s\r\n",s); + + //rest.post(p,s); + //wait(2); + //str = rxBuf; + //if(str.rfind("/alert/2") != std::string::npos) { + // str.erase(str.begin(),str.begin()+str.rfind("/alert/2")); + // if(str.find("\"status\":\"success\"") != std::string::npos) { + // DBG.printf("send alert ch2 success\r\n"); + // } else { + // DBG.printf("send alert ch2 failed\r\n"); + // } + //} + + + } + + if(CVrms > vrmsTHH || CWatt > wattTHH) { + DBG.printf("alert on ch3\r\n"); + + connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmacTime.c_str(),q,CVrms,CWatt); + 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); + DBG.printf("ch3req:%s\r\n",s); + sscanf(restPORT.c_str(),"%d",&connPort); + str.clear(); + str = ethREST(restSERVER,connPort,s); + DBG.printf("ch3:%s\r\n",str.c_str()); + + + //sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + //emmaUID.c_str(),hmacTime.c_str(),q,CVrms,CWatt); + + //sprintf(p,"/%s/api/controller/alert/3",appNAME.c_str()); + //DBG.printf("ch3:%s\r\n",s); + + //rest.post(p,s); + //wait(2); + //str = rxBuf; + //if(str.rfind("/alert/3") != std::string::npos) { + // str.erase(str.begin(),str.begin()+str.rfind("/alert/3")); + // if(str.find("\"status\":\"success\"") != std::string::npos) { + // DBG.printf("send alert ch3 success\r\n"); + // } else { + // DBG.printf("send alert ch3 failed\r\n"); + // } + //} + + + } + //DBG.printf("checkVoltagePower-finish\r\n"); +} +*/ /*end energy related*/ /*start wifi mqtt*/