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:
- 7:7e8c6ad3fd64
- Parent:
- 6:45f50f1177db
- Child:
- 8:51a0ca9079ca
diff -r 45f50f1177db -r 7e8c6ad3fd64 emmaCode.cpp --- a/emmaCode.cpp Thu Jul 09 03:11:22 2015 +0000 +++ b/emmaCode.cpp Fri Jul 10 05:26:04 2015 +0000 @@ -612,12 +612,6 @@ DBG.printf("ethConnected:%d\r\n",ethConnected); DBG.printf("wifiConnected:%d\r\n",wifiConnected); - //check firmware update - - //execute last state of switches on board - - //get list of nodes from server - //calculate hmac //for(int j=0; j<sizeof(s); j++) { // s[j]=0; } @@ -682,17 +676,62 @@ ADE.CVAhLSB = 0; //init rest to server - if(rest.begin(EMMA_SERVER_HOST,8080,false)) { + if(rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) { DBG.printf("rest to server is created\r\n"); } else { DBG.printf("rest to server is NOT created\r\n"); } + //check firmware update + + //execute last state of switches on board + + //get list of nodes from server + sprintf(s,"/emma/api/controller/remotes?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str()); + rest.get(s); + for(int i=0; i<sizeof(s); i++) { + s[i]=0; } + rest.getResponse(s,sizeof(s)); + str = s; + if(str.rfind("[{\"mac\"") != std::string::npos) { + DBG.printf("get nodes from server\r\n"); + str.erase(str.begin(),str.begin()+str.rfind("[{\"mac\"")); + str.erase(str.begin()+str.rfind("}]")+2,str.end()); + + MbedJSONValue jsonValue; + parse(jsonValue,str.c_str()); + char *parameter[2] = {"mac","ip"}; + + //check whether nodes valid + bool validNodes = true; + for(int i=0; i<jsonValue.size(); i++) { + for(int j=0; j<2; j++) { + validNodes = validNodes && jsonValue[i].hasMember(parameter[j]); + } + } + DBG.printf("nodes validity:%d\r\n",validNodes); + + if(validNodes) { + for(int i=0; i<jsonValue.size(); i++) { + string macValue = jsonValue[i][parameter[0]].get<std::string>(); + string ipValue = jsonValue[i][parameter[1]].get<std::string>(); + nodes[i].macAddr = macValue; + nodes[i].ipAddr = ipValue; + DBG.printf("nodes[%d]-mac:%s\r\n",i,nodes[i].macAddr.c_str()); + DBG.printf("nodes[%d]-ip:%s\r\n",i,nodes[i].ipAddr.c_str()); + } + } + + } else { + DBG.printf("no nodes from server\r\n"); + } + + //preset nodes' macAddr and ipAddr - nodes[0].macAddr = "002629034222"; - nodes[0].ipAddr = "192.168.2.15"; - nodes[1].macAddr = "00262903424e"; - nodes[1].ipAddr = "192.168.2.32"; + //nodes[0].macAddr = "002629034222"; + //nodes[0].ipAddr = "192.168.2.15"; + //nodes[1].macAddr = "00262903424e"; + //nodes[1].ipAddr = "192.168.2.32"; //init rest to remotes for(int i=0; i<NODES_MAX; i++) { @@ -811,7 +850,7 @@ parse(jsonValue,globalCommand.c_str()); char *parameter[5] = {"id","nType","nAddr","dType","cmd"}; - //check if command is valid + //check whether command is valid bool validCommand = true; for(int i=0; i<5; i++) { validCommand = validCommand && jsonValue.hasMember(parameter[i]); @@ -1265,7 +1304,7 @@ } if(CVrms > VRMSTHRESHOLD || CWatt > WATTTHRESHOLD) { - DBG.printf("alert on channel C\r\n"); + 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);