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:
- 36:ed696fd1b44e
- Parent:
- 35:b8ba2b73201b
- Child:
- 37:93f196daf5cf
--- a/emmaCode.cpp Fri Aug 14 08:34:15 2015 +0000 +++ b/emmaCode.cpp Fri Aug 14 15:44:01 2015 +0000 @@ -936,10 +936,8 @@ char q[32]; char r[32]; char s[4096]; - int dbgLoop=0; int loop=0; int trial=0; - //string hmac; string hmacTime; string hmacCmd; string str; @@ -984,33 +982,26 @@ TFT.printf("GPRS:N/A"); } - //calculate hmac - //for(int j=0; j<sizeof(s); j++) { - // s[j]=0; } - //sprintf(s,"emma-%s",emmaUID.c_str()); - //hmac = calculateMD5(s); - //DBG.printf("hmac:%s\r\n",hmac.c_str()); - if(ethConnected) { DBG.printf("emmaModeOperation - eth\r\n"); //DBG.printf("IP Address:%s\r\n",ipstack.getEth().getIPAddress()); //DBG.printf("MAC Address:%s\r\n",ipstack.getEth().getMACAddress()); ethMQTTAttemptConnect(&client, &ipstack); - //t.start(); + t.start(); DBG.printf("start\r\n"); while(true) { - //if(!ipstack.getEth().linkstatus()) { - // NVIC_SystemReset(); - //} - //if(t.read_ms() > 5000) { - // if(publish(&client,&ipstack) != 0) - // ethMQTTAttemptConnect(&client, &ipstack); - // t.reset(); - //} + if(!ipstack.getEth().linkstatus()) { + NVIC_SystemReset(); + } + if(t.read_ms() > 5000) { + if(publish(&client,&ipstack) != 0) + ethMQTTAttemptConnect(&client, &ipstack); + t.reset(); + } //check for new command - if(newCommand) { - DBG.printf("newCommand\r\n"); +// if(newCommand) { +// DBG.printf("newCommand\r\n"); //DBG.printf("globalCommand: %s\r\n",globalCommand.c_str()); //string qw(globCmd); @@ -1080,8 +1071,8 @@ } } */ - newCommand = false; - } +// newCommand = false; +// } client.yield(100); //allow MQTT client to receive message } } else if(wifiConnected) { @@ -1091,7 +1082,7 @@ seconds = time(NULL); strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++++++++++++")); - DBG.printf("newDbg:%d\r\n",writeDbg(q,"++++++++++++++++++++")); + //DBG.printf("newDbg:%d\r\n",writeDbg(q,"++++++++++++++++++++")); //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work //_ESP.printf("MODE=B"); @@ -1122,12 +1113,6 @@ //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.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET); @@ -1138,15 +1123,6 @@ ADE.write16bits(BVAG, 0); ADE.write16bits(CVAG, 0); - //ADE.AWhLSB = 0.000001192;//0.00006025556; - //ADE.AWhLSB = 0.00002109; - //ADE.BWhLSB = 0.25075167; - //ADE.CWhLSB = 0.25075167; - - //ADE.AVAhLSB = 0.00008370; - //ADE.BVAhLSB = 0; - //ADE.CVAhLSB = 0; - //init rest to server if(rest.begin(REST_SERVER_HOST,REST_SERVER_PORT,false)) { DBG.printf("rest to server is created\r\n"); @@ -1215,7 +1191,7 @@ } /* - //success + //working DBG.printf("emma: setup mqtt client\r\n"); sprintf(mqttClientId,"emma/%s",emmaUID.c_str()); if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) { @@ -1236,12 +1212,6 @@ t.reset(); */ - //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"; - //init rest to remotes for(int i=0; i<NODES_MAX; i++) { if(!nodes[i].ipAddr.empty()) { @@ -1291,12 +1261,6 @@ espDHCPClientStart = false; } - //debugging - //seconds = time(NULL); - //strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); - //sprintf(r,"dbgPE[%d]",dbgLoop); - //DBG.printf("dbgPE:%d\r\n",writeDbg(q,r)); - //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); @@ -1397,12 +1361,6 @@ loop++; } - //debugging - //seconds = time(NULL); - //strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); - //sprintf(r,"dbgPEnv[%d]",dbgLoop); - //DBG.printf("dbgPEnv:%d\r\n",writeDbg(q,r)); - //panel environment checkRxBuffer(); if(tPanel.read() > 900.0f) { //900 is 15 minutes @@ -1465,12 +1423,6 @@ tPanel.reset(); } - //debugging - //seconds = time(NULL); - //strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); - //sprintf(r,"dbgNT[%d]",dbgLoop); - //DBG.printf("dbgNT:%d\r\n",writeDbg(q,r)); - //nodeTemp checkRxBuffer(); if(tNodes.read() > 900.0f) { //900 is 15 minutes @@ -1529,12 +1481,6 @@ tNodes.reset(); } - //debugging - //seconds = time(NULL); - //strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds)); - //sprintf(r,"dbgCmd[%d]",dbgLoop); - //DBG.printf("dbgCmd:%d\r\n",writeDbg(q,r)); - //command rest.get("/emma/api/controller/command"); checkRxBuffer(); @@ -1687,9 +1633,6 @@ newCommand = false; } - //debugging - //dbgLoop++; - osDelay(5000); } } @@ -2092,9 +2035,111 @@ } /*end wifi rest*/ +int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) +{ + MQTT::Message message; + char* topic = new char[strlen(platformDOMAIN.c_str())+strlen(emmaUID.c_str())+9]; + *topic = '\0'; + strcat(topic, platformDOMAIN.c_str()); + strcat(topic, "/"); + strcat(topic, emmaUID.c_str()); + strcat(topic, "/dummy"); + + const char buf[25] = "{\"d\":{\"myName\":\"EMMA\"}}"; + + message.qos = MQTT::QOS2; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf); + + DBG.printf("Publishing %s\r\n", buf); + return client->publish(topic, &message); +} + /*start eth mqtt*/ void ethMQTTMessageArrived(MQTT::MessageData& md) { MQTT::Message &message = md.message; + char topic[md.topicName.lenstring.len + 1]; + string str; + sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); + + DBG.printf("Message arrived on topic %s: %.*s\r\n", topic, message.payloadlen, message.payload); + char *s = (char*)message.payload; + string sp(s); + if(sp.find("[") != std::string::npos && sp.find("]") != std::string::npos) { + sp.erase(sp.begin(),sp.begin()+sp.find("[")+1); + sp.erase(sp.begin()+sp.find("]"),sp.end()); + } + else { + DBG.printf("Invalid MQTT command"); + return; + } + + MbedJSONValue jsonValue; + parse(jsonValue, sp.c_str()); + const char *parameter[5] = {"name","nType","nAddr","dType","cmd"}; + + //check if command is valid + bool validCommand = true; + for(int i=0; i<5; i++) { + validCommand = validCommand && jsonValue.hasMember(parameter[i]); + } + DBG.printf("command validity:%d\r\n",validCommand); + + //check for new command + if(validCommand) { + string commandId = jsonValue[parameter[0]].get<std::string>(); + string commandNType = jsonValue[parameter[1]].get<std::string>(); + string commandNAddr = jsonValue[parameter[2]].get<std::string>(); + string commandDType = jsonValue[parameter[3]].get<std::string>(); + string commandCmd = jsonValue[parameter[4]].get<std::string>(); + + if(commandNType == "0") { //switch on panel controller + DBG.printf("command for switch\r\n"); + } + 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 = readNodeIP(commandNAddr); + //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str()); + + //get cmd string based on device type and command number + string nodeCmd = readNodeCmd(commandDType,commandCmd); + //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str()); + //execute command + int trial=0; + trial = 0; + bool execStatus=false; + + while( !execStatus ) { + sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); + str.assign(s); + DBG.printf("str value:%s\r\n",str.c_str()); + string rcv = ethGET(nodeIP,REMOTE_TCP_PORT,str); // cause mqtt to stop + DBG.printf("response:%s\r\n",rcv.c_str()); + str = rcv; + if(str.find("OK") != std::string::npos) { + DBG.printf("CMD executed successfully\r\n"); + execStatus = true; + break; + } + if(trial>5) { //two times trial + DBG.printf("cmd is not executed\r\n"); + execStatus = false; + } + DBG.printf("Trial++\r\n"); + trial++; + wait(3); + } + DBG.printf("Send execution status\r\n"); + //send execution status + } + } + DBG.printf("Finish processing new command\r\n"); + + /* + MQTT::Message &message = md.message; DBG.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); DBG.printf("Payload: %.*s\r\n", message.payloadlen, (char*)message.payload); @@ -2130,22 +2175,20 @@ newCommand = true; } sp.clear(); + */ } int ethMQTTConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) { - //char hostname[] = "q.thingfabric.com"; - //char hostname[] = "192.168.131.200"; - //int rc = ipstack->connect(hostname, MQTT_PORT); int rc = ipstack->connect(MQTT_HOST, MQTT_PORT); if(rc!=0) return rc; //MQTT Connect - //char clientId[] = "emma/0674ff575349896767072538"; - char clientId [32]; + char clientId [64]; sprintf(clientId,"emma/%s",emmaUID.c_str()); //DBG.printf("clientId:%s\r\n",clientId); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; data.clientID.cstring = clientId; @@ -2153,16 +2196,22 @@ data.password.cstring = "ef25cf4567fbc07113252f8d72b7faf2"; if((rc = client->connect(&data)) == 0) { - DBG.printf("connected\r\n"); + DBG.printf("ethMQTT connected\r\n"); } //MQTT Subscribe - //char* topic = "gaisbwqreqrfxjc/0674ff575349896767072538/command"; - char s[64]; - sprintf(s,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str()); - string topic = s; - //DBG.printf("topic:%s\r\n",topic.c_str()); - if((rc=client->subscribe(topic.c_str(),MQTT::QOS0,ethMQTTMessageArrived)) == 0) { + //char s[64]; + //sprintf(s,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str()); + //string topic = s; + char* topic = new char[strlen(platformDOMAIN.c_str())+strlen(emmaUID.c_str())+9]; + *topic = '\0'; + strcat(topic, platformDOMAIN.c_str()); + strcat(topic, "/"); + strcat(topic, emmaUID.c_str()); + strcat(topic, "/command"); + DBG.printf("topic:%s\r\n",topic); + + if((rc=client->subscribe(topic,MQTT::QOS0,ethMQTTMessageArrived)) == 0) { DBG.printf("subscribe success!\r\n"); } return rc;