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:
- 61:4eda981730ae
- Parent:
- 60:1d4232f5f9c9
- Child:
- 62:03ba6e82c290
--- a/emmaCode.cpp Fri Sep 25 08:44:03 2015 +0000 +++ b/emmaCode.cpp Sat Sep 26 09:43:09 2015 +0000 @@ -150,6 +150,7 @@ bool newEnergyData = false; string globalCommand; string rxBuf; +string gprsRxBuf; string rxLog; string rxLogA; @@ -181,7 +182,7 @@ TFT.fillarc(159,149,20,10,0,360, colorLightGray); for(int i=0; i<2880; i+=4) { - TFT.fillarc(159,149,20,10,(i>>1)-45,(i>>1)+45,colorDarkGray); + TFT.fillarc(159, 149, 20, 10, (i>>1)-45, (i>>1)+45,colorDarkGray); TFT.fillarc(159, 149, 20, 10, (i >> 1)-45-4, (i >> 1)-45, colorLightGray); TFT.fillarc(159, 149, 40, 10, 1440-i-45, 1440-i+45, colorDarkGray); @@ -932,13 +933,14 @@ Timer t; TFT.locate(0,0); - TFT.printf(" please wait"); + TFT.printf(" please wait..."); //check connected interface isEthConnected(); if(!ethConnected) { isWiFiConnected(); - } else if(!ethConnected && !wifiConnected) { + } + if(!ethConnected && !wifiConnected) { isGprsConnected(); } @@ -1110,8 +1112,8 @@ connHost = restSERVER; connPort = restPORT; } - TFT.locate(0,0); - TFT.printf(" emmaModeRegister"); + //TFT.locate(0,0); + //TFT.printf(" emmaModeRegister"); //rest begin if(!rest.begin(connHost.c_str(),connPort,false)) { @@ -1126,132 +1128,143 @@ } //wifiConnected = true; //with custom firmware, wifi module should connect automatically - esp.process(); - if(wifiConnected) { - //check proxy - if(useProxy) { - sprintf(r,"http://%s:%d/%s/api/controller/register",restSERVER.c_str(),restPORT,appNAME.c_str()); - } else { - sprintf(r,"/%s/api/controller/register",appNAME.c_str()); - } + //esp.process(); + //check proxy + if(useProxy) { + sprintf(r,"http://%s:%d/%s/api/controller/register",restSERVER.c_str(),restPORT,appNAME.c_str()); + } else { + sprintf(r,"/%s/api/controller/register",appNAME.c_str()); + } - //register - while(!emmaGetRegKey) { - sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str()); - //DBG.printf("s:%s\r\n",s); - //string iniStr = "{\"uid\":\"005300553533510334313732\",\"hmac\":\"45fc6a4447989a9434d1f21087a78061\"}"; - //DBG.printf("s:%s\r\n",iniStr.c_str()); - - //rest.post(r,iniStr.c_str()); - rest.post(r,s); - for(int i=0; i<sizeof(s); i++) { - s[i]=0; } - rest.getResponse(s,sizeof(s)); - DBG.printf("rsp reg:%s\r\n",s); + //register + while(!emmaGetRegKey) { + sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str()); - //check and save settings - str = s; - if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) { - str.erase(str.begin(),str.begin()+str.find("[")+1); - str.erase(str.begin()+str.find("]"),str.end()); + rest.post(r,s); + //for(int i=0; i<sizeof(s); i++) { + // s[i]=0; } + rest.getResponse(s,sizeof(s)); + DBG.printf("rsp reg:%s\r\n",s); + + //check and save settings + str = s; + if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.find("[")+1); + str.erase(str.begin()+str.find("]"),str.end()); - MbedJSONValue jsonValue; - parse(jsonValue,str.c_str()); + MbedJSONValue jsonValue; + parse(jsonValue,str.c_str()); - char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"}; + char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"}; - //save mqtt parameter - writeSetting(parameter[0],"()"); //sd card need to be initialized - for(int i=0; i<3; i++) { - if(jsonValue.hasMember(parameter[i])) { - string val = jsonValue[parameter[i]].get<std::string>(); - int st = writeSetting(parameter[i],val.c_str()); - if(st) { - DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str()); - } else { - DBG.printf("%s is not saved\r\n",parameter[i]); - } - } - } - - //get registrationKey - if(jsonValue.hasMember(parameter[3])) { - string val = jsonValue[parameter[3]].get<std::string>(); - if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) { - val.erase(val.begin(),val.begin()+val.find("(")+1); - val.erase(val.begin()+val.find(")"),val.end()); - regKey = val; - DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str()); - - TFT.locate(0,20); - TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str()); - - emmaGetRegKey = true; + //save mqtt parameter + writeSetting(parameter[0],"()"); //sd card need to be initialized + for(int i=0; i<3; i++) { + if(jsonValue.hasMember(parameter[i])) { + string val = jsonValue[parameter[i]].get<std::string>(); + int st = writeSetting(parameter[i],val.c_str()); + if(st) { + DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str()); + } else { + DBG.printf("%s is not saved\r\n",parameter[i]); } } } - wait(1); - } - - //calculate hmac - for(int i=0; i<sizeof(r); i++) { - r[i]=0; } - sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str()); - hmac = calculateMD5(r); - DBG.printf("hmac:%s\r\n",hmac.c_str()); - - //check proxy - if(useProxy) { - sprintf(r,"http://%s:%d/%s/api/controller/verify",restSERVER.c_str(),restPORT,appNAME.c_str()); - } else { - sprintf(r,"/%s/api/controller/verify",appNAME.c_str()); - } - - //verify registration - while(!emmaRegistered && loop < 12){ - sprintf(s,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str()); - rest.post(r,s); - rest.getResponse(s,sizeof(s)); - DBG.printf("rsp vrf:%s\r\n",s); - TFT.locate(0,40); - TFT.printf(" "); - TFT.locate(0,40); - TFT.printf(" wait:%d\r\n",loop); - - //check verification - str = s; - if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) { - str.erase(str.begin(),str.begin()+str.find("[")+1); - str.erase(str.begin()+str.find("]"),str.end()); - - MbedJSONValue jsonValue; - parse(jsonValue,str.c_str()); - if(jsonValue.hasMember("user")) { - string val = jsonValue["user"].get<std::string>(); - DBG.printf(" %s is registered\r\n",val.c_str()); - TFT.locate(0,40); - TFT.printf(" "); - TFT.locate(0,40); - TFT.printf(" %s is registered\r\n",val.c_str()); - emmaRegistered = true; + + //get registrationKey + if(jsonValue.hasMember(parameter[3])) { + string val = jsonValue[parameter[3]].get<std::string>(); + if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) { + val.erase(val.begin(),val.begin()+val.find("(")+1); + val.erase(val.begin()+val.find(")"),val.end()); + regKey = val; + DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str()); + + //TFT.locate(0,20); + //TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str()); + TFT.cls(); + TFT.locate(5,5); + TFT.set_font((unsigned char*) Lato19x19); + TFT.printf(" Input kode & tunggu ya..."); + TFT.locate(100,100); + TFT.set_font((unsigned char*) LatoBlack39x38); + TFT.printf("%s",regKey.c_str()); + TFT.fillarc(165,120,90,10,0,360,Green); + + emmaGetRegKey = true; } } - wait(5); - loop++; } + wait(1); + } + + //calculate hmac + //for(int i=0; i<sizeof(r); i++) { + // r[i]=0; } + sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str()); + hmac = calculateMD5(r); + DBG.printf("hmac:%s\r\n",hmac.c_str()); - //check whether registration success - if(emmaRegistered) { - DBG.printf("registration successful\r\n"); - TFT.locate(0,60); - TFT.printf(" registration successful. please restart.\r\n"); - } else { - DBG.printf("registration unsuccessful\r\n"); - TFT.locate(0,60); - TFT.printf(" registration unsuccessful. please restart.\r\n"); + //check proxy + if(useProxy) { + sprintf(r,"http://%s:%d/%s/api/controller/verify",restSERVER.c_str(),restPORT,appNAME.c_str()); + } else { + sprintf(r,"/%s/api/controller/verify",appNAME.c_str()); + } + + //verify registration + TFT.set_font((unsigned char*) Lato19x19); + while(!emmaRegistered && loop < 12){ + sprintf(s,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str()); + rest.post(r,s); + rest.getResponse(s,sizeof(s)); + DBG.printf("rsp vrf:%s\r\n",s); + //TFT.locate(0,200); + //TFT.printf(" "); + //TFT.locate(10,200); + //TFT.printf(" wait:%d\r\n",loop); + TFT.fillarc(165,120,90,10,0,(loop*30)+30,colorDarkGray); + + //check verification + str = s; + if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.find("[")+1); + str.erase(str.begin()+str.find("]"),str.end()); + + MbedJSONValue jsonValue; + parse(jsonValue,str.c_str()); + if(jsonValue.hasMember("user")) { + string val = jsonValue["user"].get<std::string>(); + DBG.printf(" %s is registered\r\n",val.c_str()); + TFT.cls(); + TFT.locate(5,5); + TFT.printf(" %s is registered\r\n",val.c_str()); + emmaRegistered = true; + } } - while(1); + wait(5); + loop++; } + + //check whether registration success + TFT.cls(); + if(emmaRegistered) { + DBG.printf("registration successful\r\n"); + lcdDrawSmile(); + TFT.locate(0,200); + TFT.printf(" "); + TFT.locate(10,200); + TFT.printf("Sukses! Tolong restart EMMA"); + } else { + DBG.printf("registration unsuccessful\r\n"); + lcdDrawFrown(); + TFT.locate(0,200); + TFT.printf(" "); + TFT.locate(10,200); + TFT.printf("Gagal! Tolong restart EMMA"); + } + while(1); + } else if(gprsConnected) { DBG.printf("emmaModeRegister - gprs\r\n"); @@ -1259,8 +1272,11 @@ connPort = restPORT; connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str()); sprintf(s,"POST /%s/api/controller/register HTTP/1.1\nHost: %s:%d\nContent-Length: %d\n\n%s\n\n%c",appNAME.c_str(),restSERVER.c_str(),connPort,connBodyLen,connBody,26); + while(!emmaGetRegKey) { - str = gprsREST(restSERVER,connPort,s); + //str = gprsREST(restSERVER,connPort,s); + str = gprsRESTAnimate(restSERVER,connPort,s); + DBG.printf("emmaGetRegKey:%s\r\n",str.c_str()); //check and save settings if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) { @@ -1389,7 +1405,8 @@ isEthConnected(); if(!ethConnected) { isWiFiConnected(); - } else if(!ethConnected && !wifiConnected) { + } + if(!ethConnected && !wifiConnected) { isGprsConnected(); } DBG.printf("ethConnected:%d\r\n",ethConnected); @@ -3980,6 +3997,16 @@ pwrKey = 0; wait(2); } +void gprsRxInterrupt(void) { + char c; + + while(sim900.readable()) { + c = sim900.getc(); + if(c != 0) { //char is not null + gprsRxBuf += c; + } + } +} string gprsInit(string APN) { char r[128]; char s[128]; @@ -4058,6 +4085,184 @@ } } } +string gprsInitAnimate(string APN) { + bool waitAnimation = false; + char s[128]; + int n=0; + string str; + Timer t; + + //initialization //should be placed in main program + sim900.attach(&gprsRxInterrupt,SoftSerial::RxIrq); + + //AT+CIPMUX + waitAnimation = true; + while(waitAnimation) { + sim900.printf("AT+CIPMUX=1\r\n"); + for(int i=0; i<960; i+=4) { //960 is ~3sec + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray); + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue); + TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray); + TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray); + + if(gprsRxBuf.find("OK") != std::string::npos) { + waitAnimation = false; + } + } + //DBG.printf("rxBuf:%s\r\n",gprsRxBuf.c_str()); + if(n>=3) { + return "ERROR"; + } + n++; + } + + //APN + if(!APN.compare("Telkomsel")) { + strcpy(s,"AT+CSTT=\"telkomsel\",\"wap\",\"wap123\"\r\n"); + } else if(!APN.compare("Indosat")) { + strcpy(s,"AT+CSTT=\"indosatgprs\",\"indosat\",\"indosat\"\r\n"); + } else if(!APN.compare("XL")) { + strcpy(s,"AT+CSTT=\"www.xlgprs.net\",\"xlgprs\",\"proxl\"\r\n"); + } else if(!APN.compare("3")) { + strcpy(s,"AT+CSTT=\"3gprs\",\"3gprs\",\"3gprs\"\r\n"); + } else { + return "ERROR"; + } + + //AT+CSTT + waitAnimation = true; + n=0; + gprsRxBuf.clear(); + while(waitAnimation) { + sim900.printf("%s",s); + for(int i=0; i<1440; i+=4) { //1440 is ~5sec + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray); + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue); + TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray); + TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray); + + if(gprsRxBuf.find("OK") != std::string::npos) { + waitAnimation = false; + } + } + if(n>=3) { + return "ERROR"; + } + n++; + } + + //AT+CIICR + waitAnimation = true; + n=0; + gprsRxBuf.clear(); + while(waitAnimation) { + sim900.printf("AT+CIICR\r\n"); + for(int i=0; i<960; i+=4) { //960 is ~3sec + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray); + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue); + TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray); + TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray); + + if(gprsRxBuf.find("OK") != std::string::npos) { + waitAnimation = false; + } + } + if(n>=3) { + return "ERROR"; + } + n++; + } + + //AT+CIFSR + gprsRxBuf.clear(); + sim900.printf("AT+CIFSR\r\n"); + for(int i=0; i<960; i+=4) { //960 is ~3sec + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray); + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue); + TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray); + TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray); + } + if(gprsRxBuf.find("ERROR") != std::string::npos) { + return "ERROR"; + } else { + return gprsRxBuf; + } + + + /* + while(1) { + sim900.printf("AT+CIPMUX=1\r\n"); + gprsRcvReply(r,3000); + //DBG.printf("rspn[CIPMUX]:%s\r\n",r); + DBG.printf("AT+CIPMUX\r\n"); + str = r; + if(str.find("OK") != std::string::npos) { + break; + } + if(n>=3) { + return "ERROR"; + } + n++; + } + + if(!APN.compare("Telkomsel")) { + strcpy(s,"AT+CSTT=\"telkomsel\",\"wap\",\"wap123\"\r\n"); + } else if(!APN.compare("Indosat")) { + strcpy(s,"AT+CSTT=\"indosatgprs\",\"indosat\",\"indosat\"\r\n"); + } else if(!APN.compare("XL")) { + strcpy(s,"AT+CSTT=\"www.xlgprs.net\",\"xlgprs\",\"proxl\"\r\n"); + } else if(!APN.compare("3")) { + strcpy(s,"AT+CSTT=\"3gprs\",\"3gprs\",\"3gprs\"\r\n"); + } else { + return "ERROR"; + } + + n=0; + while(1) { + sim900.printf("%s",s); + gprsRcvReply(r,5000); + //DBG.printf("rspn[CSTT]:%s\r\n",r); + DBG.printf("AT+CSTT\r\n"); + str = r; + if(str.find("OK") != std::string::npos) { + break; + } + if(n>=3) { + return "ERROR"; + } + n++; + } + + n=0; + while(1) { + sim900.printf("AT+CIICR\r\n"); + gprsRcvReply(r,3000); + //DBG.printf("rspn[CIICR]:%s\r\n",r); + DBG.printf("AT+CIICR\r\n"); + str = r; + if(str.find("OK") != std::string::npos) { + break; + } + if(n>=3) { + return "ERROR"; + } + n++; + } + + while(1) { + sim900.printf("AT+CIFSR\r\n"); + gprsRcvReply(r,3000); + //DBG.printf("rspn[CIFSR]:%s\r\n",r); + DBG.printf("AT+CIFSR\r\n"); + str = r; + if(str.find("ERROR") != std::string::npos) { + return "ERROR"; + } else { + return str; + } + } + */ +} string gprsREST(string host, int port, string data) { char r[128]; char rLong[2048]; @@ -4122,6 +4327,97 @@ } return rLong; } +string gprsRESTAnimate(string host, int port, string data) { + bool waitAnimation = false; + int n=0; + string str; + Timer t; + + //initialization + //sim900.attach(&gprsRxInterrupt,SoftSerial::RxIrq); + + //AT+CIPSTART + waitAnimation = true; + + while(waitAnimation) { + sim900.printf("AT+CIPSTART=1,\"TCP\",\"%s\",%d\r\n",host.c_str(),port); + for(int i=0; i<2880; i+=4) { + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45, (i >> 1)+45, colorDarkGray); + TFT.fillarc(160, 130, 92, 4, (i >> 1)-45-4, (i >> 1)-45, Blue); + TFT.fillarc(160, 130, 95, 4, 1440-i-45, 1440-i+45, colorDarkGray); + TFT.fillarc(160, 130, 95, 4, 1440-i+45, 1440-i+45+4, colorLightGray); + + if(rxBuf.find("CONNECT OK") != std::string::npos) { + waitAnimation = false; + } + } + if(n>5) { + return "ERROR:CIPSTART"; //unable to start connection + } + n++; + } + + return "CONNECT OK"; //should be deleted + + /* + while(1) { + //sim900.printf("AT+CIPSTART=1,\"TCP\",\"%s\",%d\r\n",host.c_str(),port); + gprsRcvReply(r,3000); + //DBG.printf("rspn[CIPSTART]:%s",r); + DBG.printf("[CIPSTART]\r\n"); + str = r; + if(str.find("CONNECT OK") != std::string::npos) { + break; + } + if(n>5) { + return "ERROR:CIPSTART"; //unable to start connection + } + n++; + } + + //AT+CIPSEND + sim900.printf("AT+CIPSEND=1,%d\r\n",data.length()); + gprsRcvReply(r,1000); + //DBG.printf("rspn[CIPSEND]:%s",r); + DBG.printf("[CIPSEND]\r\n"); + + str = r; + if(str.find(">") != std::string::npos) { + sim900.printf("%s",data.c_str()); + gprsRcvReply(rLong,10000); + //DBG.printf("rspn[>]:%s\r\n",rLong); + DBG.printf("[>]\r\n"); + } + else { + return "ERROR:>"; //unable to send data + } + + //close connection (case for get request) + str = rLong; + if(str.find("CLOSED") != std::string::npos) { + return rLong; + } + + //close connection + n=0; + while(1) { + sim900.printf("AT+CIPCLOSE=1,1\r\n"); + gprsRcvReply(r,3000); + //DBG.printf("rspn[CIPCLOSE]:%s\r\n",r); + DBG.printf("[CIPCLOSE]\r\n"); + str = r; + if(str.find("CLOSE OK") != std::string::npos) { + break; + } + if(n>3) { + //return "ERROR:CIPCLOSE"; //unable to close connection + return rLong; //assumption if unable to close connection because it already been closed + } + n++; + } + return rLong; + */ +} void gprsRcvReply(char *r, int to) { Timer t; bool ended = false; @@ -4504,8 +4800,11 @@ int connPort; string str; + DBG.printf("isGprsConnected\r\n"); gprsReset(); - str = gprsInit(gprsAPN); + //str = gprsInit(gprsAPN); + str = gprsInitAnimate(gprsAPN); + //DBG.printf("str:%s\r\n",str.c_str()); if(str.find("ERROR") != std::string::npos) { gprsConnected = false; } else {