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:
- 37:93f196daf5cf
- Parent:
- 36:ed696fd1b44e
- Child:
- 38:e44f35f8129d
--- a/emmaCode.cpp Fri Aug 14 15:44:01 2015 +0000 +++ b/emmaCode.cpp Wed Aug 19 08:52:27 2015 +0000 @@ -15,7 +15,7 @@ //init eth port SPI spi(PB_15, PB_14, PB_13); //mosi, miso, sck MQTTEthernet ipstack(&spi, PB_12, PC_6); //spi, cs, reset -MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); +//MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); //init sd card SDFileSystem sd(PA_7, PA_6, PA_5, PB_3, "sd"); //mosi, miso, sck, cs @@ -27,7 +27,6 @@ SPI_TFT_ILI9341 TFT(PA_7, PA_6, PA_5, PA_4, PC_5, PC_4,"TFT"); //mosi, miso, sclk, cs, reset, dc //init touch screen - without cs pin TouchScreenADS7843 TP(PB_10, PB_0, PB_1, PB_2, &TFT); //mosi, miso, sclk, irq, tft -//InterruptIn tpIRQ(PB_2); //init dht sensor DHT11 d(PD_2); @@ -73,9 +72,6 @@ //ade7758 variables uint32_t AWattHrValue, BWattHrValue, CWattHrValue; uint32_t AVAHrValue, BVAHrValue, CVAHrValue; -//long AWattHrSum = 0; -//long BWattHrSum = 0; -//long CWattHrSum = 0; uint32_t AWattHrSum = 0; uint32_t BWattHrSum = 0; uint32_t CWattHrSum = 0; @@ -94,7 +90,7 @@ bool gprsConnected = false; bool useProxy = false; bool newCommand = false; -bool espFreeMemory = true; //for after bootup initialization +bool espFreeMemory = true; //for after bootup bool espDHCPClientStart = false; bool newEnergyData = false; string globalCommand; @@ -141,11 +137,6 @@ TP.SetCalibration(&matrix, &ScreenSample[0]); - //TFT.locate(0,0); - //TFT.printf(" X:"); - //TFT.locate(70,0); - //TFT.printf(" Y:"); - //draw border TFT.line(15,15,310,15,Orange); TFT.line(310,15,310,250,Orange); @@ -231,18 +222,9 @@ TPx = TP.display.x; TPy = TP.display.y; TP.TP_DrawPoint(TPx,TPy, Blue); - //TFT.locate(25,0); - //TFT.printf("%03d",TPx); - //TFT.locate(95,0); - //TFT.printf("%03d",TPy); for(int i=0; i<6; i++) { if((menu[i].xTL < TPx && TPx < menu[i].xBR) && (menu[i].yTL < TPy && TPy < menu[i].yBR)) { - //TFT.locate(25,170); - //TFT.printf(" "); - //TFT.locate(25,170); - //TFT.printf("mode: %s is selected",menu[i].text.c_str()); - //wait(3); md = i; modeSelected = true; } @@ -280,24 +262,22 @@ DBG.printf("\r\nemmaReadSettings\r\n"); //read settings - //readSetting("emmaUID"); //sd card need to be read once before working correctly emmaUID = readSetting("emmaUID"); //emmaUID = "066eff575349896767073038"; DBG.printf("emmaUID:%s\r\n",emmaUID.c_str()); + //calculate hmac for(int i=0; i<sizeof(s); i++) { s[i]=0; } sprintf(s,"emma-%s",emmaUID.c_str()); hmac = calculateMD5(s); DBG.printf("hmac:%s\r\n",hmac.c_str()); + platformDOMAIN = readSetting("platformDOMAIN"); - //platformDOMAIN = "testdulu"; DBG.printf("platformDOMAIN:%s\r\n",platformDOMAIN.c_str()); platformKEY = readSetting("platformKEY"); - //platformKEY = "5980e444-81dd-47ba-8222-6a40bc94fdce"; DBG.printf("platformKEY:%s\r\n",platformKEY.c_str()); platformSECRET = readSetting("platformSECRET"); - //platformSECRET = "3ca8ec0239fda2b6d12ba1580c91a052"; DBG.printf("platformSECRET:%s\r\n",platformSECRET.c_str()); proxySERVER = readSetting("proxySERVER"); DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str()); @@ -381,14 +361,14 @@ if(st) { DBG.printf("%s is saved\r\n",parameter[i]); TFT.locate(0,40); - TFT.printf(" %s is saved\r\n",parameter[i]); + TFT.printf(" %s is saved",parameter[i]); wait(3); TFT.locate(0,40); TFT.printf(" "); } else { DBG.printf("%s is not saved\r\n",parameter[i]); TFT.locate(0,40); - TFT.printf(" %s is not saved\r\n",parameter[i]); + TFT.printf(" %s is not saved",parameter[i]); wait(3); TFT.locate(0,40); TFT.printf(" "); @@ -437,6 +417,11 @@ if(ethAvailable) { DBG.printf("emmaModeSettings - eth\r\n"); + wait(2); + TFT.locate(0,0); + TFT.printf(" "); + TFT.locate(0,0); + TFT.printf(" emmaModeSettings"); TCPSocketServer svr; TCPSocketConnection clientSock; @@ -451,9 +436,14 @@ DBG.printf("please connect to %s\r\n",ipstack.getEth().getIPAddress()); if(svr.listen(1) < 0) { - DBG.printf("tcp server listen failed\r\n"); + DBG.printf("tcp server listen failed\r\n"); + TFT.locate(0,20); + TFT.printf(" settings error. please restart."); + while(1); } else { - DBG.printf("tcp server is listening...\r\n"); + DBG.printf("tcp server is listening...\r\n"); + TFT.locate(0,20); + TFT.printf(" connect with emma app now!"); } clientSock.set_blocking(false,30000); //timeout after 30sec @@ -461,9 +451,18 @@ //listening while (serverIsListened) { if(svr.accept(clientSock) < 0) { - DBG.printf("failed to accept connection\r\n"); + DBG.printf("failed to accept connection\r\n"); + TFT.locate(0,20); + TFT.printf(" "); + TFT.locate(0,20); + TFT.printf("failed to accept connection"); } else { DBG.printf("connection success!\r\nIP: %s\r\n",clientSock.get_address()); + TFT.locate(0,20); + TFT.printf(" "); + TFT.locate(0,20); + //TFT.printf(" connection success! IP: %s",clientSock.get_address()); + TFT.printf(" connection success!"); clientIsConnected = true; while(clientIsConnected) { @@ -497,8 +496,18 @@ int st = writeSetting(parameter[i],val.c_str()); if(st) { DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str()); + TFT.locate(0,40); + TFT.printf(" %s: %s is saved",parameter[i],val.c_str()); + wait(1); + TFT.locate(0,40); + TFT.printf(" "); } else { DBG.printf("%s is not saved\r\n",parameter[i]); + TFT.locate(0,40); + TFT.printf(" %s is not saved",parameter[i]); + wait(1); + TFT.locate(0,40); + TFT.printf(" "); } } } @@ -510,12 +519,21 @@ sscanf(epTime.c_str(),"%d",&seconds); set_time(seconds); DBG.printf("time is set\r\n"); + TFT.locate(0,40); + TFT.printf(" time is set"); + wait(1); + TFT.locate(0,40); + TFT.printf(" "); } } break; } } DBG.printf("close connection\r\n"); + TFT.locate(0,20); + TFT.printf(" "); + TFT.locate(0,20); + TFT.printf(" settings finish. please restart."); clientSock.close(); } } @@ -534,7 +552,7 @@ TFT.locate(0,0); TFT.printf(" emmaModeSettings"); TFT.locate(0,20); - TFT.printf(" connect with emma app now"); + TFT.printf(" connect with emma app now!"); while(1) { char rcv[512] = {}; @@ -560,14 +578,14 @@ DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str()); TFT.locate(0,40); TFT.printf(" %s: %s is saved",parameter[i],val.c_str()); - wait(3); + wait(1); TFT.locate(0,40); TFT.printf(" "); } else { DBG.printf("%s is not saved\r\n",parameter[i]); TFT.locate(0,40); TFT.printf(" %s is not saved",parameter[i]); - wait(3); + wait(1); TFT.locate(0,40); TFT.printf(" "); } @@ -583,7 +601,7 @@ DBG.printf("time is set\r\n"); TFT.locate(0,40); TFT.printf(" time is set"); - wait(3); + wait(1); TFT.locate(0,40); TFT.printf(" "); } @@ -599,7 +617,7 @@ TFT.locate(0,20); TFT.printf(" "); TFT.locate(0,20); - TFT.printf(" connection success"); + TFT.printf(" connection success!"); } } } else { @@ -617,7 +635,6 @@ int loop = 0; string connData; string connHost; - //string hmac; string str; string regKey; Timer t; @@ -626,20 +643,12 @@ TFT.printf(" please wait"); //check connected interface - //connectedIface(); isEthConnected(); isWiFiConnected(); isGprsConnected(); DBG.printf("ethConnected:%d\r\n",ethConnected); DBG.printf("wifiConnected:%d\r\n",wifiConnected); - //calculate hmac - //for(int i=0; i<sizeof(s); i++) { - // s[i]=0; } - //sprintf(s,"emma-%s",emmaUID.c_str()); - //hmac = calculateMD5(s); - //DBG.printf("hmac:%s\r\n",hmac.c_str()); - if(ethConnected) { DBG.printf("emmaModeRegister - eth\r\n"); @@ -951,7 +960,6 @@ TFT.printf(" please wait"); //check connected interface - //connectedIface(); isEthConnected(); isWiFiConnected(); isGprsConnected(); @@ -986,9 +994,10 @@ 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); + //ethMQTTAttemptConnect(&client, &ipstack); t.start(); DBG.printf("start\r\n"); + /* while(true) { if(!ipstack.getEth().linkstatus()) { NVIC_SystemReset(); @@ -998,83 +1007,9 @@ ethMQTTAttemptConnect(&client, &ipstack); t.reset(); } - - //check for new command -// if(newCommand) { -// DBG.printf("newCommand\r\n"); - //DBG.printf("globalCommand: %s\r\n",globalCommand.c_str()); - //string qw(globCmd); - - //if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) { - // cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1); - // cmd.erase(cmd.begin()+cmd.find("]"),cmd.end()); - //globalCommand = cmd; - //newCommand = true; - //} - /* - MbedJSONValue jsonValue; - parse(jsonValue,globalCommand.c_str()); - char *parameter[5] = {"id","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); - - 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; - string execStatus="failed"; - - while(1) { - sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); - str = s; - string rcv = ethGET(nodeIP,REMOTE_TCP_PORT,str); - DBG.printf("response:%s\r\n",rcv.c_str()); - str = rcv; - if(str.find("OK") != std::string::npos) { - DBG.printf("cmd executed\r\n"); - execStatus = "success"; - break; - } - if(trial>0) { //two times trial - DBG.printf("cmd is not executed\r\n"); - break; - } - trial++; - wait(3); - } - - //send execution status - } - } - */ -// newCommand = false; -// } client.yield(100); //allow MQTT client to receive message } + */ } else if(wifiConnected) { DBG.printf("emmaModeOperation - wifi\r\n"); @@ -2436,90 +2371,6 @@ /*end emma connection function*/ /*start emma private function*/ -/* -void connectedIface(void) { //WARNING: should be run in emmaModeRegister and emmaModeOperation only - problem with esp, after MODE=B, cannot go back to MODE=S - char s[512]; - int connPort; - string connHost; - string str; - Timer t; - - //wifi interface - if(wifiAvailable) { - _ESP.printf("MODE=B"); - if(useProxy) { - connHost = proxySERVER; - sscanf(proxyPORT.c_str(),"%d",&connPort); - for(int i=0; i<sizeof(s); i++) { - s[i]=0; } - sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST); - } else { - connHost = REST_SERVER_HOST; - connPort = REST_SERVER_PORT; - for(int i=0; i<sizeof(s); i++) { - s[i]=0; } - sprintf(s,"/emma/api/controller/test"); - } - wait(1); - t.start(); - while(!esp.ready() && t.read_ms() < 5000); - t.stop(); - if(rest.begin(connHost.c_str(),connPort,false)) { - //DBG.printf("rest begin\r\n"); - esp.process(); - rest.get(s); - for(int i=0; i<sizeof(s); i++) { - s[i]=0; } - rest.getResponse(s,sizeof(s)); - str = s; - //DBG.printf("response:%s\r\n",s); - if(str.find("OK") != std::string::npos) { - wifiConnected = true; - } - } else { - wifiConnected = false; - } - } else { - wifiConnected = false; - } - - //eth interface - if(ethAvailable) { - if(useProxy) { - connHost = proxySERVER; - sscanf(proxyPORT.c_str(),"%d",&connPort); - for(int i=0; i<sizeof(s); i++) { - s[i]=0; } - sprintf(s,"GET http://%s:%d/emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n",REST_SERVER_HOST,REST_SERVER_PORT,REST_SERVER_HOST); - } else { - connHost = REST_SERVER_HOST; - connPort = REST_SERVER_PORT; - for(int i=0; i<sizeof(s); i++) { - s[i]=0; } - strcpy(s,"GET /emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n"); - } - - t.start(); - while(1) { - str = ethGET(connHost,connPort,s); - if(str.find("OK") != std::string::npos) { - t.stop(); - ethConnected = true; - break; - } - if(t.read_ms() > 5000) { - t.stop(); - ethConnected = false; - break; - } - } - } else { - ethConnected = false; - } - - //gprs interface -} -*/ void isEthAvailable(void) { if(ipstack.getEth().linkstatus()) { ethAvailable = true;