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:
- 22:e18d361bf688
- Parent:
- 21:33bd8b82560f
- Child:
- 23:fb369e171b7d
--- a/emmaCode.cpp Thu Jul 23 04:22:55 2015 +0000 +++ b/emmaCode.cpp Thu Jul 23 05:21:10 2015 +0000 @@ -93,6 +93,7 @@ bool gprsConnected = false; bool useProxy = false; bool newCommand = false; +bool newEnergyData = false; string globalCommand; string rxBuf; @@ -884,12 +885,14 @@ void emmaModeOperation(void) { char mqttClientId[32]; + char p[64]; char q[32]; char r[32]; char s[512]; int loop=0; int trial=0; //string hmac; + string hmacTime; string str; time_t seconds; Timer t; @@ -1207,56 +1210,68 @@ TFT.locate(0,100); TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt); - //for(int i=1; i<4; i++) { - for(int i=1; i<2; i++) { - DBG.printf("sending channel: %d\r\n",i); - if(i==1){ - XWattHr = AWattHr; - XVrms = AVrms; - XWatt = AWatt; - } else if(i==2) { - XWattHr = BWattHr; - XVrms = BVrms; - XWatt = BWatt; - } else { - XWattHr = CWattHr; - XVrms = CVrms; - XWatt = CWatt; + if(newEnergyData) { + //for(int i=1; i<4; i++) { + for(int i=1; i<2; i++) { + DBG.printf("sending channel: %d\r\n",i); + if(i==1){ + XWattHr = AWattHr; + XVrms = AVrms; + XWatt = AWatt; + } else if(i==2) { + XWattHr = BWattHr; + XVrms = BVrms; + XWatt = BWatt; + } else { + XWattHr = CWattHr; + XVrms = CVrms; + XWatt = CWatt; + } + sprintf(r,"/emma/api/controller/energy/%d",i); + seconds = time(NULL); + //for(int j=0; j<sizeof(q); j++) { + // q[j]=0; } + 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); + + sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}", + emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt); + //DBG.printf("dataEnergy:\r\n%s\r\n",s); + rest.post(r,s); + wait(2); + if(rxBuf.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("send channel: %d success\r\n",i); + TFT.foreground(Green); + TFT.locate(0,120); + TFT.printf(" "); + TFT.locate(0,120); + TFT.printf("send ch%d success",i); + wait(1); + TFT.locate(0,120); + TFT.printf(" "); + TFT.foreground(White); + } else { + DBG.printf("send channel: %d failed\r\n",i); + TFT.foreground(Red); + TFT.locate(0,120); + TFT.printf(" "); + TFT.locate(0,120); + TFT.printf("send ch%d failed",i); + wait(1); + TFT.locate(0,120); + TFT.printf(" "); + TFT.foreground(White); + } } - sprintf(r,"/emma/api/controller/energy/%d",i); - seconds = time(NULL); - for(int j=0; j<sizeof(q); j++) { - q[j]=0; } - strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}", - emmaUID.c_str(),hmac.c_str(),q,XWattHr,XVrms,XWatt); - //DBG.printf("dataEnergy:\r\n%s\r\n",s); - rest.post(r,s); - wait(2); - if(rxBuf.find("\"status\":\"success\"") != std::string::npos) { - DBG.printf("send channel: %d success\r\n",i); - TFT.foreground(Green); - TFT.locate(0,120); - TFT.printf(" "); - TFT.locate(0,120); - TFT.printf("send ch%d success",i); - wait(1); - TFT.locate(0,120); - TFT.printf(" "); - TFT.foreground(White); - } else { - DBG.printf("send channel: %d failed\r\n",i); - TFT.foreground(Red); - TFT.locate(0,120); - TFT.printf(" "); - TFT.locate(0,120); - TFT.printf("send ch%d failed",i); - wait(1); - TFT.locate(0,120); - TFT.printf(" "); - TFT.foreground(White); - } + + newEnergyData = false; } + t.reset(); loop++; } @@ -1288,8 +1303,15 @@ //send environment sensor 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); + sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}", - emmaUID.c_str(),hmac.c_str(),q,dTemp,dHum,dGas); + emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas); DBG.printf("dataEnvironment:\r\n%s\r\n",s); rest.post("/emma/api/controller/environment",s); wait(2); @@ -1334,11 +1356,18 @@ //send node's temp seconds = time(NULL); - for(int j=0; j<sizeof(q); j++) { - q[j]=0; } + //for(int j=0; j<sizeof(q); j++) { + // q[j]=0; } 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); + 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()); + emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str()); //DBG.printf("dataNodeTemp:\r\n%s\r\n",s); rest.post("/emma/api/controller/nodetemp",s); wait(2); @@ -1725,6 +1754,8 @@ BWattHr = BWattHrSum * 0.0000197f; CWattHr = CWattHrSum * 0.0000196f; + newEnergyData = true; + tEnergy.stop(); tEnergy.reset(); DBG.printf("energyThread-finish\r\n"); @@ -1732,9 +1763,10 @@ } void checkVoltagePower(void) { //check if voltage or power violates threshold + char p[64]; char q[32]; char s[256]; - + string hmacTime; string str; time_t seconds; @@ -1763,10 +1795,16 @@ 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(),hmac.c_str(),q,AVrms,AWatt); + emmaUID.c_str(),hmacTime.c_str(),q,AVrms,AWatt); rest.post("/emma/api/controller/alert/1",s); wait(2); str = rxBuf; @@ -1783,7 +1821,7 @@ 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(),hmac.c_str(),q,BVrms,BWatt); + emmaUID.c_str(),hmacTime.c_str(),q,BVrms,BWatt); rest.post("/emma/api/controller/alert/2",s); wait(2); str = rxBuf; @@ -1801,7 +1839,7 @@ 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(),hmac.c_str(),q,CVrms,CWatt); + emmaUID.c_str(),hmacTime.c_str(),q,CVrms,CWatt); rest.post("/emma/api/controller/alert/3",s); wait(2); str = rxBuf;