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:
- 5:d00233dd36f5
- Parent:
- 4:76ab12e2f8a3
- Child:
- 6:45f50f1177db
diff -r 76ab12e2f8a3 -r d00233dd36f5 emmaCode.cpp --- a/emmaCode.cpp Wed Jul 08 13:33:14 2015 +0000 +++ b/emmaCode.cpp Wed Jul 08 14:04:28 2015 +0000 @@ -756,61 +756,57 @@ loop++; } + checkRxBuffer(); + checkVoltagePower(); //nodeTemp /* - if(tNodeTemp.read_ms() > 30000) { - DBG.printf("getNodeTemperature\r\n"); - - //get list of nodes - string *node; - node = readNodeList(); + if(tNodes.read_ms() > 30000) { + DBG.printf("getNodesTemperature\r\n"); + + for(int i=0; i<NODES_MAX; i++) { + if(!nodes[i].ipAddr.empty()) { + //get node's temp + string temp; + nodes[i].restConn->get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n"); + wait(2); + temp = rxBuf; + if(temp.rfind(nodes[i].ipAddr) != std::string::npos) { + temp.erase(temp.begin(),temp.begin()+temp.rfind(nodes[i].ipAddr)); + if(temp.rfind("temp=") != std::string::npos) { + temp.erase(temp.begin(),temp.begin()+temp.rfind("temp=")+6); + temp.erase(temp.begin()+temp.find("\""),temp.end()); + } else { + temp = "0"; //connect to node, but receive none + } + } else { + temp = "0"; //not connected to node + } - int idx=0; - while(!(node[idx].empty())) { - //get node's temperature - string nodeTemp = wifiGetNodeTemp(node[idx]); - DBG.printf("nodeTemp[%d]:%s\r\n",idx,nodeTemp.c_str()); - - //send node's temperature - if(rest.begin("192.168.131.200",8080,false)) { - for(int j=0; j<sizeof(s); j++) { - s[j]=0; } + DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str()); + + //send node's temp sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"mac\":\"%s\",\"value\":%s}", - emmaUID.c_str(),hmac.c_str(),node[idx].c_str(),nodeTemp.c_str()); + emmaUID.c_str(),hmac.c_str(),nodes[i].macAddr.c_str(),temp.c_str()); rest.post("/emma/api/controller/nodetemp",s); - for(int j=0; j<sizeof(s); j++) { - s[j]=0; } - rest.getResponse(s,sizeof(s)); - DBG.printf("Response: %s\r\n",s); - } - idx++; + wait(2); + temp = rxBuf; + if(temp.rfind("/nodetemp") != std::string::npos) { + temp.erase(temp.begin(),temp.begin()+temp.rfind("/nodetemp")); + if(temp.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("send nodeTemp success\r\n"); + } else { + DBG.printf("send nodeTemp failed\r\n"); + } + } + } } - tNodeTemp.reset(); - } - */ - //publish data - //if(t.read_ms() > 30000) { - // sprintf(mqttTopic,"%s/%s/panelTemp",platformDOMAIN.c_str(),emmaUID.c_str()); - // sprintf(mqttPayload,"{\"time\":\"20150615-12:32:12\",\"value\":%d}",loop); - // DBG.printf(mqttPayload); - // DBG.printf("\r\n"); - // mqtt.publish(mqttTopic,mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelTemp",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelHum",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelGas",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelCh1",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelCh2",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelCh3",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/nodeTemp",mqttPayload); - //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/cmdExecution","success"); - // loop++; - // t.reset(); - //} - - //check for new command - /* + } + */ + checkRxBuffer(); + checkVoltagePower(); + //command if(newCommand) { - DBG.printf("newCommand\r\n"); + DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str()); MbedJSONValue jsonValue; parse(jsonValue,globalCommand.c_str()); char *parameter[5] = {"id","nType","nAddr","dType","cmd"}; @@ -835,67 +831,77 @@ else if(commandNType == "1") { //node with mac address DBG.printf("command for node\r\n"); //get node ip address based on node mac address - string nodeIP; + //string nodeIP; //nodeIp = readNodeIP(commandNAddr); - nodeIP = "192.168.2.15"; + //nodeIP = "192.168.2.15"; //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str()); + + //get index of node list based on mac address + int idx = NODES_INVALID; + for(int i=0; i<NODES_MAX; i++) { + if(!nodes[i].macAddr.compare(commandNAddr)) { + idx = i; + } + } + + //execution process + int trial; + string execResult = "notExecuted"; + if(idx != NODES_INVALID) { + DBG.printf("index found at %d\r\n",idx); - //get cmd string based on device type and command number - string nodeCmd; - //nodeCmd = readNodeCmd(commandDType,commandCmd); - nodeCmd = "020129A0163B161315131613153C151316131514143C153C16141414141415151315141414141514141415141414143D1514143D141415141414143D14000D"; - //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str()); + //get cmd string based on device type and command number + string nodeCmd; + //nodeCmd = readNodeCmd(commandDType,commandCmd); + nodeCmd = "020129A0163B161315131613153C151316131514143C153C16141414141415151315141414141514141415141414143D1514143D141415141414143D14000D"; //turn off + //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str()); - //execute command - if(rest.begin(nodeIP.c_str(),REMOTE_TCP_PORT,false)) { + //execute command + DBG.printf("executing command\r\n"); sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); - int trial=0; - string execStatus = "failed"; + trial=0; while(1) { - char rcv[1024] = {}; - rest.get("/",s); - rcvReply(rcv,2000); - str = rcv; - if(str.find("REST: Sent") != std::string::npos) { - DBG.printf("cmd executed\r\n"); - execStatus = "success"; - break; - } - if(trial>0) { //two times trial + //cmdExecuted = false; + if(trial>=2) { //two times trial DBG.printf("cmd is not executed\r\n"); break; } + nodes[idx].restConn->get("/",s); + wait(2); + if(rxBuf.find("REST: Sent") != std::string::npos) { + DBG.printf("cmd executed\r\n"); + execResult = "executed"; + break; + } trial++; - wait(3); + } + } + + wait(2); + //send execution result + DBG.printf("send execution result\r\n"); + sprintf(s,"{\"id\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\"}", + commandId.c_str(),commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execResult.c_str()); + + trial=0; + while(1) { + if(trial>=2) { //two times trial + DBG.printf("send execution result failed\r\n"); + break; } - - //send execution status - mqtt - //sprintf(mqttTopic,"%s/%s/executionResult",platformDOMAIN.c_str(),emmaUID.c_str()); - //sprintf(mqttPayload,"{\"id\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\"}", - //commandId.c_str(),commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execStatus.c_str()); - //DBG.printf(mqttPayload); - //DBG.printf("\r\n"); - //mqtt.publish(mqttTopic,mqttPayload); - - //send execution status - rest - wait(5); - if(rest.begin("192.168.131.200",8080,false)) { - sprintf(mqttPayload,"{\"id\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\"}", - commandId.c_str(),commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execStatus.c_str()); - //rest.post("/emma/api/controller/test","{\"test\": \"post\"}"); - rest.post("/emma/api/controller/test",mqttPayload); - rest.getResponse(s,sizeof(s)); - DBG.printf("Response: %s\r\n",s); - for(int j=0; j<sizeof(s); j++) { - s[j]=0; } + rest.post("/emma/api/controller/test",s); + wait(2); + if(rxBuf.find("\"status\":\"OK\"") != std::string::npos) { + DBG.printf("send execution result success\r\n"); + break; } + trial++; } } } newCommand = false; } - */ osDelay(5000); } }