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 Emma

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers emmaCode.cpp Source File

emmaCode.cpp

00001 #include "emmaCode.h"
00002 
00003 //init debug port
00004 Serial DBG(PA_9, PA_10);    //tx, rx
00005 
00006 //init wifi port
00007 Serial _ESP(PA_2, PA_3);    //tx, rx
00008 //init espduino - without ch_pd pin
00009 ESP esp(&_ESP, &DBG, ESP_BAUD);
00010 //init wifi mqtt
00011 ESPMQTT mqtt(&esp);
00012 //init wifi rest
00013 REST rest(&esp);
00014 
00015 //init eth port
00016 SPI spi(PB_15, PB_14, PB_13);               //mosi, miso, sck
00017 MQTTEthernet ipstack(&spi, PB_12, PC_6);    //spi, cs, reset
00018 MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
00019 
00020 //init sd card
00021 SDFileSystem sd(PA_7, PA_6, PA_5, PB_3, "sd"); //mosi, miso, sck, cs
00022 
00023 //init ade7758 - without cs pin
00024 ADE7758 ADE(PB_6, PB_4, PB_5, PB_7);  //mosi, miso, sck, irq
00025 
00026 //init tft lcd
00027 SPI_TFT_ILI9341 TFT(PA_7, PA_6, PA_5, PA_4, PC_5, PC_4,"TFT");  //mosi, miso, sclk, cs, reset, dc
00028 //init touch screen - without cs pin
00029 TouchScreenADS7843 TP(PB_10, PB_0, PB_1, PB_2, &TFT);    //mosi, miso, sclk, irq, tft
00030 //InterruptIn tpIRQ(PB_2);
00031 
00032 //init dht sensor
00033 DHT11 d(PD_2);
00034 
00035 
00036 //emma settings
00037 string emmaUID;
00038 string hmac;
00039 string platformDOMAIN;
00040 string platformKEY;
00041 string platformSECRET;
00042 string wifiSSID;
00043 string wifiPASS;
00044 string gprsAPN;
00045 string proxySERVER;
00046 string proxyPORT;
00047 string proxyAUTH;
00048 
00049 //nodes settings
00050 class NODES {
00051 public:
00052     REST *restConn;
00053     NODES(REST *r);
00054     string macAddr;
00055     string ipAddr;
00056 };
00057 NODES::NODES(REST *r) {
00058     restConn = r;    
00059 }
00060 REST restObj[NODES_MAX] = {REST(&esp),REST(&esp),REST(&esp),REST(&esp),REST(&esp)};
00061 NODES nodes[NODES_MAX] = {NODES(&restObj[0]),NODES(&restObj[1]),NODES(&restObj[2]),NODES(&restObj[3]),NODES(&restObj[4])};
00062 
00063 //mode box class for main menu
00064 class modeBox {
00065 public:
00066     int xTL;    //TopLeft
00067     int yTL;
00068     int xBR;    //BottomRight
00069     int yBR;
00070     string text;    
00071 };
00072 
00073 //ade7758 variables
00074 uint32_t AWattHrValue, BWattHrValue, CWattHrValue;
00075 uint32_t AVAHrValue, BVAHrValue, CVAHrValue;
00076 //long AWattHrSum = 0;
00077 //long BWattHrSum = 0;
00078 //long CWattHrSum = 0;
00079 uint32_t AWattHrSum = 0;
00080 uint32_t BWattHrSum = 0;
00081 uint32_t CWattHrSum = 0;
00082 float AWattHr, BWattHr, CWattHr;
00083 float AVrms, BVrms, CVrms;
00084 float AIrms, BIrms, CIrms;
00085 float AWatt, BWatt, CWatt;
00086 float XWattHr,XVrms,XWatt;
00087 
00088 //variables
00089 bool ethAvailable = false;
00090 bool wifiAvailable = false;
00091 bool gprsAvailable = false;
00092 bool ethConnected = false;
00093 bool wifiConnected = false;
00094 bool gprsConnected = false;
00095 bool useProxy = false;
00096 bool newCommand = false;
00097 bool espFreeMemory = true;  //for after bootup initialization
00098 bool newEnergyData = false;
00099 string globalCommand;
00100 string rxBuf;
00101 
00102 /*start lcd and touch*/
00103 int emmaModeSelection(void) {
00104     bool modeSelected = false;
00105     int md=0;
00106     int TPx;
00107     int TPy;
00108     
00109     TFT.background(Black);
00110     TFT.foreground(White);
00111     
00112     TFT.set_font((unsigned char*) Arial12x12);
00113     TFT.set_orientation(1);
00114     TFT.cls();
00115     TFT.locate(0,0);
00116     TFT.printf("Hello, I'm Emma!");
00117     wait(2);
00118     TFT.cls();
00119     
00120     Matrix matrix;
00121     Coordinate ScreenSample[3];
00122     
00123     matrix.An = 580;
00124     matrix.Bn = 75980;
00125     matrix.Cn = -3410580;
00126     matrix.Dn = 57855;
00127     matrix.En = -2465;
00128     matrix.Fn = -3483515;
00129     matrix.Divider = 209144;
00130     
00131     ScreenSample[0].x = 230;
00132     ScreenSample[0].y = 167;
00133     ScreenSample[1].x = 754;
00134     ScreenSample[1].y = 163;
00135     ScreenSample[2].x = 771;
00136     ScreenSample[2].y = 562;
00137     
00138     TP.SetCalibration(&matrix, &ScreenSample[0]);
00139     
00140     //TFT.locate(0,0);
00141     //TFT.printf(" X:");
00142     //TFT.locate(70,0);
00143     //TFT.printf(" Y:");
00144     
00145     //draw border
00146     TFT.line(15,15,310,15,Orange);
00147     TFT.line(310,15,310,250,Orange);
00148     TFT.line(310,250,15,250,Orange);
00149     TFT.line(15,250,15,15,Orange);
00150     
00151     //init main menu
00152     modeBox menu[6];
00153     
00154     //wifi config mode
00155     menu[MODE_WIFI_CONFIG].xTL = 25;
00156     menu[MODE_WIFI_CONFIG].yTL = 25;
00157     menu[MODE_WIFI_CONFIG].xBR = 110;
00158     menu[MODE_WIFI_CONFIG].yBR = 90;
00159     menu[MODE_WIFI_CONFIG].text = "wifi config";
00160     
00161     //setting mode
00162     menu[MODE_SETTINGS].xTL = 120;
00163     menu[MODE_SETTINGS].yTL = 25;
00164     menu[MODE_SETTINGS].xBR = 205;
00165     menu[MODE_SETTINGS].yBR = 90;
00166     menu[MODE_SETTINGS].text = "settings";
00167     
00168     //register mode
00169     menu[MODE_REGISTER].xTL = 25;
00170     menu[MODE_REGISTER].yTL = 100;
00171     menu[MODE_REGISTER].xBR = 110;
00172     menu[MODE_REGISTER].yBR = 165;
00173     menu[MODE_REGISTER].text = "register";
00174     
00175     //operational mode
00176     menu[MODE_OPERATION].xTL = 120;
00177     menu[MODE_OPERATION].yTL = 100;
00178     menu[MODE_OPERATION].xBR = 205;
00179     menu[MODE_OPERATION].yBR = 165;
00180     menu[MODE_OPERATION].text = "operation";
00181     
00182     //firmware download mode
00183     menu[MODE_FIRMWARE_DOWNLOAD].xTL = 215;
00184     menu[MODE_FIRMWARE_DOWNLOAD].yTL = 25;
00185     menu[MODE_FIRMWARE_DOWNLOAD].xBR = 300;
00186     menu[MODE_FIRMWARE_DOWNLOAD].yBR = 90;
00187     menu[MODE_FIRMWARE_DOWNLOAD].text = "fw dwld";
00188     
00189     //reserved mode
00190     menu[5].xTL = 215;
00191     menu[5].yTL = 100;
00192     menu[5].xBR = 300;
00193     menu[5].yBR = 165;
00194     menu[5].text = "reserved";
00195     
00196     //draw main menu
00197     for(int i=0; i<6; i++) {
00198         TFT.fillrect(menu[i].xTL,menu[i].yTL,menu[i].xBR,menu[i].yBR,Orange);    
00199     }
00200     
00201     //add text to main menu
00202     for(int i=0; i<6; i++) {
00203         TFT.locate(menu[i].xTL,menu[i].yTL);
00204         TFT.printf("%s",menu[i].text.c_str());    
00205     }
00206     
00207     while(!modeSelected) {
00208         if(!TP._tp_irq) {
00209             if(TP.Read_Ads7843()) {
00210                 TP.getDisplayPoint();
00211                 TPx = TP.display.x;
00212                 TPy = TP.display.y;
00213                 TP.TP_DrawPoint(TPx,TPy, Blue);
00214                 //TFT.locate(25,0);
00215                 //TFT.printf("%03d",TPx);
00216                 //TFT.locate(95,0);
00217                 //TFT.printf("%03d",TPy);
00218                 
00219                 for(int i=0; i<6; i++) {
00220                     if((menu[i].xTL < TPx && TPx < menu[i].xBR) && (menu[i].yTL < TPy && TPy < menu[i].yBR)) {
00221                         //TFT.locate(25,170);
00222                         //TFT.printf("                         ");
00223                         //TFT.locate(25,170);
00224                         //TFT.printf("mode: %s is selected",menu[i].text.c_str());
00225                         //wait(3);
00226                         md = i;
00227                         modeSelected = true;
00228                     }
00229                 }
00230             }
00231         }    
00232     }
00233     
00234     TFT.locate(25,170);
00235     TFT.printf("                         ");
00236     TFT.locate(25,170);
00237     TFT.printf("mode: %s is selected",menu[md].text.c_str());
00238     wait(2);
00239     TFT.cls();
00240     
00241     return md;
00242 }
00243 /*end lcd and touch*/
00244 
00245 /*start emma mode*/
00246 void emmaInit(int mode) {
00247     char s[64];
00248     DBG.baud(19200);
00249     DBG.printf("\r\nemmaInit\r\n");
00250     DBG.printf("mode:%d\r\n",mode);
00251     
00252     //read settings
00253     //readSetting("emmaUID");         //sd card need to be read once before working correctly
00254     emmaUID = readSetting("emmaUID");
00255     //emmaUID = "066eff575349896767073038";
00256     DBG.printf("emmaUID:%s\r\n",emmaUID.c_str());
00257     //calculate hmac
00258     for(int i=0; i<sizeof(s); i++) {
00259         s[i]=0; }
00260     sprintf(s,"emma-%s",emmaUID.c_str());   
00261     hmac = calculateMD5(s);
00262     DBG.printf("hmac:%s\r\n",hmac.c_str());
00263     platformDOMAIN = readSetting("platformDOMAIN");
00264     //platformDOMAIN = "testdulu";
00265     DBG.printf("platformDOMAIN:%s\r\n",platformDOMAIN.c_str());
00266     platformKEY = readSetting("platformKEY");
00267     //platformKEY = "5980e444-81dd-47ba-8222-6a40bc94fdce";
00268     DBG.printf("platformKEY:%s\r\n",platformKEY.c_str());
00269     platformSECRET = readSetting("platformSECRET");
00270     //platformSECRET = "3ca8ec0239fda2b6d12ba1580c91a052";
00271     DBG.printf("platformSECRET:%s\r\n",platformSECRET.c_str());
00272     proxySERVER = readSetting("proxySERVER");
00273     DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str());
00274     proxyPORT = readSetting("proxyPORT");
00275     DBG.printf("proxyPORT:%s\r\n",proxyPORT.c_str());
00276     proxyAUTH = readSetting("proxyAUTH");
00277     DBG.printf("proxyAUTH:%s\r\n",proxyAUTH.c_str());
00278     
00279     //check proxy
00280     if(!proxySERVER.empty() && !proxyPORT.empty() && !proxyAUTH.empty()) {
00281         useProxy = true;
00282     } else {
00283         useProxy = false;    
00284     }
00285     //testing purpose
00286     useProxy = false;
00287     DBG.printf("proxy:%d\r\n",useProxy);
00288     
00289     //check available interface
00290     isEthAvailable();           //check whether cable is connected
00291     wifiAvailable = true;       //we assume wifi will always available
00292     DBG.printf("eth:%d\r\n",ethAvailable);
00293     DBG.printf("wifi:%d\r\n",wifiAvailable);
00294     DBG.printf("gprs:%d\r\n",gprsAvailable);
00295 }
00296 void emmaModeWiFiConfig(void) {
00297     string str;
00298     TFT.locate(0,0);
00299     TFT.printf(" please wait");
00300     MbedJSONValue jsonValue;    
00301     
00302     if(wifiAvailable) {
00303         DBG.printf("emmaModeWiFiConfig\r\n");
00304         
00305         //set wifi module to configuration
00306         _ESP.printf("MODE=C");
00307         while(1) {
00308             char rcv[128] = {};
00309             rcvReply(rcv,3000);
00310             str = rcv;
00311             if(str.find("SC_STATUS_FIND_CHANNEL") != std::string::npos)
00312                 break;
00313         }
00314         
00315         TFT.locate(0,0);
00316         TFT.printf(" emmaModeWiFiConfig");
00317         TFT.locate(0,20);
00318         TFT.printf(" connect with emma app now");
00319         
00320         DBG.printf("entering wifi configuration mode\r\n");
00321         while(1) {
00322             char rcv[128] = {};
00323             rcvReply(rcv,3000);
00324             str = rcv;
00325             if(str.find("MODE=C OK") != std::string::npos) {
00326                 //save wifiSSID and wifiPASS
00327                 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00328                     str.erase(str.begin(),str.begin()+str.find("[")+1);
00329                     str.erase(str.begin()+str.find("]"),str.end());
00330                     
00331                     parse(jsonValue,str.c_str());
00332                         
00333                     const char *parameter[2] = {"wifiSSID","wifiPASS"};
00334                         
00335                     for(int i=0; i<2; i++) {
00336                         if(jsonValue.hasMember(parameter[i])) {
00337                             string val = jsonValue[parameter[i]].get<std::string>();
00338                             int st = writeSetting(parameter[i],val.c_str());
00339                             if(st) {
00340                                 DBG.printf("%s is saved\r\n",parameter[i]);
00341                                 TFT.locate(0,40);
00342                                 TFT.printf(" %s is saved\r\n",parameter[i]);
00343                                 wait(3);
00344                                 TFT.locate(0,40);
00345                                 TFT.printf("                              ");
00346                             } else {
00347                                 DBG.printf("%s is not saved\r\n",parameter[i]);
00348                                 TFT.locate(0,40);
00349                                 TFT.printf(" %s is not saved\r\n",parameter[i]);
00350                                 wait(3);
00351                                 TFT.locate(0,40);
00352                                 TFT.printf("                              ");
00353                             }
00354                         }
00355                     }
00356                     
00357                     //wificonfig finish
00358                     TFT.locate(0,20);
00359                     TFT.printf("                                             ");
00360                     TFT.locate(0,20);
00361                     TFT.printf(" wificonfig finish. please restart.");
00362                     
00363                 } 
00364             } else if(str.find("SC_STATUS_GETTING_SSID_PSWD") != std::string::npos){
00365                 DBG.printf("app connected\r\n");
00366                 TFT.locate(0,20);
00367                 TFT.printf("                              ");
00368                 TFT.locate(0,20);
00369                 TFT.printf(" app connected");
00370             }
00371         }    
00372     } else {
00373         DBG.printf("no wifi found\r\n");
00374         TFT.locate(0,20);
00375         TFT.printf(" no wifi found");
00376     }
00377 }
00378 void emmaModeSettings(void) {
00379     bool clientIsConnected = false;
00380     bool serverIsListened = false;
00381     char s[32];
00382     string str;
00383     MbedJSONValue jsonValue;
00384     
00385     TFT.locate(0,0);
00386     TFT.printf(" please wait");
00387     
00388     //create settings dir
00389     mkdir("/sd/settings",0777);
00390     
00391     //get and write emmaUID
00392     string uid = getUID();
00393     sprintf(s,"(%s)",uid.c_str());
00394     uid = s;
00395     writeSetting("emmaUID",uid);
00396     
00397     if(ethAvailable) {
00398         DBG.printf("emmaModeSettings - eth\r\n");
00399         
00400         TCPSocketServer svr;
00401         TCPSocketConnection clientSock;
00402     
00403         if(svr.bind(SERVER_PORT) < 0) {
00404             DBG.printf("tcp server bind failed\r\n");    
00405         } else {
00406             DBG.printf("tcp server bind success\r\n");
00407             serverIsListened = true;    
00408         }
00409     
00410         DBG.printf("please connect to %s\r\n",ipstack.getEth().getIPAddress());
00411         
00412         if(svr.listen(1) < 0) {
00413             DBG.printf("tcp server listen failed\r\n");    
00414         } else {
00415             DBG.printf("tcp server is listening...\r\n");    
00416         }
00417     
00418         clientSock.set_blocking(false,30000);   //timeout after 30sec
00419     
00420         //listening
00421         while (serverIsListened) {
00422             if(svr.accept(clientSock) < 0) {
00423                 DBG.printf("failed to accept connection\r\n");    
00424             } else {
00425                 DBG.printf("connection success!\r\nIP: %s\r\n",clientSock.get_address());
00426                 clientIsConnected = true;
00427             
00428                 while(clientIsConnected) {
00429                     char buffer[1024] = {};
00430                     switch(clientSock.receive(buffer,1023)) {
00431                         case 0:
00432                             DBG.printf("received buffer is empty\r\n");
00433                             clientIsConnected = false;
00434                             break;
00435                         case -1:
00436                             DBG.printf("failed to read data from client\r\n");
00437                             clientIsConnected = false;
00438                             break;
00439                         default:
00440                             //DBG.printf("received data: %d\r\n%s\r\n",strlen(buffer),buffer);
00441                             DBG.printf("\r\n");
00442                         
00443                             str = buffer;
00444                             if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00445                                 str.erase(str.begin(),str.begin()+str.find("[")+1);
00446                                 str.erase(str.begin()+str.find("]"),str.end());
00447                             
00448                                 parse(jsonValue,str.c_str());
00449                         
00450                                 const char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
00451                         
00452                                 for(int i=0; i<4; i++) {
00453                                     if(jsonValue.hasMember(parameter[i])) {
00454                                         string val = jsonValue[parameter[i]].get<std::string>();
00455                                         int st = writeSetting(parameter[i],val.c_str());
00456                                         if(st) {
00457                                             DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
00458                                         } else {
00459                                             DBG.printf("%s is not saved\r\n",parameter[i]);
00460                                         }
00461                                     }
00462                                 }
00463                                 
00464                                 //set time
00465                                 if(jsonValue.hasMember(parameter[4])) {
00466                                     string epTime = jsonValue[parameter[4]].get<std::string>();
00467                                     time_t seconds;
00468                                     sscanf(epTime.c_str(),"%d",&seconds);
00469                                     set_time(seconds);
00470                                     DBG.printf("time is set\r\n");
00471                                 }
00472                             }
00473                             break;     
00474                     }    
00475                 }
00476                 DBG.printf("close connection\r\n");
00477                 clientSock.close();
00478             }
00479         }
00480     } else if(wifiAvailable) {
00481         DBG.printf("emmaModeSettings - wifi\r\n");
00482         
00483         _ESP.printf("MODE=S");
00484         while(1) {
00485             char rcv[128] = {};
00486             rcvReply(rcv,3000);
00487             str = rcv;
00488             if(str.find("MODE=S_OK") != std::string::npos)
00489                 break;
00490         }
00491         DBG.printf("entering settings mode\r\n");
00492         TFT.locate(0,0);
00493         TFT.printf(" emmaModeSettings");
00494         TFT.locate(0,20);
00495         TFT.printf(" connect with emma app now");
00496         
00497         while(1) {
00498             char rcv[512] = {};
00499             rcvReply(rcv,3000);
00500             //DBG.printf("rcv:%s\r\n",rcv);
00501             str = rcv;
00502             if(str.find("MODE=S_Config") != std::string::npos) {
00503                 //save gprs and proxy setting
00504                 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00505                     str.erase(str.begin(),str.begin()+str.find("[")+1);
00506                     str.erase(str.begin()+str.find("]"),str.end());
00507                             
00508                     parse(jsonValue,str.c_str());
00509                         
00510                     const char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
00511                         
00512                     for(int i=0; i<4; i++) {
00513                         if(jsonValue.hasMember(parameter[i])) {
00514                             string val = jsonValue[parameter[i]].get<std::string>();
00515                             int st = writeSetting(parameter[i],val.c_str());
00516                             if(st) {
00517                                 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
00518                                 TFT.locate(0,40);
00519                                 TFT.printf(" %s: %s is saved",parameter[i],val.c_str());
00520                                 wait(3);
00521                                 TFT.locate(0,40);
00522                                 TFT.printf("                                             ");
00523                             } else {
00524                                 DBG.printf("%s is not saved\r\n",parameter[i]);
00525                                 TFT.locate(0,40);
00526                                 TFT.printf(" %s is not saved",parameter[i]);
00527                                 wait(3);
00528                                 TFT.locate(0,40);
00529                                 TFT.printf("                                             ");
00530                             }
00531                         }
00532                     }
00533                     
00534                     //set time
00535                     if(jsonValue.hasMember(parameter[4])) {
00536                         string epTime = jsonValue[parameter[4]].get<std::string>();
00537                         time_t seconds;
00538                         sscanf(epTime.c_str(),"%d",&seconds);
00539                         set_time(seconds);
00540                         DBG.printf("time is set\r\n");
00541                         TFT.locate(0,40);
00542                         TFT.printf(" time is set");
00543                         wait(3);
00544                         TFT.locate(0,40);
00545                         TFT.printf("                                             ");
00546                     }
00547                     
00548                     //setting finish
00549                     TFT.locate(0,20);
00550                     TFT.printf("                                             ");
00551                     TFT.locate(0,20);
00552                     TFT.printf(" settings finish. please restart.");    
00553                 }    
00554             } else if(str.find("connect") != std::string::npos) {
00555                 DBG.printf("connection success!\r\n");
00556                 TFT.locate(0,20);
00557                 TFT.printf("                                             ");
00558                 TFT.locate(0,20);
00559                 TFT.printf(" connection success");
00560             }
00561         }
00562     } else {
00563         DBG.printf("no eth or wifi is available\r\n");
00564         TFT.locate(0,0);
00565         TFT.printf(" no iface avail, please restart!");
00566     }
00567 }
00568 void emmaModeRegister(void) {
00569     bool emmaGetRegKey = false;
00570     bool emmaRegistered = false;
00571     char s[512];
00572     char r[256];
00573     int connPort;
00574     int loop = 0;
00575     string connData;
00576     string connHost;
00577     //string hmac;
00578     string str;
00579     string regKey;
00580     Timer t;
00581     MbedJSONValue jsonValue;
00582     
00583     TFT.locate(0,0);
00584     TFT.printf(" please wait");
00585     useProxy = true;
00586     //check connected interface
00587     //connectedIface();
00588     isEthConnected();
00589     isWiFiConnected();
00590     isGprsConnected();
00591     DBG.printf("ethConnected:%d\r\n",ethConnected);
00592     DBG.printf("wifiConnected:%d\r\n",wifiConnected);
00593     
00594     //calculate hmac
00595     //for(int i=0; i<sizeof(s); i++) {
00596     //    s[i]=0; }
00597     //sprintf(s,"emma-%s",emmaUID.c_str());   
00598     //hmac = calculateMD5(s);
00599     //DBG.printf("hmac:%s\r\n",hmac.c_str());
00600     
00601     if(ethConnected) {
00602         DBG.printf("emmaModeRegister - eth\r\n");
00603         
00604         //set connHost, connPort, connData
00605         if(useProxy) {
00606             DBG.printf("use proxy\r\n");
00607             connHost = proxySERVER;
00608             sscanf(proxyPORT.c_str(),"%d",&connPort);
00609             for(int i=0; i<sizeof(s); i++) {
00610                 s[i]=0; }
00611             sprintf(s,"GET http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
00612             connData = s;
00613         } else {
00614             DBG.printf("no proxy\r\n");
00615             connHost = EMMA_SERVER_HOST;
00616             connPort = EMMA_SERVER_PORT;
00617             for(int i=0; i<sizeof(s); i++) {
00618                 s[i]=0; }
00619             sprintf(s,"GET /emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
00620             connData = s;
00621         }
00622         
00623         //register
00624         while(!emmaGetRegKey) {
00625             str = "";
00626             str = ethGET(connHost,connPort,connData);
00627             DBG.printf("rsp reg:%s\r\n",str.c_str());
00628 
00629             //check and save platform setting
00630             if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00631                 str.erase(str.begin(),str.begin()+str.find("[")+1);
00632                 str.erase(str.begin()+str.find("]"),str.end());
00633         
00634                 parse(jsonValue,str.c_str());
00635     
00636                 const char *parameter[4] = {"platformDOMAIN","platformKEY","platformSECRET","registrationKey"};
00637     
00638                 //save platform parameter
00639                 writeSetting(parameter[0],"()");    //sd card need to be initialized
00640                 for(int i=0; i<3; i++) {
00641                     if(jsonValue.hasMember(parameter[i])) {
00642                         string val = jsonValue[parameter[i]].get<std::string>();
00643                         int st = writeSetting(parameter[i],val.c_str());
00644                         if(st) {
00645                             DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
00646                         } else {
00647                             DBG.printf("%s is not saved\r\n",parameter[i]);
00648                         }
00649                     }
00650                 }
00651         
00652                 //get registrationKey
00653                 if(jsonValue.hasMember(parameter[3])) {
00654                     string val = jsonValue[parameter[3]].get<std::string>();
00655                     if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
00656                         val.erase(val.begin(),val.begin()+val.find("(")+1);
00657                         val.erase(val.begin()+val.find(")"),val.end());
00658                         regKey = val;
00659                         DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
00660                         emmaGetRegKey = true;
00661                     }
00662                 }
00663             }
00664         }
00665         
00666         //calculate hmac
00667         for(int i=0; i<sizeof(r); i++) {
00668             r[i]=0; }
00669         sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
00670         hmac = calculateMD5(r);
00671         DBG.printf("hmac:%s\r\n",hmac.c_str());
00672         
00673         //set connData
00674         if(useProxy) {
00675             DBG.printf("use proxy\r\n");
00676             for(int i=0; i<sizeof(s); i++) {
00677                 s[i]=0; }
00678             sprintf(s,"GET http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
00679             connData = s;
00680         } else {
00681             DBG.printf("no proxy\r\n");
00682             for(int i=0; i<sizeof(s); i++) {
00683                 s[i]=0; }
00684             sprintf(s,"GET /emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
00685             connData = s;
00686         }
00687         
00688         //verify registration
00689         while(!emmaRegistered && loop < 12){
00690             str.clear();
00691             str = ethGET(connHost,connPort,connData);
00692             DBG.printf("rsp vrf:%s\r\n",str.c_str());
00693                 
00694             //check verification
00695             if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00696                 str.erase(str.begin(),str.begin()+str.find("[")+1);
00697                 str.erase(str.begin()+str.find("]"),str.end());
00698                     
00699                 parse(jsonValue,str.c_str());
00700                 if(jsonValue.hasMember("user")) {
00701                     string val = jsonValue["user"].get<std::string>();
00702                     DBG.printf("%s is registered\r\n",val.c_str());
00703                     emmaRegistered = true;
00704                 }
00705             }
00706             wait(5);
00707             loop++;
00708         }
00709             
00710         //check whether registration success
00711         if(emmaRegistered) {
00712             DBG.printf("registration successful\r\n");
00713         } else {
00714             DBG.printf("registration unsuccessful\r\n");
00715         }
00716         while(1);
00717         
00718     } else if(wifiConnected) {
00719         DBG.printf("emmaModeRegister - wifi\r\n");
00720         
00721         _ESP.printf("MODE=B");
00722         wait(1);
00723         while(!esp.ready());
00724         
00725         //set connHost, connPort
00726         if(useProxy) {
00727             connHost = proxySERVER;
00728             sscanf(proxyPORT.c_str(),"%d",&connPort);
00729         } else {
00730             connHost = EMMA_SERVER_HOST;
00731             connPort = EMMA_SERVER_PORT;
00732         }
00733         TFT.locate(0,0);
00734         TFT.printf(" emmaModeRegister");
00735         
00736         //rest begin
00737         if(!rest.begin(connHost.c_str(),connPort,false)) {
00738             DBG.printf("EMMA: fail to setup rest");
00739             while(1);    
00740         }
00741         //wifiConnected = true;   //with custom firmware, wifi module should connect automatically
00742         
00743         esp.process();
00744         if(wifiConnected) {
00745             //set connData
00746             if(useProxy) {
00747                 for(int i=0; i<sizeof(s); i++) {
00748                     s[i]=0; }
00749                 sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str());
00750                 connData = s;
00751             } else {
00752                 for(int i=0; i<sizeof(s); i++) {
00753                     s[i]=0; }
00754                 sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
00755                 connData = s;
00756             }
00757          
00758             //register
00759             while(!emmaGetRegKey) {
00760                 rest.get(connData.c_str());
00761                 for(int i=0; i<sizeof(s); i++) {
00762                     s[i]=0; }
00763                 rest.getResponse(s,sizeof(s));
00764                 DBG.printf("rsp reg:%s\r\n",s);
00765 
00766                 //check and save platform setting
00767                 str = s;
00768                 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00769                     str.erase(str.begin(),str.begin()+str.find("[")+1);
00770                     str.erase(str.begin()+str.find("]"),str.end());
00771         
00772                     parse(jsonValue,str.c_str());
00773     
00774                     const char *parameter[4] = {"platformDOMAIN","platformKEY","platformSECRET","registrationKey"};
00775     
00776                     //save platform parameter
00777                     writeSetting(parameter[0],"()");    //sd card need to be initialized
00778                     for(int i=0; i<3; i++) {
00779                         if(jsonValue.hasMember(parameter[i])) {
00780                             string val = jsonValue[parameter[i]].get<std::string>();
00781                             int st = writeSetting(parameter[i],val.c_str());
00782                             if(st) {
00783                                 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
00784                             } else {
00785                                 DBG.printf("%s is not saved\r\n",parameter[i]);
00786                             }
00787                         }
00788                     }
00789         
00790                     //get registrationKey
00791                     if(jsonValue.hasMember(parameter[3])) {
00792                         string val = jsonValue[parameter[3]].get<std::string>();
00793                         if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
00794                             val.erase(val.begin(),val.begin()+val.find("(")+1);
00795                             val.erase(val.begin()+val.find(")"),val.end());
00796                             regKey = val;
00797                             DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
00798                             
00799                             TFT.locate(0,20);
00800                             TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
00801                             
00802                             emmaGetRegKey = true;
00803                         }
00804                     }
00805                 }
00806             }
00807                 
00808             //calculate hmac
00809             for(int i=0; i<sizeof(r); i++) {
00810                 r[i]=0; }
00811             sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
00812             hmac = calculateMD5(r);
00813             DBG.printf("hmac:%s\r\n",hmac.c_str());
00814             
00815             //set connData
00816             if(useProxy) {
00817                 for(int i=0; i<sizeof(s); i++) {
00818                     s[i]=0; }
00819                 sprintf(s,"http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str());
00820                 connData = s;
00821             } else {
00822                 for(int i=0; i<sizeof(s); i++) {
00823                     s[i]=0; }
00824                 sprintf(s,"/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
00825                 connData = s;
00826             }
00827                 
00828             //verify registration
00829             while(!emmaRegistered && loop < 12){
00830                 rest.get(connData.c_str());
00831                 for(int i=0; i<sizeof(s); i++) {
00832                     s[i]=0; }
00833                 rest.getResponse(s,sizeof(s));
00834                 DBG.printf("rsp vrf:%s\r\n",s);
00835                 TFT.locate(0,40);
00836                 TFT.printf("                              ");
00837                 TFT.locate(0,40);
00838                 TFT.printf(" wait:%d\r\n",loop);
00839                 
00840                 //check verification
00841                 str = s;
00842                 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
00843                     str.erase(str.begin(),str.begin()+str.find("[")+1);
00844                     str.erase(str.begin()+str.find("]"),str.end());
00845                     
00846                     parse(jsonValue,str.c_str());
00847                     if(jsonValue.hasMember("user")) {
00848                         string val = jsonValue["user"].get<std::string>();
00849                         DBG.printf(" %s is registered\r\n",val.c_str());
00850                         TFT.locate(0,40);
00851                         TFT.printf("                              ");
00852                         TFT.locate(0,40);
00853                         TFT.printf(" %s is registered\r\n",val.c_str());
00854                         emmaRegistered = true;
00855                     }
00856                 }
00857                 wait(5);
00858                 loop++;
00859             }
00860             
00861             //check whether registration success
00862             if(emmaRegistered) {
00863                 DBG.printf("registration successful\r\n");
00864                 TFT.locate(0,60);
00865                 TFT.printf(" registration successful\r\n");
00866             } else {
00867                 DBG.printf("registration unsuccessful\r\n");
00868                 TFT.locate(0,60);
00869                 TFT.printf(" egistration unsuccessful. please restart.\r\n");
00870             }
00871             while(1);
00872         }
00873         
00874     } else if(gprsConnected) {
00875         DBG.printf("emmaModeRegister - gprs\r\n");
00876         
00877     } else {
00878         DBG.printf("no eth, wifi, or gprs is connected\r\n");
00879         TFT.locate(0,60);
00880         TFT.printf(" no iface connected. please restart.\r\n");
00881     }
00882 }
00883 
00884 void emmaModeOperation(void) {
00885     char mqttClientId[32];
00886     char p[64];
00887     char q[32];
00888     char r[32];
00889     char s[512];
00890     int loop=0;
00891     int trial=0;
00892     //string hmac;
00893     string hmacTime;
00894     string hmacCmd;
00895     string str;
00896     time_t seconds;
00897     Timer t;
00898     Timer tPanelEnergy;
00899     Timer tPanel;
00900     Timer tNodes;
00901     MbedJSONValue jsonValue;
00902      
00903     TFT.locate(0,0);
00904     TFT.printf(" please wait");
00905     
00906     //check connected interface
00907     //connectedIface();
00908     DBG.printf("EMMA OPERATION MODE\r\n");
00909     isEthConnected();
00910     isWiFiConnected();
00911     isGprsConnected();
00912     DBG.printf("ethConnected:%d\r\n",ethConnected);
00913     DBG.printf("wifiConnected:%d\r\n",wifiConnected);
00914     
00915     TFT.locate(0,0);
00916     TFT.printf(" emmaModeOperation");
00917     
00918     TFT.locate(0,20);
00919     if(ethConnected) {
00920         TFT.printf("ETH:Avail");       
00921     } else {
00922         TFT.printf("ETH:N/A");
00923     }
00924     
00925     TFT.locate(80,20);
00926     if(wifiConnected) {
00927         TFT.printf("WiFi:Avail");       
00928     } else {
00929         TFT.printf("WiFi:N/A");
00930     }
00931     
00932     TFT.locate(160,20);
00933     if(gprsConnected) {
00934         TFT.printf("GPRS:Avail");       
00935     } else {
00936         TFT.printf("GPRS:N/A");
00937     }
00938     
00939     //calculate hmac
00940     //for(int j=0; j<sizeof(s); j++) {
00941     //    s[j]=0; }
00942     //sprintf(s,"emma-%s",emmaUID.c_str());   
00943     //hmac = calculateMD5(s);
00944     //DBG.printf("hmac:%s\r\n",hmac.c_str());
00945     int count = 0;
00946     if(ethConnected) {
00947         DBG.printf("emmaModeOperation - eth\r\n");
00948         DBG.printf("IP Address:%s\r\n",ipstack.getEth().getIPAddress());
00949         DBG.printf("MAC Address:%s\r\n",ipstack.getEth().getMACAddress());
00950         ethMQTTAttemptConnect(&client, &ipstack);
00951 //        t.start();
00952 //        DBG.printf("start\r\n");
00953         while(true) {
00954             if(!ipstack.getEth().linkstatus()) {
00955                 NVIC_SystemReset();  
00956             }
00957             
00958             if (++count == 200)
00959             {               // Publish a message every second
00960                 if (publish(&client, &ipstack) != 0)
00961                     ethMQTTAttemptConnect(&client, &ipstack);   // if we have lost the connection
00962                 count = 0;
00963             }
00964             
00965             // DBG.printf("Yielding...\r\n");            
00966             client.yield(100);  //allow MQTT client to receive message
00967             // DBG.printf("Yield finished\r\n");
00968         }
00969     } else if(wifiConnected) {
00970         DBG.printf("emmaModeOperation - wifi\r\n");
00971     
00972         //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work
00973         //_ESP.printf("MODE=B");
00974         //wait(1);
00975         //while(!esp.ready());
00976 
00977         /*
00978         DBG.printf("emma: setup mqtt client\r\n");
00979         sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
00980     
00981         if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
00982             mqtt.connectedCb.attach(&mqttConnected);
00983             mqtt.disconnectedCb.attach(&mqttDisconnected);
00984             mqtt.connect(MQTT_HOST,MQTT_PORT,false);
00985             DBG.printf("emma: success to setup mqtt\r\n");
00986             TFT.locate(0,40);
00987             TFT.printf("emma: success to setup mqtt");    
00988         }
00989         DBG.printf("emma: system started\r\n");
00990         */
00991         
00992         //t.start();
00993         //while(t.read_ms() < 5000) {
00994         //    esp.process();    
00995         //}
00996         //t.stop();
00997         //t.reset();
00998         
00999         //set ade7758 parameter
01000         ADE.begin();
01001         //ADE.AVRMSCalib = 1526873.00;
01002         //ADE.BVRMSCalib = 534202.00;
01003         //ADE.CVRMSCalib = 456990.00;
01004         //ADE.AIRMSCalib = 39248.00;
01005         //ADE.BIRMSCalib = 654.00;
01006         //ADE.CIRMSCalib = 111.00;
01007         
01008         ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET);
01009         
01010         ADE.write16bits(AWG, 0);
01011         ADE.write16bits(BWG, 0);
01012         ADE.write16bits(CWG, 0);
01013         ADE.write16bits(AVAG, 0);
01014         ADE.write16bits(BVAG, 0);
01015         ADE.write16bits(CVAG, 0);
01016         
01017         //ADE.AWhLSB = 0.000001192;//0.00006025556;
01018         //ADE.AWhLSB = 0.00002109;
01019         //ADE.BWhLSB = 0.25075167;
01020         //ADE.CWhLSB = 0.25075167;
01021         
01022         //ADE.AVAhLSB = 0.00008370;
01023         //ADE.BVAhLSB = 0;
01024         //ADE.CVAhLSB = 0;
01025         
01026         //init rest to server
01027         if(rest.begin(EMMA_SERVER_HOST,EMMA_SERVER_PORT,false)) {
01028             DBG.printf("rest to server is created\r\n");
01029             TFT.locate(0,40);
01030             TFT.printf("                                        ");
01031             TFT.locate(0,40);
01032             TFT.printf("rest to server is created");
01033         } else {
01034             DBG.printf("rest to server is NOT created\r\n");
01035             TFT.locate(0,40);
01036             TFT.printf("                                        ");
01037             TFT.locate(0,40);
01038             TFT.printf("rest to server is NOT created");
01039         }
01040         
01041         //check firmware update
01042 
01043         //execute last state of switches on board
01044     
01045         //get list of nodes from server
01046         sprintf(s,"/emma/api/controller/remotes?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
01047         rest.get(s);
01048         for(int i=0; i<sizeof(s); i++) {
01049             s[i]=0; }
01050         rest.getResponse(s,sizeof(s));
01051         str = s;
01052         if(str.rfind("[{\"mac\"") != std::string::npos) {
01053             DBG.printf("get nodes from server\r\n");
01054             str.erase(str.begin(),str.begin()+str.rfind("[{\"mac\""));
01055             str.erase(str.begin()+str.rfind("}]")+2,str.end());
01056             
01057             parse(jsonValue,str.c_str());
01058             const char *parameter[2] = {"mac","ip"};
01059             
01060             TFT.locate(0,40);
01061             TFT.printf("                                        ");
01062             TFT.locate(0,40);
01063             TFT.printf("get %d nodes from server",jsonValue.size());
01064             wait(0.5);
01065             TFT.locate(0,40);
01066             TFT.printf("                                        ");
01067             
01068             //check whether nodes valid
01069             bool validNodes = true;
01070             for(int i=0; i<jsonValue.size(); i++) {
01071                 for(int j=0; j<2; j++) {
01072                     validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
01073                 }    
01074             }
01075             DBG.printf("nodes validity:%d\r\n",validNodes);
01076             
01077             if(validNodes) {
01078                 for(int i=0; i<jsonValue.size(); i++) {
01079                     string macValue = jsonValue[i][parameter[0]].get<std::string>();
01080                     string ipValue = jsonValue[i][parameter[1]].get<std::string>();
01081                     nodes[i].macAddr = macValue;
01082                     nodes[i].ipAddr = ipValue;
01083                     DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
01084                     DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
01085                 }
01086             }
01087             
01088         } else {
01089             DBG.printf("no nodes from server\r\n");    
01090         }
01091         
01092         /*
01093         //success
01094         DBG.printf("emma: setup mqtt client\r\n");
01095         sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
01096         if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
01097             mqtt.connectedCb.attach(&mqttConnected);
01098             mqtt.disconnectedCb.attach(&mqttDisconnected);
01099             mqtt.connect(MQTT_HOST,MQTT_PORT,false);
01100             DBG.printf("emma: success to setup mqtt\r\n");
01101             TFT.locate(0,40);
01102             TFT.printf("emma: success to setup mqtt");    
01103         }
01104         DBG.printf("emma: system started\r\n");
01105         
01106         t.start();
01107         while(t.read_ms() < 5000) {
01108             esp.process();    
01109         }
01110         t.stop();
01111         t.reset();
01112         */
01113         
01114         //preset nodes' macAddr and ipAddr
01115         //nodes[0].macAddr = "002629034222";
01116         //nodes[0].ipAddr = "192.168.2.15";
01117         //nodes[1].macAddr = "00262903424e";
01118         //nodes[1].ipAddr = "192.168.2.32";
01119         
01120         //init rest to remotes
01121         for(int i=0; i<NODES_MAX; i++) {
01122             if(!nodes[i].ipAddr.empty()) {
01123                 DBG.printf("restConn nodes[%d] is created\r\n",i);
01124                 nodes[i].restConn->begin(nodes[i].ipAddr.c_str(),16038,false);
01125                 wait(1);
01126             } else {
01127                 DBG.printf("restConn nodes[%d] is NOT created\r\n",i);
01128                 wait(1);
01129             }
01130         }
01131         
01132         _ESP.attach(&rxInterrupt,Serial::RxIrq);
01133         
01134         //define thread
01135         osThreadDef(energyThread, osPriorityBelowNormal, (8*DEFAULT_STACK_SIZE));
01136         //create thread
01137         osThreadCreate(osThread(energyThread),NULL);
01138         
01139         tPanelEnergy.start();
01140         tPanel.start();
01141         tNodes.start();
01142         wait(1);
01143         while(1) {
01144             checkRxBuffer();
01145             checkVoltagePower();
01146             
01147             //whether espFreeMemory occurs
01148             if(espFreeMemory) {
01149                 //success if rxInterrupt is disabled
01150                 
01151                 //disable UART2
01152                 NVIC_DisableIRQ(USART2_IRQn);
01153                 
01154                 DBG.printf("emma: setup mqtt client\r\n");
01155                 sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
01156                 if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
01157                     mqtt.connectedCb.attach(&mqttConnected);
01158                     mqtt.disconnectedCb.attach(&mqttDisconnected);
01159                     mqtt.connect(MQTT_HOST,MQTT_PORT,false);
01160                     DBG.printf("emma: success to setup mqtt\r\n");
01161                     TFT.locate(0,40);
01162                     TFT.printf("emma: success to setup mqtt");    
01163                 }
01164                 DBG.printf("emma: system started\r\n");
01165         
01166                 t.start();
01167                 while(t.read_ms() < 5000) {
01168                     esp.process();    
01169                 }
01170                 t.stop();
01171                 t.reset();
01172                 
01173                 //enable UART2
01174                 NVIC_EnableIRQ(USART2_IRQn);
01175 
01176                 espFreeMemory = false;    
01177             }
01178             
01179             //panelEnergy, panelVoltage, and panelPower
01180             if(tPanelEnergy.read() > 30.0f) {
01181                 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
01182                 TFT.locate(0,60);
01183                 TFT.printf("                                                  ");
01184                 TFT.locate(0,60);
01185                 TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
01186                 
01187                 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
01188                 TFT.locate(0,80);
01189                 TFT.printf("                                                  ");
01190                 TFT.locate(0,80);
01191                 TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
01192                 
01193                 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
01194                 TFT.locate(0,100);
01195                 TFT.printf("                                                  ");
01196                 TFT.locate(0,100);
01197                 TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
01198                 
01199                 if(newEnergyData) {
01200                     //for(int i=1; i<4; i++) {
01201                     for(int i=1; i<2; i++) {
01202                         DBG.printf("sending channel: %d\r\n",i);
01203                         if(i==1){
01204                             XWattHr = AWattHr;
01205                             XVrms = AVrms;
01206                             XWatt = AWatt;    
01207                         } else if(i==2) {
01208                             XWattHr = BWattHr;
01209                             XVrms = BVrms;
01210                             XWatt = BWatt;
01211                         } else {
01212                             XWattHr = CWattHr;
01213                             XVrms = CVrms;
01214                             XWatt = CWatt;
01215                         }
01216                         
01217                         if(XWattHr != 0.0f) {
01218                         
01219                             sprintf(r,"/emma/api/controller/energy/%d",i);
01220                             seconds = time(NULL);
01221                             //for(int j=0; j<sizeof(q); j++) {
01222                             //    q[j]=0; }
01223                             strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
01224                         
01225                             //calculate hmacTime
01226                             for(int j=0; j<sizeof(p); j++) {
01227                                 p[j]=0; }
01228                             sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
01229                             hmacTime = calculateMD5(p);
01230                         
01231                             sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
01232                             emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
01233                             //DBG.printf("dataEnergy:\r\n%s\r\n",s);
01234                             rest.post(r,s);
01235                             wait(2);
01236                             if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
01237                                 DBG.printf("send channel: %d success\r\n",i);
01238                                 TFT.foreground(Green);
01239                                 TFT.locate(0,120);
01240                                 TFT.printf("                                        ");
01241                                 TFT.locate(0,120);
01242                                 TFT.printf("send ch%d success",i);
01243                                 wait(1);
01244                                 TFT.locate(0,120);
01245                                 TFT.printf("                                        ");
01246                                 TFT.foreground(White);    
01247                             } else {
01248                                 DBG.printf("send channel: %d failed\r\n",i);
01249                                 TFT.foreground(Red);
01250                                 TFT.locate(0,120);
01251                                 TFT.printf("                                        ");
01252                                 TFT.locate(0,120);
01253                                 TFT.printf("send ch%d failed",i);
01254                                 wait(1);
01255                                 TFT.locate(0,120);
01256                                 TFT.printf("                                        ");
01257                                 TFT.foreground(White);    
01258                             }
01259                         }
01260                     }
01261                     
01262                     newEnergyData = false;
01263                 }
01264                 
01265                 tPanelEnergy.reset();
01266                 loop++;    
01267             }
01268             
01269             //panel environment
01270             checkRxBuffer();
01271             if(tPanel.read() > 900.0f) {    //900 is 15 minutes
01272                 int dTemp=0;
01273                 int dHum=0;
01274                 int dGas=0;
01275                 
01276                 DBG.printf("getPanelEnvironment\r\n");
01277                 
01278                 //get environment sensor
01279                 trial=0;
01280                 while(1) {
01281                     if(trial>=2) {  //two times trial
01282                         break;
01283                     }
01284                     if(d.readData() == DHT11::OK) {
01285                         dTemp = d.readTemperature();
01286                         dHum = d.readHumidity();
01287                         break;    
01288                     }
01289                     trial++;
01290                     wait(3);    
01291                 }
01292                 
01293                 //send environment sensor
01294                 if(dTemp!=0 && dHum!=0) {
01295                     seconds = time(NULL);
01296                     strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
01297                 
01298                     //calculate hmacTime
01299                     for(int j=0; j<sizeof(p); j++) {
01300                         p[j]=0; }
01301                     sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
01302                     hmacTime = calculateMD5(p);
01303                 
01304                     sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
01305                     emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas);
01306                     //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
01307                     rest.post("/emma/api/controller/environment",s);
01308                     wait(2);
01309                     str = rxBuf;
01310                     if(str.rfind("/environment") != std::string::npos) {
01311                         str.erase(str.begin(),str.begin()+str.rfind("/environment"));
01312                         if(str.find("\"status\":\"success\"") != std::string::npos) {
01313                             DBG.printf("send panel environment success\r\n");
01314                         } else {
01315                             DBG.printf("send panel environment failed\r\n");
01316                         }
01317                     }
01318                     checkRxBuffer();
01319                 }
01320                 tPanel.reset();
01321             }
01322             
01323             //nodeTemp
01324             checkRxBuffer();
01325             if(tNodes.read() > 900.0f) {    //900 is 15 minutes
01326                 DBG.printf("getNodesTemperature\r\n");
01327                 
01328                 for(int i=0; i<NODES_MAX; i++) {
01329                     if(!nodes[i].ipAddr.empty()) {
01330                         //get node's temp
01331                         string temp;
01332                         nodes[i].restConn->get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
01333                         wait(2);
01334                         temp = rxBuf;
01335                         if(temp.rfind(nodes[i].ipAddr) != std::string::npos) {
01336                             temp.erase(temp.begin(),temp.begin()+temp.rfind(nodes[i].ipAddr));
01337                             if(temp.rfind("temp=") != std::string::npos) {
01338                                 temp.erase(temp.begin(),temp.begin()+temp.rfind("temp=")+6);
01339                                 temp.erase(temp.begin()+temp.find("\""),temp.end());
01340                             } else {
01341                                 temp = "0"; //connect to node, but receive none
01342                             }
01343                         } else {
01344                             temp = "0";     //not connected to node
01345                         }
01346                     
01347                         DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
01348                     
01349                         //send node's temp
01350                         if(temp != "0") {
01351                             seconds = time(NULL);
01352                             strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
01353                         
01354                             //calculate hmacTime
01355                             for(int j=0; j<sizeof(p); j++) {
01356                                 p[j]=0; }
01357                             sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
01358                             hmacTime = calculateMD5(p);
01359                         
01360                             sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":\"%s\",\"value\":%s}",
01361                             emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
01362                             //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
01363                             rest.post("/emma/api/controller/nodetemp",s);
01364                             wait(2);
01365                             str = rxBuf;
01366                             if(str.rfind("/nodetemp") != std::string::npos) {
01367                                 str.erase(str.begin(),str.begin()+str.rfind("/nodetemp"));
01368                                 if(str.find("\"status\":\"success\"") != std::string::npos) {
01369                                     DBG.printf("send nodeTemp success\r\n");
01370                                 } else {
01371                                     DBG.printf("send nodeTemp failed\r\n");
01372                                 }
01373                             }
01374                             checkRxBuffer();
01375                         }
01376                     }    
01377                 }
01378                 tNodes.reset();
01379             }
01380             
01381             //command
01382             checkRxBuffer();
01383             if(newCommand) {
01384                 DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
01385                 TFT.locate(0,160);
01386                 TFT.printf("                                        ");
01387                 TFT.locate(0,160);
01388                 TFT.printf("newCommand");
01389                 
01390                 parse(jsonValue,globalCommand.c_str());
01391                 const char *parameter[5] = {"name","nType","nAddr","dType","cmd"};
01392                 
01393                 //check whether command is valid
01394                 bool validCommand = true;
01395                 for(int i=0; i<5; i++) {
01396                     validCommand = validCommand && jsonValue.hasMember(parameter[i]);
01397                 }
01398                 DBG.printf("command validity:%d\r\n",validCommand);
01399                 
01400                 if(validCommand) {
01401                     string commandName = jsonValue[parameter[0]].get<std::string>();
01402                     string commandNType = jsonValue[parameter[1]].get<std::string>();
01403                     string commandNAddr = jsonValue[parameter[2]].get<std::string>();
01404                     string commandDType = jsonValue[parameter[3]].get<std::string>();
01405                     string commandCmd = jsonValue[parameter[4]].get<std::string>();
01406                     
01407                     if(commandNType == "0") {       //switch on panel controller
01408                         DBG.printf("command for switch\r\n");
01409                     }
01410                     else if(commandNType == "1") {  //node with mac address
01411                         DBG.printf("command for node\r\n");
01412                         //get node ip address based on node mac address
01413                         //string nodeIP;
01414                         //nodeIp = readNodeIP(commandNAddr);
01415                         //nodeIP = "192.168.2.15";
01416                         //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
01417                     
01418                         //get index of node list based on mac address
01419                         int idx = NODES_INVALID;
01420                         for(int i=0; i<NODES_MAX; i++) {
01421                             if(!nodes[i].macAddr.compare(commandNAddr)) {
01422                                 idx = i;
01423                             }
01424                         }
01425                     
01426                         //execution process
01427                         //int trial;
01428                         string execResult = "failed";
01429                         if(idx != NODES_INVALID) {
01430                             DBG.printf("index found at %d\r\n",idx);
01431                         
01432                             //get cmd string based on device type and command number
01433                             string nodeCmd;
01434                             nodeCmd = readNodeCmd(commandDType,commandCmd);
01435                             //nodeCmd = "020129A0163B161315131613153C151316131514143C153C16141414141415151315141414141514141415141414143D1514143D141415141414143D14000D"; //turn off
01436                             //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
01437                         
01438                             //execute command
01439                             DBG.printf("executing command\r\n");
01440                             sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
01441                             
01442                             trial=0;
01443                             while(1) {
01444                                 //cmdExecuted = false;
01445                                 if(trial>=2) {   //two times trial
01446                                     DBG.printf("cmd is not executed\r\n");
01447                                     TFT.foreground(Red);
01448                                     TFT.locate(0,180);
01449                                     TFT.printf("                                        ");
01450                                     TFT.locate(0,180);
01451                                     TFT.printf("cmd is not executed");
01452                                     wait(1);
01453                                     TFT.locate(0,180);
01454                                     TFT.printf("                                        ");
01455                                     TFT.foreground(White);
01456                                     break;    
01457                                 }
01458                                 nodes[idx].restConn->get("/",s);
01459                                 wait(2);
01460                                 if(rxBuf.find("REST: Sent") != std::string::npos) {
01461                                     DBG.printf("cmd is executed\r\n");
01462                                     TFT.foreground(Green);
01463                                     TFT.locate(0,180);
01464                                     TFT.printf("                                        ");
01465                                     TFT.locate(0,180);
01466                                     TFT.printf("cmd is executed");
01467                                     wait(1);
01468                                     TFT.locate(0,180);
01469                                     TFT.printf("                                        ");
01470                                     TFT.foreground(White);
01471                                     execResult = "success";
01472                                     break;    
01473                                 }
01474                                 trial++;
01475                             }    
01476                         } else {
01477                             TFT.foreground(Red);
01478                             TFT.locate(0,180);
01479                             TFT.printf("                                        ");
01480                             TFT.locate(0,180);
01481                             TFT.printf("node is invalid");
01482                             wait(1);
01483                             TFT.locate(0,180);
01484                             TFT.printf("                                        ");
01485                             TFT.foreground(White);    
01486                         }
01487                     
01488                         wait(2);       
01489                         //send execution result
01490                         //DBG.printf("send execution result\r\n");
01491                         
01492                         //calculate hmacCmd
01493                         for(int j=0; j<sizeof(p); j++) {
01494                             p[j]=0; }
01495                         sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());   
01496                         hmacCmd = calculateMD5(p);
01497                         
01498                         sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"name\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
01499                         emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandName.c_str(),execResult.c_str(),hmacCmd.c_str());
01500                     
01501                         trial=0;
01502                         while(1) {
01503                             if(trial>=2) {   //two times trial
01504                                 DBG.printf("failed to send execution result\r\n");
01505                                 break;    
01506                             }
01507                             rest.post("/emma/api/controller/result",s);
01508                             wait(2);
01509                             str = rxBuf;
01510                             if(str.rfind("/result") != std::string::npos) {
01511                                 str.erase(str.begin(),str.begin()+str.rfind("/result"));
01512                                 if(str.find("\"status\":\"success\"") != std::string::npos) {
01513                                     DBG.printf("success to send execution result\r\n");
01514                                     break;
01515                                 }
01516                             }
01517                             checkRxBuffer();
01518                             trial++;
01519                         }
01520                     }
01521                 }
01522                 
01523                 //clear text on lcd
01524                 TFT.locate(0,160);
01525                 TFT.printf("                                        ");
01526                 
01527                 newCommand = false;
01528             }
01529             osDelay(5000);    
01530         }
01531     }
01532 }
01533 void emmaModeFirmwareDownload(void) {
01534     bool emmaGetFirmwareParam = false;
01535     MbedJSONValue jsonValue;
01536     
01537     DBG.printf("emmaModeFirmwareDownload\r\n");
01538     
01539     char s[384];
01540     string str;
01541     string connData;
01542     string chunk;
01543     
01544     //firmware parameter
01545     string firmwareVer;
01546     string firmwareName;
01547     int numPart;
01548     
01549     //downloading
01550     string firmwarePart;
01551     string calcMD5;
01552     string srvrMD5;
01553     bool nextPart;
01554     
01555     //set wifi to mode bridge
01556     _ESP.printf("MODE=B");
01557     DBG.printf("set mode bridge\r\n");
01558     while(1) {
01559         char rcv[128] = {};
01560         rcvReply(rcv,3000);
01561         str = rcv;
01562         if(str.find("MODE=B_OK") != std::string::npos)
01563             break;
01564     }
01565     DBG.printf("MODE=B\r\n");
01566     
01567     esp.enable();
01568     wait(1);
01569     while(!esp.ready());
01570     
01571     if(!rest.begin("candra.tritronik.com",3128,false)) {
01572         DBG.printf("EMMA: fail to setup rest");
01573         while(1);    
01574     }
01575     
01576     //wifiConnected = true; //with custom firmware, panel should connect wifi automatically
01577     useProxy = true;
01578     
01579     esp.process();
01580     //set connData
01581     if(useProxy) {
01582         for(int i=0; i<sizeof(s); i++) {
01583             s[i]=0; }
01584         //sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str());
01585         sprintf(s,"http://192.168.128.69/emmaController/firmware/firmwareParameter");
01586         connData = s;
01587     } else {
01588         for(int i=0; i<sizeof(s); i++) {
01589             s[i]=0; }
01590         //sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
01591         sprintf(s,"/emmaController/firmware/firmwareParameter");
01592         connData = s;
01593     }
01594     
01595     //get parameter of firmware to be downloaded
01596     while(!emmaGetFirmwareParam) {
01597         rest.get(connData.c_str());
01598         for(int i=0; i<sizeof(s); i++) {
01599             s[i]=0; }
01600         rest.getResponse(s,sizeof(s));
01601         //DBG.printf("rsp param:%s\r\n",s);
01602         
01603         str = s;
01604         if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
01605             str.erase(str.begin(),str.begin()+str.find("[")+1);
01606             str.erase(str.begin()+str.find("]"),str.end());
01607             
01608             parse(jsonValue,str.c_str());
01609             
01610             const char *parameter[2] = {"firmwareVer","numPart"};
01611             
01612             for(int i=0; i<2; i++) {
01613                 if(jsonValue.hasMember(parameter[i])) {
01614                     string val = jsonValue[parameter[i]].get<std::string>();
01615                     if(i==0) {
01616                         firmwareVer = val;    
01617                     } else if(i==1) {
01618                         sscanf(val.c_str(),"%d",&numPart);
01619                     }
01620                 }
01621             }
01622             
01623             if(!firmwareVer.empty() && numPart!=0) {
01624                 emmaGetFirmwareParam = true;    
01625             }
01626         }
01627     }
01628     DBG.printf("firmwareVer:%s\r\n",firmwareVer.c_str());
01629     DBG.printf("numPart:%d\r\n",numPart);
01630     
01631     //clear firmware file
01632     while(1) {
01633         if(clearFirmware()){
01634             DBG.printf("clear firmware on sd card\r\n\r\n");
01635             break;
01636         }
01637         wait(1);
01638     }
01639     
01640     //set connData
01641     if(useProxy) {
01642         for(int i=0; i<sizeof(s); i++) {
01643             s[i]=0; }
01644         sprintf(s,"http://192.168.128.69/emmaController/firmware/%s",firmwareVer.c_str());
01645         connData = s;
01646     } else {
01647         for(int i=0; i<sizeof(s); i++) {
01648             s[i]=0; }
01649         sprintf(s,"/emmaController/firmware/%s",firmwareVer.c_str());
01650         connData = s;
01651     }
01652     
01653     //download firmware
01654     for(int n=0; n<numPart; n++) {
01655         nextPart = false;
01656         while(!nextPart) {
01657             for(int i=0; i<sizeof(s); i++) {
01658                 s[i]=0; }
01659             sprintf(s,"%s/firmware_Hex_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
01660             rest.get(s);
01661             for(int i=0; i<sizeof(s); i++) {
01662                 s[i]=0; }
01663             rest.getResponse(s,sizeof(s));
01664             //DBG.printf("rsp[%d]:%s\r\n",n,s);
01665         
01666             str = s;
01667             if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
01668                 str.erase(str.begin(),str.begin()+str.find("{")+1);
01669                 str.erase(str.begin()+str.find("}"),str.end());
01670                 //DBG.printf("firmwarePart[%d]:%s\r\n",n,str.c_str());
01671                 firmwarePart = str;
01672             
01673                 //calculated MD5
01674                 calcMD5 = calculateMD5(firmwarePart);
01675                 //DBG.printf("calcMD5[%d]:%s\r\n",n,calcMD5.c_str());
01676             
01677                 //MD5 from server
01678                 for(int i=0; i<sizeof(s); i++) {
01679                     s[i]=0; }
01680                 sprintf(s,"%s/MD5/firmware_MD5_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
01681                 rest.get(s);
01682                 for(int i=0; i<sizeof(s); i++) {
01683                     s[i]=0; }
01684                 rest.getResponse(s,sizeof(s));
01685             
01686                 str = s;
01687                 if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
01688                     str.erase(str.begin(),str.begin()+str.find("{")+1);
01689                     str.erase(str.begin()+str.find("}"),str.end());
01690                     srvrMD5 = str;
01691                     //DBG.printf("srvrMD5[%d]:%s\r\n",n,srvrMD5.c_str());
01692                 
01693                     //compare original MD5 vs MD5 from server
01694                     if(strcmp(calcMD5.c_str(),srvrMD5.c_str()) == 0) {
01695                         //DBG.printf("MD5 correct\r\n");
01696                         
01697                         //save to sd card
01698                         int st = writeFirmwareHexToChar(firmwarePart);
01699                         if(st) {
01700                             DBG.printf("firmwarePart[%d/%d] written\r\n",n,numPart-1);
01701                             nextPart = true;    
01702                         }
01703                     
01704                     } else {
01705                         DBG.printf("MD5 incorrect\r\n");    
01706                     }
01707                 }   
01708             } else {
01709                 DBG.printf("retry to fetch firmwarePart[%d]\r\n",n);    
01710             }
01711             wait(0.5);
01712         }
01713     }
01714     DBG.printf("download finished\r\n");
01715 }
01716 /*end emma mode*/
01717 
01718 /*start energy related*/
01719 void energyThread(void const*) {
01720     Timer tEnergy;
01721     
01722     while(1) {
01723         tEnergy.start();
01724         DBG.printf("energyThread-start\r\n");
01725         
01726         AWattHrSum = 0;
01727         BWattHrSum = 0;
01728         CWattHrSum = 0;
01729         
01730         while(tEnergy.read() < 1*60.0) {
01731             AWattHrValue = ADE.getWattHR(PHASE_A);
01732             BWattHrValue = ADE.getWattHR(PHASE_B);
01733             CWattHrValue = ADE.getWattHR(PHASE_C); 
01734             
01735             AWattHrSum += AWattHrValue;
01736             BWattHrSum += BWattHrValue;
01737             CWattHrSum += CWattHrValue;
01738             
01739             
01740             //start check voltage and power
01741             AVrms = ADE.VRMS(PHASE_A) * 0.000124f;      //0.000158;     //constants are from calculateVRMS function
01742             BVrms = ADE.VRMS(PHASE_B) * 0.000123f;
01743             CVrms = ADE.VRMS(PHASE_C) * 0.000122f;
01744     
01745             AIrms = ADE.IRMS(PHASE_A) * 0.00001006f;    //0.0000125f;   //constants are from calculateIRMS function
01746             BIrms = ADE.IRMS(PHASE_B) * 0.00001005f;
01747             CIrms = ADE.IRMS(PHASE_C) * 0.00001004f;
01748     
01749             AWatt = AVrms * AIrms;
01750             BWatt = BVrms * BIrms;
01751             CWatt = CVrms * CIrms;
01752             //end check voltage and power
01753                
01754         }
01755         
01756         AWattHr = AWattHrSum * 0.000044169f;    //0.0000198f;
01757         BWattHr = BWattHrSum * 0.000044168f;    //0.0000197f;
01758         CWattHr = CWattHrSum * 0.000044167f;    //0.0000196f;
01759         
01760         newEnergyData = true;
01761         
01762         tEnergy.stop();
01763         tEnergy.reset();
01764         DBG.printf("energyThread-finish\r\n");
01765     }
01766 }
01767 void checkVoltagePower(void) {
01768     //check if voltage or power violates threshold
01769     char p[64];
01770     char q[32];
01771     char s[256];
01772     string hmacTime;
01773     string str;
01774     time_t seconds;
01775     
01776     //DBG.printf("checkVoltagePower-start\r\n");
01777     
01778     //vrms and irms might be placed inside energy calculation routine
01779     /*
01780     AVrms = ADE.VRMS(PHASE_A) * 0.000128f; //0.000158;   //constants are from calculateVRMS function
01781     BVrms = ADE.VRMS(PHASE_B) * 0.000127f; //0.000157;
01782     CVrms = ADE.VRMS(PHASE_C) * 0.000126f; //0.000156;
01783     
01784     AIrms = ADE.IRMS(PHASE_A) * 0.0000125f;  //constants are from calculateIRMS function
01785     BIrms = ADE.IRMS(PHASE_B) * 0.0000123f;
01786     CIrms = ADE.IRMS(PHASE_C) * 0.0000124f;
01787     
01788     AWatt = AVrms * AIrms;
01789     BWatt = BVrms * BIrms;
01790     CWatt = CVrms * CIrms;
01791     */
01792     
01793     //DBG.printf("Vrms of each phase:%.2f - %.2f - %.2f\r\n", AVrms, BVrms, CVrms);
01794     //DBG.printf("Watt of each phase:%.2f - %.2f - %.2f\r\n", AWatt, BWatt, CWatt);
01795     //wait(1);
01796     
01797     //get time
01798     seconds = time(NULL);
01799     strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
01800     
01801     //calculate hmacTime
01802     for(int j=0; j<sizeof(p); j++) {
01803         p[j]=0; }
01804     sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);   
01805     hmacTime = calculateMD5(p);
01806     
01807     if(AVrms > VRMSTHRESHOLD || AWatt > WATTTHRESHOLD) {
01808         DBG.printf("alert on ch1\r\n");
01809         sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}",
01810         emmaUID.c_str(),hmacTime.c_str(),q,AVrms,AWatt);
01811         rest.post("/emma/api/controller/alert/1",s);
01812         wait(2);
01813         str = rxBuf;
01814         if(str.rfind("/alert/1") != std::string::npos) {
01815             str.erase(str.begin(),str.begin()+str.rfind("/alert/1"));
01816             if(str.find("\"status\":\"success\"") != std::string::npos) {
01817                 DBG.printf("send alert ch1 success\r\n");
01818             } else {
01819                 DBG.printf("send alert ch1 failed\r\n");
01820             }
01821         }
01822     }
01823     
01824     if(BVrms > VRMSTHRESHOLD || BWatt > WATTTHRESHOLD) {
01825         DBG.printf("alert on ch2\r\n");
01826         sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}",
01827         emmaUID.c_str(),hmacTime.c_str(),q,BVrms,BWatt);
01828         rest.post("/emma/api/controller/alert/2",s);
01829         wait(2);
01830         str = rxBuf;
01831         if(str.rfind("/alert/2") != std::string::npos) {
01832             str.erase(str.begin(),str.begin()+str.rfind("/alert/2"));
01833             if(str.find("\"status\":\"success\"") != std::string::npos) {
01834                 DBG.printf("send alert ch2 success\r\n");
01835             } else {
01836                 DBG.printf("send alert ch2 failed\r\n");
01837             }
01838         }
01839     }
01840     
01841     if(CVrms > VRMSTHRESHOLD || CWatt > WATTTHRESHOLD) {
01842         DBG.printf("alert on ch3\r\n");
01843         DBG.printf("alert on ch3\r\n");
01844         sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}",
01845         emmaUID.c_str(),hmacTime.c_str(),q,CVrms,CWatt);
01846         rest.post("/emma/api/controller/alert/3",s);
01847         wait(2);
01848         str = rxBuf;
01849         if(str.rfind("/alert/3") != std::string::npos) {
01850             str.erase(str.begin(),str.begin()+str.rfind("/alert/3"));
01851             if(str.find("\"status\":\"success\"") != std::string::npos) {
01852                 DBG.printf("send alert ch3 success\r\n");
01853             } else {
01854                 DBG.printf("send alert ch3 failed\r\n");
01855             }
01856         }
01857     }
01858     //DBG.printf("checkVoltagePower-finish\r\n");
01859 }
01860 /*end energy related*/
01861 
01862 /*start wifi mqtt*/
01863 void mqttConnected(void* response) {
01864     DBG.printf("MQTT Connected\r\n");
01865     char mqttTopic[64];
01866     sprintf(mqttTopic,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str());
01867     mqtt.subscribe(mqttTopic);
01868 }
01869 void mqttDisconnected(void* response) {
01870     DBG.printf("MQTT Disconnected\r\n");    
01871 }
01872 /*end wifi mqtt*/
01873 
01874 /*start wifi rest*/
01875 void rxInterrupt(void) {
01876     char c;
01877     
01878     while(_ESP.readable()) {
01879         c = _ESP.getc();
01880         if(c != 0) {    //char is not null
01881             rxBuf += c;
01882         }
01883     }
01884 }
01885 void checkRxBuffer(void) {
01886     //check new command
01887     if(rxBuf.rfind("/command") != std::string::npos) {
01888         rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.rfind("/command"));
01889         if(rxBuf.find("[{") != std::string::npos && rxBuf.find("}]") != std::string::npos) {
01890             rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.find("[{")+1);
01891             rxBuf.erase(rxBuf.begin()+rxBuf.find("]"),rxBuf.end());
01892             rxBuf.assign(globalCommand);
01893             //DBG.printf("gC:%s\r\n",globalCommand.c_str());
01894             newCommand = true;
01895         }
01896     }
01897     
01898     //check free memory -> reinitialize mqtt connection
01899     if(rxBuf.rfind("Free memory") != std::string::npos || rxBuf.rfind("dhcp client start") != std::string::npos) {
01900         espFreeMemory = true;
01901     }
01902     
01903     //clear rxBuf
01904     rxBuf.clear();
01905 }
01906 /*end wifi rest*/
01907 
01908 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
01909 {
01910     MQTT::Message message;
01911     char* topic = new char[strlen(platformDOMAIN.c_str())+strlen(emmaUID.c_str())+9];
01912     *topic = '\0';
01913     strcat(topic, platformDOMAIN.c_str());
01914     strcat(topic, "/");
01915     strcat(topic, emmaUID.c_str());
01916     strcat(topic, "/dummy");
01917 
01918     const char buf[25] = "{\"d\":{\"myName\":\"EMMA\"}}";
01919     
01920     message.qos = MQTT::QOS2;
01921     message.retained = false;
01922     message.dup = false;
01923     message.payload = (void*)buf;
01924     message.payloadlen = strlen(buf);
01925 
01926     DBG.printf("Publishing %s\r\n", buf);
01927     return client->publish(topic, &message);
01928 }
01929 
01930 /*start eth mqtt*/
01931 void ethMQTTMessageArrived(MQTT::MessageData& md) {
01932 
01933     MQTT::Message &message = md.message;
01934     char topic[md.topicName.lenstring.len + 1];
01935     string str;
01936     sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
01937 
01938     DBG.printf("Message arrived on topic %s: %.*s\r\n",  topic, message.payloadlen, message.payload);
01939     char *s = (char*)message.payload;
01940     string sp(s);
01941     if(sp.find("[") != std::string::npos && sp.find("]") != std::string::npos) {
01942         sp.erase(sp.begin(),sp.begin()+sp.find("[")+1);
01943         sp.erase(sp.begin()+sp.find("]"),sp.end());
01944     }
01945     else {
01946         DBG.printf("Invalid MQTT command");
01947         return;
01948     }
01949 
01950     MbedJSONValue jsonValue;
01951     parse(jsonValue, sp.c_str());
01952     const char *parameter[5] = {"name","nType","nAddr","dType","cmd"};
01953     
01954     //check if command is valid
01955     bool validCommand = true;
01956     for(int i=0; i<5; i++) {
01957         validCommand = validCommand && jsonValue.hasMember(parameter[i]);
01958     }
01959     DBG.printf("command validity:%d\r\n",validCommand);
01960     
01961     //check for new command
01962     if(validCommand) {
01963         string commandId = jsonValue[parameter[0]].get<std::string>();
01964         string commandNType = jsonValue[parameter[1]].get<std::string>();
01965         string commandNAddr = jsonValue[parameter[2]].get<std::string>();
01966         string commandDType = jsonValue[parameter[3]].get<std::string>();
01967         string commandCmd = jsonValue[parameter[4]].get<std::string>();
01968         
01969         if(commandNType == "0") {       //switch on panel controller
01970             DBG.printf("command for switch\r\n");
01971         }
01972         else if(commandNType == "1") {  //node with mac address
01973             DBG.printf("command for node\r\n");
01974             //get node ip address based on node mac address
01975             string nodeIP = readNodeIP(commandNAddr);
01976             //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
01977             
01978             //get cmd string based on device type and command number
01979             string nodeCmd = readNodeCmd(commandDType,commandCmd);
01980             //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());                        
01981             //execute command
01982             int trial=0;
01983             trial = 0;
01984             bool execStatus=false;
01985 
01986             while( !execStatus ) {
01987                 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
01988                 str.assign(s);
01989                 DBG.printf("str value:%s\r\n",str.c_str());                            
01990                 string rcv = ethGET(nodeIP,REMOTE_TCP_PORT,str); // cause mqtt to stop
01991                 DBG.printf("response:%s\r\n",rcv.c_str());
01992                 str = rcv;
01993                 if(str.find("OK") != std::string::npos) {
01994                     DBG.printf("CMD executed successfully\r\n");
01995                     execStatus = true;
01996                     break;
01997                 }
01998                 if(trial>5) {   //two times trial
01999                     DBG.printf("cmd is not executed\r\n");
02000                     execStatus = false;
02001                 }
02002                 DBG.printf("Trial++\r\n");
02003                 trial++;
02004                 wait(3);
02005             }
02006             DBG.printf("Send execution status\r\n");
02007             //send execution status
02008         }
02009     }
02010     DBG.printf("Finish processing new command\r\n");
02011 }
02012 
02013 int ethMQTTConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {
02014 
02015     int rc = ipstack->connect(MQTT_HOST, MQTT_PORT);
02016     
02017     if(rc!=0)
02018         return rc;
02019         
02020     //MQTT Connect
02021     //char clientId[] = "emma/0674ff575349896767072538";
02022     char clientId [strlen(emmaUID.c_str())+6];
02023     sprintf(clientId,"emma/%s", emmaUID.c_str());
02024     DBG.printf("clientId:%s\r\n",clientId);
02025     
02026     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
02027     data.MQTTVersion = 3;
02028     data.clientID.cstring = clientId;
02029     data.username.cstring = "761b233e-a49a-4830-a8ae-87cec3dc1086";
02030     data.password.cstring = "ef25cf4567fbc07113252f8d72b7faf2";
02031     
02032     if ((rc = client->connect(&data)) == 0)
02033     {
02034 //         connected = true;
02035         DBG.printf("MQTT connected\r\n");
02036     }
02037     
02038     //MQTT Subscribe
02039 //    char* topic = "emma/005300553533510334313732/command";
02040     char* topic = new char[strlen(platformDOMAIN.c_str())+strlen(emmaUID.c_str())+9];
02041     *topic = '\0';
02042     strcat(topic, platformDOMAIN.c_str());
02043     strcat(topic, "/");
02044     strcat(topic, emmaUID.c_str());
02045     strcat(topic, "/command");
02046     DBG.printf("MQTT subscription topic: %s\r\n", topic);
02047                 
02048     if ((rc = client->subscribe(topic, MQTT::QOS2, ethMQTTMessageArrived)) == 0) {
02049         DBG.printf("Subscribe success\r\n");    
02050     }
02051     
02052     return rc;
02053 }
02054 
02055 int getConnTimeout(int attemptNumber)
02056 {  // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
02057    // after 20 attempts, retry every 10 minutes
02058     return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
02059 }
02060 
02061 void ethMQTTAttemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {
02062     int retryAttempt = 0;
02063     
02064     while(!ipstack->getEth().linkstatus()) {
02065         wait(1.0f);
02066         DBG.printf("Ethernet link not present. Check cable connection\r\n");
02067     }
02068     
02069     while(ethMQTTConnect(client,ipstack) != 0) {
02070         int timeout = getConnTimeout(++retryAttempt);
02071         DBG.printf("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout);
02072         wait(timeout);
02073     }
02074 }
02075 
02076 /*end eth mqtt*/
02077 
02078 /*start emma settings*/
02079 string getUID(void) {
02080     char s[32];
02081     unsigned long *unique = (unsigned long *)BASE_ADDR;
02082     sprintf(s,"%08x%08x%08x",unique[0], unique[1], unique[2]);
02083     return s;    
02084 }
02085 
02086 string readSetting(string parameter) {
02087     FILE *fp;
02088     signed char c;
02089     int i=0;
02090     char s[64];
02091     string strS;
02092     
02093     sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
02094     
02095     fp = fopen(s,"r");
02096     memset(s,0,sizeof(s));
02097     if(fp != NULL) {
02098         while(1) {
02099             c = fgetc(fp);
02100             if(c == EOF){
02101                 break;
02102             }
02103             s[i] = c;
02104             i++;
02105         }
02106         strS = s;
02107         if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
02108             strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
02109             strS.erase(strS.begin()+strS.find(")"),strS.end());
02110         } else {
02111             strS = "";    
02112         }
02113     }
02114     fclose(fp);
02115     return strS;    
02116 }
02117 
02118 bool writeSetting(string parameter, string value) {
02119     FILE *fp;
02120     char s[255];
02121     
02122     sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
02123     fp = fopen(s,"w");
02124     if(fp != NULL) {
02125         fprintf(fp,value.c_str());
02126         fclose(fp);
02127         return true;
02128     }
02129     fclose(fp);
02130     return false;
02131 }
02132 /*end emma settings*/
02133 
02134 /*start emma node*/
02135 string readNodeIP(string macAddr) {
02136     FILE *fp;
02137     signed char c;
02138     int i=0;
02139     char s[64];
02140     string strS;
02141     
02142     sprintf(s,"/sd/nodeList/%s/nodeIP.txt",macAddr.c_str());
02143     
02144     fp = fopen(s,"r");
02145     memset(s,0,sizeof(s));
02146     if(fp != NULL) {
02147         while(1) {
02148             c = fgetc(fp);
02149             if(c == EOF){
02150                 break;
02151             }
02152             s[i] = c;
02153             i++;
02154         }
02155         strS = s;
02156         if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
02157             strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
02158             strS.erase(strS.begin()+strS.find(")"),strS.end());
02159         } else {
02160             strS = "";    
02161         }
02162     }
02163     fclose(fp);
02164     return strS;
02165 }
02166 
02167 string readNodeCmd(string dType, string cmd) {
02168     FILE *fp;
02169     signed char c;
02170     int i=0;
02171     char s[128];
02172     string strS;
02173     
02174     sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmd.c_str());
02175     
02176     fp = fopen(s,"r");
02177     memset(s,0,sizeof(s));
02178     if(fp != NULL) {
02179         while(1) {
02180             c = fgetc(fp);
02181             if(c == EOF){
02182                 break;
02183             }
02184             s[i] = c;
02185             i++;
02186         }
02187         strS = s;
02188         if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
02189             strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
02190             strS.erase(strS.begin()+strS.find(")"),strS.end());
02191         } else {
02192             strS = "";    
02193         }
02194     }
02195     fclose(fp);
02196     return strS;    
02197 }
02198 
02199 string *readNodeList(void) {
02200     static string nd[10];   //max node
02201     DIR *d;
02202     struct dirent *p;
02203     string q;
02204     int i=0;
02205     
02206     d = opendir("/sd/nodeList");
02207     if(d != NULL) {
02208         while((p = readdir(d)) != NULL) {
02209             q = p->d_name;
02210             nd[i] = q;
02211             i++;    
02212         }    
02213     }
02214     closedir(d);
02215     return nd;
02216 }
02217 
02218 string wifiGetNodeTemp(string macAddr) {
02219     int trial=0;
02220     string nodeIP = readNodeIP(macAddr);
02221     string str;
02222     string temp = "0";
02223     
02224     if(rest.begin(nodeIP.c_str(),REMOTE_TCP_PORT,false)) {
02225         while(1) {
02226             char rcv[256] = {};
02227             rest.get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
02228             rest.getResponse(rcv,sizeof(rcv));
02229             str = rcv;
02230             if(str.find("temp=") != std::string::npos) {
02231                 str.erase(str.begin(),str.begin()+str.find("temp=")+6);
02232                 str.erase(str.begin()+str.find("\""),str.end());
02233                 temp = str;
02234                 break;
02235             }
02236             if(trial>1) {   //three times trial
02237                 break;    
02238             }
02239             trial++;
02240             wait(2);
02241         }
02242     }
02243     return temp;
02244 }
02245 /*end emma node*/
02246 
02247 /*start emma connection function*/
02248 string ethGET(string host, int port, string url) {
02249     DBG.printf("Entering ethGET with url: %s\r\n", url);
02250     DBG.printf("Host: %s\r\n", host);
02251     DBG.printf("Port: %d\r\n", port);    
02252     char buf[1024];
02253     char s[256];
02254     int ret;
02255     TCPSocketConnection sock;
02256     Timer t;
02257     
02258     DBG.printf("Sock connecting\r\n");
02259     if (sock.connect(host.c_str(), port) == 0){
02260         DBG.printf("Sock connected successfully\r\n");
02261     }
02262     else {
02263         DBG.printf("Sock failed to connect\r\n");
02264         sock.close();
02265         return buf;
02266     }
02267     DBG.printf("Sock sending all\r\n");
02268     sprintf(s,"%s",url.c_str());
02269     int size = sock.send_all(s,sizeof(s)-1);
02270     if (size >= 0) {
02271         DBG.printf("Sock sent %d\r\n", size);
02272     }
02273     else {
02274         DBG.printf("Sock failed to send\r\n");
02275         sock.close();
02276         return buf;
02277     }
02278     DBG.printf("Wait...\r\n");
02279     wait(2);
02280     
02281     //receive return
02282     t.start();
02283     while(1) {
02284         ret = sock.receive(buf, sizeof(buf));
02285         if(ret<=0 || t.read_ms() > 10000) {
02286             t.stop();
02287             break;
02288         }    
02289     }
02290     sock.close();
02291     return buf;
02292 }
02293 /*end emma connection function*/
02294 
02295 /*start emma private function*/
02296 /*
02297 void connectedIface(void) { //WARNING: should be run in emmaModeRegister and emmaModeOperation only - problem with esp, after MODE=B, cannot go back to MODE=S
02298     char s[512];
02299     int connPort;
02300     string connHost;
02301     string str;
02302     Timer t;
02303     
02304     //wifi interface
02305     if(wifiAvailable) {
02306         _ESP.printf("MODE=B");
02307         if(useProxy) {
02308             connHost = proxySERVER;
02309             sscanf(proxyPORT.c_str(),"%d",&connPort);
02310             for(int i=0; i<sizeof(s); i++) {
02311                 s[i]=0; }
02312             sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
02313         } else {
02314             connHost = EMMA_SERVER_HOST;
02315             connPort = EMMA_SERVER_PORT;
02316             for(int i=0; i<sizeof(s); i++) {
02317                 s[i]=0; }
02318             sprintf(s,"/emma/api/controller/test");
02319         }
02320         wait(1);
02321         t.start();
02322         while(!esp.ready() && t.read_ms() < 5000);
02323         t.stop();
02324         if(rest.begin(connHost.c_str(),connPort,false)) {
02325             //DBG.printf("rest begin\r\n");
02326             esp.process();
02327             rest.get(s);
02328             for(int i=0; i<sizeof(s); i++) {
02329                 s[i]=0; }
02330             rest.getResponse(s,sizeof(s));
02331             str = s;
02332             //DBG.printf("response:%s\r\n",s);
02333             if(str.find("OK") != std::string::npos) {
02334                 wifiConnected = true;
02335             }
02336         } else {
02337             wifiConnected = false;    
02338         }
02339     } else {
02340         wifiConnected = false;    
02341     }
02342     
02343     //eth interface
02344     if(ethAvailable) {
02345         if(useProxy) {
02346             connHost = proxySERVER;
02347             sscanf(proxyPORT.c_str(),"%d",&connPort);
02348             for(int i=0; i<sizeof(s); i++) {
02349                 s[i]=0; }
02350             sprintf(s,"GET http://%s:%d/emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
02351         } else {
02352             connHost = EMMA_SERVER_HOST;
02353             connPort = EMMA_SERVER_PORT;
02354             for(int i=0; i<sizeof(s); i++) {
02355                 s[i]=0; }
02356             strcpy(s,"GET /emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n");
02357         }
02358         
02359         t.start();
02360         while(1) {
02361             str = ethGET(connHost,connPort,s);
02362             if(str.find("OK") != std::string::npos) {
02363                 t.stop();
02364                 ethConnected = true;
02365                 break;    
02366             }
02367             if(t.read_ms() > 5000) {
02368                 t.stop();
02369                 ethConnected = false;
02370                 break;    
02371             }
02372         }
02373     } else {
02374         ethConnected = false;    
02375     }
02376     
02377     //gprs interface    
02378 }
02379 */
02380 void isEthAvailable(void) {
02381     if(ipstack.getEth().linkstatus()) {
02382         ethAvailable = true;    
02383         DBG.printf("IP address: %s\r\n", ipstack.getEth().getIPAddress());
02384     } else {
02385         ethAvailable = false;    
02386     }
02387 }
02388 
02389 void isEthConnected(void) {
02390     char s[512];
02391     int connPort;
02392     string connHost;
02393     string str;
02394     Timer t;
02395     
02396     if(ethAvailable) {
02397         if(useProxy) {
02398             connHost = proxySERVER;
02399             sscanf(proxyPORT.c_str(),"%d",&connPort);
02400             for(int i=0; i<sizeof(s); i++) {
02401                 s[i]=0; }
02402             sprintf(s,"GET http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
02403         } else {
02404             connHost = EMMA_SERVER_HOST;
02405             connPort = EMMA_SERVER_PORT;
02406             for(int i=0; i<sizeof(s); i++) {
02407                 s[i]=0; }
02408             sprintf(s,"GET /emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST);
02409         }
02410         
02411         t.start();
02412         while(1) {
02413             str = ethGET(connHost,connPort,s);
02414             DBG.printf("str value: %s", str);
02415             if(str.find("OK") != std::string::npos) {
02416                 t.stop();
02417                 ethConnected = true;
02418                 break;    
02419             }
02420             if(t.read_ms() > 5000) {
02421                 t.stop();
02422                 ethConnected = false;
02423                 break;    
02424             }
02425         }
02426     } else {
02427         ethConnected = false;    
02428     }
02429 }
02430 
02431 void isWiFiConnected(void) {    //WARNING: should be run in emmaModeRegister and emmaModeOperation only - limitation with esp, after MODE=B, cannot go to MODE=S
02432     char s[512];
02433     int connPort;
02434     string connHost;
02435     string str;
02436     Timer t;
02437     
02438     if(wifiAvailable) {
02439         _ESP.printf("MODE=B");
02440         if(useProxy) {
02441             connHost = proxySERVER;
02442             sscanf(proxyPORT.c_str(),"%d",&connPort);
02443             for(int i=0; i<sizeof(s); i++) {
02444                 s[i]=0; }
02445             sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
02446         } else {
02447             connHost = EMMA_SERVER_HOST;
02448             connPort = EMMA_SERVER_PORT;
02449             for(int i=0; i<sizeof(s); i++) {
02450                 s[i]=0; }
02451             sprintf(s,"/emma/api/controller/test");
02452         }
02453         wait(1);
02454         t.start();
02455         while(!esp.ready() && t.read_ms() < 5000);
02456         t.stop();
02457         if(rest.begin(connHost.c_str(),connPort,false)) {
02458             //DBG.printf("rest begin\r\n");
02459             esp.process();
02460             rest.get(s);
02461             for(int i=0; i<sizeof(s); i++) {
02462                 s[i]=0; }
02463             rest.getResponse(s,sizeof(s));
02464             str = s;
02465             //DBG.printf("response:%s\r\n",s);
02466             if(str.find("OK") != std::string::npos) {
02467                 wifiConnected = true;
02468             }
02469         } else {
02470             wifiConnected = false;    
02471         }
02472     } else {
02473         wifiConnected = false;    
02474     }
02475 }
02476 
02477 void isGprsConnected(void) {
02478         
02479 }
02480 
02481 void addChar(char *s, char c) {
02482     uint16_t k;     //customized for EMS
02483     k = strlen(s);
02484     s[k] = c;
02485     s[k + 1] = 0;
02486 }
02487 
02488 void rcvReply(char *r, int to) {
02489     Timer t;
02490     bool ended = false;
02491     char c;
02492     
02493     strcpy(r,"");
02494     t.start();
02495     while(!ended) {
02496         if(_ESP.readable()) {
02497             c = _ESP.getc();
02498             addChar(r,c);
02499             t.start();
02500         }
02501         if(t.read_ms() > to) {
02502             ended = true;    
02503         }    
02504     }
02505     addChar(r, 0x00);    
02506 }
02507 
02508 string calculateMD5(string text) {
02509     char s[64];
02510     memset(s,0,sizeof(s));  //for unknown reason, after reading UID, the 's' will contaion UID data
02511     uint8_t hash[16];
02512     MD5::computeHash(hash, (uint8_t*)text.c_str(), strlen(text.c_str()));
02513     for(int i=0; i<16; ++i) {
02514         sprintf(s,"%s%02x",s,hash[i]);
02515     }
02516     return s;
02517 }
02518 
02519 bool writeFirmwareHexToChar(string value) {
02520     FILE *fp;
02521     char s[32];
02522     int number;
02523     string chunk;
02524     
02525     sprintf(s,"/sd/newFirmware/firmware.bin");
02526     fp = fopen(s,"a");
02527     if(fp != NULL) {
02528         for(int ch=0; ch<value.size(); ch+=2) {
02529             chunk = value.substr(ch,2);
02530             sscanf(chunk.c_str(),"%x",&number);
02531             fprintf(fp,"%c",number);
02532         }
02533         fclose(fp);
02534         return true;
02535     }
02536     return false;
02537 }
02538 
02539 bool clearFirmware(void) {
02540     FILE *fp;
02541     char s[32];
02542     
02543     sprintf(s,"/sd/newFirmware/firmware.bin");
02544     fp = fopen(s,"w");
02545     if(fp != NULL) {
02546         fprintf(fp,"");
02547         fclose(fp);
02548         return true;
02549     }
02550     return false;
02551 }
02552 /*end emma private function*/