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:
- 33:232069745431
- Parent:
- 32:78633fb9b2a1
--- a/emmaCode.cpp Mon Aug 10 07:04:52 2015 +0000 +++ b/emmaCode.cpp Wed Aug 12 10:04:44 2015 +0000 @@ -8,7 +8,7 @@ //init espduino - without ch_pd pin ESP esp(&_ESP, &DBG, ESP_BAUD); //init wifi mqtt -//ESPMQTT mqtt(&esp); +ESPMQTT mqtt(&esp); //init wifi rest REST rest(&esp); @@ -94,8 +94,8 @@ bool gprsConnected = false; bool useProxy = false; bool newCommand = false; -bool espFreeMemory = true; //for after bootup initialization -bool espDHCPClientStart = false; +bool espFreeMemory = false; +bool espDHCPClientStart = true; //for after bootup initialization bool newEnergyData = false; string globalCommand; string rxBuf; @@ -931,7 +931,7 @@ } void emmaModeOperation(void) { - //char mqttClientId[32]; + char mqttClientId[32]; char p[64]; char q[32]; char r[32]; @@ -1288,15 +1288,36 @@ _ESP.printf("MODE=B"); wait(2); + + //disable UART2 + NVIC_DisableIRQ(USART2_IRQn); + + 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)) { + if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 43200, 1)) { //keep-alive 12 hours + mqtt.connectedCb.attach(&mqttConnected); + mqtt.disconnectedCb.attach(&mqttDisconnected); + mqtt.connect(MQTT_HOST,MQTT_PORT,false); + DBG.printf("emma: success to setup mqtt\r\n"); + TFT.locate(0,40); + TFT.printf("emma: success to setup mqtt"); + } + DBG.printf("emma: system started\r\n"); + + t.start(); + while(t.read_ms() < 5000) { + esp.process(); + } + t.stop(); + t.reset(); + + //enable UART2 + NVIC_EnableIRQ(USART2_IRQn); + 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 +1418,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 +1480,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,14 +1538,8 @@ 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"); + //rest.get("/emma/api/controller/command"); checkRxBuffer(); if(newCommand) { DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str()); @@ -2027,7 +2030,7 @@ DBG.printf("MQTT Connected\r\n"); char mqttTopic[64]; sprintf(mqttTopic,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str()); - //mqtt.subscribe(mqttTopic); + mqtt.subscribe(mqttTopic); } void mqttDisconnected(void* response) { DBG.printf("MQTT Disconnected\r\n"); @@ -2081,7 +2084,8 @@ } //check dhcp client start -> initialize all connection - if(rxBuf.rfind("dhcp client start") != std::string::npos) { + //if(rxBuf.rfind("dhcp client start") != std::string::npos) { + if(rxBuf.rfind("ip:") != std::string::npos && rxBuf.rfind("mask:") != std::string::npos && rxBuf.rfind("gw:") != std::string::npos) { rxLogA = "dhcp client start-" + rxBuf; espDHCPClientStart = true; }