able to subscribe for >10hrs and still running
Dependencies: ADE7758_v1 Crypto DHT11 MQTT MbedJSONValue SDFileSystem SPI_TFT_ILI9341 SWSPI SetRTC TFT_fonts Touch W5500Interface mbed-rtos mbed-src tuanpm
Fork of PB_emma_controller_mbed_src by
Diff: emmaCode.cpp
- Revision:
- 20:ea14f175bbb4
- Parent:
- 19:7e3e9332f719
- Child:
- 21:33bd8b82560f
diff -r 7e3e9332f719 -r ea14f175bbb4 emmaCode.cpp --- a/emmaCode.cpp Wed Jul 22 11:44:36 2015 +0000 +++ b/emmaCode.cpp Wed Jul 22 13:17:57 2015 +0000 @@ -458,7 +458,7 @@ //set time if(jsonValue.hasMember(parameter[4])) { string epTime = jsonValue[parameter[4]].get<std::string>(); - int seconds; + time_t seconds; sscanf(epTime.c_str(),"%d",&seconds); set_time(seconds); DBG.printf("time is set\r\n"); @@ -1055,12 +1055,12 @@ //set ade7758 parameter ADE.begin(); - ADE.AVRMSCalib = 1526873.00; - ADE.BVRMSCalib = 534202.00; - ADE.CVRMSCalib = 456990.00; - ADE.AIRMSCalib = 39248.00; - ADE.BIRMSCalib = 654.00; - ADE.CIRMSCalib = 111.00; + //ADE.AVRMSCalib = 1526873.00; + //ADE.BVRMSCalib = 534202.00; + //ADE.CVRMSCalib = 456990.00; + //ADE.AIRMSCalib = 39248.00; + //ADE.BIRMSCalib = 654.00; + //ADE.CIRMSCalib = 111.00; ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET); @@ -1072,13 +1072,13 @@ ADE.write16bits(CVAG, 0); //ADE.AWhLSB = 0.000001192;//0.00006025556; - ADE.AWhLSB = 0.00002109; - ADE.BWhLSB = 0.25075167; - ADE.CWhLSB = 0.25075167; + //ADE.AWhLSB = 0.00002109; + //ADE.BWhLSB = 0.25075167; + //ADE.CWhLSB = 0.25075167; - ADE.AVAhLSB = 0.00008370; - ADE.BVAhLSB = 0; - ADE.CVAhLSB = 0; + //ADE.AVAhLSB = 0.00008370; + //ADE.BVAhLSB = 0; + //ADE.CVAhLSB = 0; //init rest to server if(rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) { @@ -1178,9 +1178,10 @@ wait(1); while(1) { checkRxBuffer(); - //checkVoltagePower(); + checkVoltagePower(); + //panelEnergy, panelVoltage, and panelPower - if(t.read() > 300.0f) { + if(t.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(" "); @@ -1284,7 +1285,7 @@ seconds = time(NULL); for(int j=0; j<sizeof(q); j++) { q[j]=0; } - strftime(q, 32, "%Y-%m-%d %H:%M:%S\r\n",localtime(&seconds)); + strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":\"%s\",\"value\":%s}", emmaUID.c_str(),hmac.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str()); //DBG.printf("dataNodeTemp:\r\n%s\r\n",s); @@ -1645,14 +1646,30 @@ BWattHrSum = 0; CWattHrSum = 0; - while(tEnergy.read() < 15*60.0) { + while(tEnergy.read() < 1*60.0) { AWattHrValue = ADE.getWattHR(PHASE_A); BWattHrValue = ADE.getWattHR(PHASE_B); CWattHrValue = ADE.getWattHR(PHASE_C); AWattHrSum += AWattHrValue; BWattHrSum += BWattHrValue; - CWattHrSum += CWattHrValue; + CWattHrSum += CWattHrValue; + + + //start check voltage and power + 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; + //end check voltage and power + } AWattHr = AWattHrSum * 0.0000198f; @@ -1663,41 +1680,19 @@ tEnergy.reset(); DBG.printf("energyThread-finish\r\n"); } - - /* - while(1) { - //DBG.printf("insideEnergyThread\r\n"); - //osDelay(5000); - - //calculate energy - DBG.printf("energyThread-start\r\n"); - float period = 0; - AWattHrSum = 0; - BWattHrSum = 0; - CWattHrSum = 0; - while(period < 15*60.0) { - period += ADE.getAccumulationTime(PHASE_A); - ADE.getAccumulatedEnergy(PHASE_A, &AWattHrValue, &BWattHrValue, &CWattHrValue, &AVAHrValue, &BVAHrValue, &CVAHrValue); - AWattHrSum += AWattHrValue; - BWattHrSum += BWattHrValue; - CWattHrSum += CWattHrValue; - } - AWattHr = AWattHrSum * ADE.AWhLSB; - BWattHr = BWattHrSum * ADE.BWhLSB; - CWattHr = CWattHrSum * ADE.CWhLSB; - - DBG.printf("energyThread-finish\r\n"); - } - */ } void checkVoltagePower() { //check if voltage or power violates threshold + char q[32]; char s[256]; + string str; + time_t seconds; //DBG.printf("checkVoltagePower-start\r\n"); - //vrms dan irms mungkin bisa ditaro di dalam routine energy calculation + //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; @@ -1709,16 +1704,20 @@ 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); - /* + //get time + seconds = time(NULL); + strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); + if(AVrms > VRMSTHRESHOLD || AWatt > WATTTHRESHOLD) { DBG.printf("alert on ch1\r\n"); - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmac.c_str(),AVrms,AWatt); + sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmac.c_str(),q,AVrms,AWatt); rest.post("/emma/api/controller/alert/1",s); wait(2); str = rxBuf; @@ -1734,8 +1733,8 @@ if(BVrms > VRMSTHRESHOLD || BWatt > WATTTHRESHOLD) { DBG.printf("alert on ch2\r\n"); - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmac.c_str(),BVrms,BWatt); + sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmac.c_str(),q,BVrms,BWatt); rest.post("/emma/api/controller/alert/2",s); wait(2); str = rxBuf; @@ -1752,8 +1751,8 @@ 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\":\"2015-06-09 12:32:12\",\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmac.c_str(),CVrms,CWatt); + sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmac.c_str(),q,CVrms,CWatt); rest.post("/emma/api/controller/alert/3",s); wait(2); str = rxBuf; @@ -1766,7 +1765,6 @@ } } } - */ //DBG.printf("checkVoltagePower-finish\r\n"); } /*end energy related*/