emma controller code in production board v1

Dependencies:   ADE7758_v1 Crypto DHT11 MQTT MbedJSONValueEmma SDFileSystem TFT_ILI9341 SWSPI SetRTC TFT_fonts Touch W5500Interface mbed-rtos mbed-src SoftSerial

Fork of emma_controller_energy by Emma

Revision:
0:f4e449fa34d7
Child:
1:d314a43ae6f7
diff -r 000000000000 -r f4e449fa34d7 emmaCode.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/emmaCode.cpp	Fri Jul 03 07:05:21 2015 +0000
@@ -0,0 +1,1677 @@
+#include "emmaCode.h"
+
+//init debug port
+Serial DBG(PA_9, PA_10);    //tx, rx
+
+//init wifi port
+Serial _ESP(PA_2, PA_3);    //tx, rx
+//init espduino
+ESP esp(&_ESP, &DBG, ESP_CH_PD, ESP_BAUD);
+//init wifi mqtt
+ESPMQTT mqtt(&esp);
+//init wifi rest
+REST rest(&esp);
+
+//init eth port
+SPI spi(PB_15, PB_14, PB_13);               //mosi, miso, sck
+MQTTEthernet ipstack(&spi, PB_12, PC_6);    //spi, cs, reset
+MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
+
+//init sd card
+SDFileSystem sd(PA_7, PA_6, PA_5, PC_12, "sd"); //mosi, miso, sck, cs
+
+//init ade7758
+ADE7758 ADE(PB_5, PD_2, PB_4, PB_6, PB_7);
+
+//emma settings
+string emmaUID;
+string platformDOMAIN;
+string platformKEY;
+string platformSECRET;
+string wifiSSID;
+string wifiPASS;
+string gprsAPN;
+string proxySERVER;
+string proxyPORT;
+string proxyAUTH;
+
+//ade7758 variables
+uint16_t AWattHrValue, BWattHrValue, CWattHrValue;
+uint16_t AVAHrValue, BVAHrValue, CVAHrValue;
+long AWattHrSum = 0;
+long BWattHrSum = 0;    
+long CWattHrSum = 0;
+float AWattHr, BWattHr, CWattHr;
+
+//variables
+bool ethAvailable = false;
+bool wifiAvailable = false;
+bool gprsAvailable = false;
+bool ethConnected = false;
+bool wifiConnected = false;
+bool useProxy = false;
+bool newCommand = false;
+string globalCommand;
+//char globCmd[255];
+//char *globCmd;
+string globCmd;
+
+void emmaInit(void) {
+    DBG.baud(19200);
+    DBG.printf("\r\nemmaInit\r\n");
+    
+    //read settings
+    //readSetting("emmaUID");         //sd card need to be read once before working correctly
+    //emmaUID = readSetting("emmaUID");
+    emmaUID = "066eff575349896767073038";
+    DBG.printf("emmaUID:%s\r\n",emmaUID.c_str());
+    //platformDOMAIN = readSetting("platformDOMAIN");
+    platformDOMAIN = "testdulu";
+    DBG.printf("platformDOMAIN:%s\r\n",platformDOMAIN.c_str());
+    //platformKEY = readSetting("platformKEY");
+    platformKEY = "5980e444-81dd-47ba-8222-6a40bc94fdce";
+    DBG.printf("platformKEY:%s\r\n",platformKEY.c_str());
+    //platformSECRET = readSetting("platformSECRET");
+    platformSECRET = "3ca8ec0239fda2b6d12ba1580c91a052";
+    DBG.printf("platformSECRET:%s\r\n",platformSECRET.c_str());
+    //proxySERVER = readSetting("proxySERVER");
+    DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str());
+    //proxyPORT = readSetting("proxyPORT");
+    DBG.printf("proxyPORT:%s\r\n",proxyPORT.c_str());
+    //proxyAUTH = readSetting("proxyAUTH");
+    DBG.printf("proxyAUTH:%s\r\n",proxyAUTH.c_str());
+    
+    //check proxy
+    if(!proxySERVER.empty() && !proxyPORT.empty() && !proxyAUTH.empty()) {
+        useProxy = true;
+    } else {
+        useProxy = false;    
+    }
+    //testing purpose
+    useProxy = false;
+    DBG.printf("proxy:%d\r\n",useProxy);
+    
+    //preset available interface
+    wifiAvailable = true;
+    ethAvailable = true;
+}
+void emmaModeWiFiConfig(void) {
+    string str;
+    
+    if(wifiAvailable) {
+        DBG.printf("emmaModeWiFiConfig\r\n");
+        
+        //set wifi module to configuration
+        _ESP.printf("MODE=C");
+        while(1) {
+            char rcv[128] = {};
+            rcvReply(rcv,3000);
+            str = rcv;
+            if(str.find("SC_STATUS_FIND_CHANNEL") != std::string::npos)
+                break;
+        }
+        DBG.printf("entering wifi configuration mode\r\n");
+        while(1) {
+            char rcv[128] = {};
+            rcvReply(rcv,3000);
+            str = rcv;
+            if(str.find("MODE=C OK") != std::string::npos) {
+                //save wifiSSID and wifiPASS
+                if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                    str.erase(str.begin(),str.begin()+str.find("[")+1);
+                    str.erase(str.begin()+str.find("]"),str.end());
+                            
+                    MbedJSONValue jsonValue;
+                    parse(jsonValue,str.c_str());
+                        
+                    char *parameter[2] = {"wifiSSID","wifiPASS"};
+                        
+                    for(int i=0; i<2; i++) {
+                        if(jsonValue.hasMember(parameter[i])) {
+                            string val = jsonValue[parameter[i]].get<std::string>();
+                            int st = writeSetting(parameter[i],val.c_str());
+                            if(st) {
+                                DBG.printf("%s is saved\r\n",parameter[i]);
+                            } else {
+                                DBG.printf("%s is not saved\r\n",parameter[i]);
+                            }
+                        }
+                    }
+                } 
+            }
+        }    
+    } else {
+        DBG.printf("no wifi found\r\n");
+    }
+}
+void emmaModeSettings(void) {
+    bool clientIsConnected = false;
+    bool serverIsListened = false;
+    char s[32];
+    string str;
+    
+    mkdir("/sd/settings",0777);
+    //get and write emmaUID
+    string uid = getUID();
+    sprintf(s,"(%s)",uid.c_str());
+    uid = s;
+    writeSetting("emmaUID",uid);
+    
+    if(wifiAvailable) {
+        DBG.printf("emmaModeSettings - wifi\r\n");
+        
+        _ESP.printf("MODE=S");
+        while(1) {
+            char rcv[128] = {};
+            rcvReply(rcv,3000);
+            str = rcv;
+            if(str.find("MODE=S_OK") != std::string::npos)
+                break;
+        }
+        DBG.printf("entering settings mode\r\n");
+        while(1) {
+            char rcv[512] = {};
+            rcvReply(rcv,3000);
+            //DBG.printf("rcv:%s\r\n",rcv);
+            str = rcv;
+            if(str.find("MODE=S_Config") != std::string::npos) {
+                //save gprs and proxy setting
+                if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                    str.erase(str.begin(),str.begin()+str.find("[")+1);
+                    str.erase(str.begin()+str.find("]"),str.end());
+                            
+                    MbedJSONValue jsonValue;
+                    parse(jsonValue,str.c_str());
+                        
+                    char *parameter[4] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH"};
+                        
+                    for(int i=0; i<4; i++) {
+                        if(jsonValue.hasMember(parameter[i])) {
+                            string val = jsonValue[parameter[i]].get<std::string>();
+                            int st = writeSetting(parameter[i],val.c_str());
+                            if(st) {
+                                DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
+                            } else {
+                                DBG.printf("%s is not saved\r\n",parameter[i]);
+                            }
+                        }
+                    }
+                }    
+            } else if(str.find("connect") != std::string::npos) {
+                DBG.printf("connection success!\r\n");
+            }
+        }
+    } else if(ethAvailable) {
+        DBG.printf("emmaModeSettings - eth\r\n");
+        
+        TCPSocketServer svr;
+        TCPSocketConnection clientSock;
+    
+        if(svr.bind(SERVER_PORT) < 0) {
+            DBG.printf("tcp server bind failed\r\n");    
+        } else {
+            DBG.printf("tcp server bind success\r\n");
+            serverIsListened = true;    
+        }
+    
+        DBG.printf("please connect to %s\r\n",ipstack.getEth().getIPAddress());
+        
+        if(svr.listen(1) < 0) {
+            DBG.printf("tcp server listen failed\r\n");    
+        } else {
+            DBG.printf("tcp server is listening...\r\n");    
+        }
+    
+        clientSock.set_blocking(false,30000);   //timeout after 30sec
+    
+        //listening
+        while (serverIsListened) {
+            if(svr.accept(clientSock) < 0) {
+                DBG.printf("failed to accept connection\r\n");    
+            } else {
+                DBG.printf("connection success!\r\nIP: %s\r\n",clientSock.get_address());
+                clientIsConnected = true;
+            
+                while(clientIsConnected) {
+                    char buffer[1024] = {};
+                    switch(clientSock.receive(buffer,1023)) {
+                        case 0:
+                            DBG.printf("received buffer is empty\r\n");
+                            clientIsConnected = false;
+                            break;
+                        case -1:
+                            DBG.printf("failed to read data from client\r\n");
+                            clientIsConnected = false;
+                            break;
+                        default:
+                            //DBG.printf("received data: %d\r\n%s\r\n",strlen(buffer),buffer);
+                            DBG.printf("\r\n");
+                        
+                            str = buffer;
+                            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                                str.erase(str.begin(),str.begin()+str.find("[")+1);
+                                str.erase(str.begin()+str.find("]"),str.end());
+                            
+                                MbedJSONValue jsonValue;
+                                parse(jsonValue,str.c_str());
+                        
+                                char *parameter[4] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH"};
+                        
+                                for(int i=0; i<4; i++) {
+                                    if(jsonValue.hasMember(parameter[i])) {
+                                        string val = jsonValue[parameter[i]].get<std::string>();
+                                        int st = writeSetting(parameter[i],val.c_str());
+                                        if(st) {
+                                            DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
+                                        } else {
+                                            DBG.printf("%s is not saved\r\n",parameter[i]);
+                                        }
+                                    }
+                                }
+                            }
+                            break;     
+                    }    
+                }
+                DBG.printf("close connection\r\n");
+                clientSock.close();
+            }
+        }
+    } else {
+        DBG.printf("no wifi or eth found\r\n");
+    }
+}
+void emmaModeRegister(void) {
+    bool emmaGetRegKey = false;
+    bool emmaRegistered = false;
+    char s[512];
+    char r[256];
+    int connPort;
+    int loop = 0;
+    string connData;
+    string connHost;
+    string hmac;
+    string str;
+    string regKey;
+    Timer t;
+    
+    //check connected interface
+    connectedIface();
+    DBG.printf("ethConnected:%d\r\n",ethConnected);
+    DBG.printf("wifiConnected:%d\r\n",wifiConnected);
+    
+    //calculate hmac
+    for(int i=0; i<sizeof(s); i++) {
+        s[i]=0; }
+    sprintf(s,"emma-%s",emmaUID.c_str());   
+    hmac = calculateMD5(s);
+    DBG.printf("hmac:%s\r\n",hmac.c_str());
+    
+    if(wifiConnected) {
+        DBG.printf("emmaModeRegister - wifi\r\n");
+        
+        _ESP.printf("MODE=B");
+        wait(1);
+        while(!esp.ready());
+        
+        //set connHost, connPort
+        if(useProxy) {
+            connHost = proxySERVER;
+            sscanf(proxyPORT.c_str(),"%d",&connPort);
+        } else {
+            connHost = EMMA_SERVER_HOST;
+            connPort = EMMA_SERVER_PORT;
+        }
+        
+        //rest begin
+        if(!rest.begin(connHost.c_str(),connPort,false)) {
+            DBG.printf("EMMA: fail to setup rest");
+            while(1);    
+        }
+        //wifiConnected = true;   //with custom firmware, wifi module should connect automatically
+        
+        esp.process();
+        if(wifiConnected) {
+            //set connData
+            if(useProxy) {
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                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());
+                connData = s;
+            } else {
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
+                connData = s;
+            }
+         
+            //register
+            while(!emmaGetRegKey) {
+                rest.get(connData.c_str());
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                rest.getResponse(s,sizeof(s));
+                DBG.printf("rsp reg:%s\r\n",s);
+
+                //check and save platform setting
+                str = s;
+                if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                    str.erase(str.begin(),str.begin()+str.find("[")+1);
+                    str.erase(str.begin()+str.find("]"),str.end());
+        
+                    MbedJSONValue jsonValue;
+                    parse(jsonValue,str.c_str());
+    
+                    char *parameter[4] = {"platformDOMAIN","platformKEY","platformSECRET","registrationKey"};
+    
+                    //save platform parameter
+                    writeSetting(parameter[0],"()");    //sd card need to be initialized
+                    for(int i=0; i<3; i++) {
+                        if(jsonValue.hasMember(parameter[i])) {
+                            string val = jsonValue[parameter[i]].get<std::string>();
+                            int st = writeSetting(parameter[i],val.c_str());
+                            if(st) {
+                                DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
+                            } else {
+                                DBG.printf("%s is not saved\r\n",parameter[i]);
+                            }
+                        }
+                    }
+        
+                    //get registrationKey
+                    if(jsonValue.hasMember(parameter[3])) {
+                        string val = jsonValue[parameter[3]].get<std::string>();
+                        if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
+                            val.erase(val.begin(),val.begin()+val.find("(")+1);
+                            val.erase(val.begin()+val.find(")"),val.end());
+                            regKey = val;
+                            DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
+                            emmaGetRegKey = true;
+                        }
+                    }
+                }
+            }
+                
+            //calculate hmac
+            for(int i=0; i<sizeof(r); i++) {
+                r[i]=0; }
+            sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
+            hmac = calculateMD5(r);
+            DBG.printf("hmac:%s\r\n",hmac.c_str());
+            
+            //set connData
+            if(useProxy) {
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                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());
+                connData = s;
+            } else {
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                sprintf(s,"/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
+                connData = s;
+            }
+                
+            //verify registration
+            while(!emmaRegistered && loop < 12){
+                rest.get(connData.c_str());
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                rest.getResponse(s,sizeof(s));
+                DBG.printf("rsp vrf:%s\r\n",s);
+                
+                //check verification
+                str = s;
+                if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                    str.erase(str.begin(),str.begin()+str.find("[")+1);
+                    str.erase(str.begin()+str.find("]"),str.end());
+                    
+                    MbedJSONValue jsonValue;
+                    parse(jsonValue,str.c_str());
+                    if(jsonValue.hasMember("user")) {
+                        string val = jsonValue["user"].get<std::string>();
+                        DBG.printf("%s is registered\r\n",val.c_str());
+                        emmaRegistered = true;
+                    }
+                }
+                wait(5);
+                loop++;
+            }
+            
+            //check whether registration success
+            if(emmaRegistered) {
+                DBG.printf("registration successful\r\n");
+            } else {
+                DBG.printf("registration unsuccessful\r\n");
+            }
+            while(1);
+        }
+        
+    } else if(ethConnected) {
+        DBG.printf("emmaModeRegister - eth\r\n");
+        
+        //set connHost, connPort, connData
+        if(useProxy) {
+            DBG.printf("use proxy\r\n");
+            connHost = proxySERVER;
+            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"GET http://%s:%d/emma/api/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);
+            connData = s;
+        } else {
+            DBG.printf("no proxy\r\n");
+            connHost = EMMA_SERVER_HOST;
+            connPort = EMMA_SERVER_PORT;
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            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);
+            connData = s;
+        }
+        
+        //register
+        while(!emmaGetRegKey) {
+            str = "";
+            str = ethGET(connHost,connPort,connData);
+            DBG.printf("rsp reg:%s\r\n",str.c_str());
+
+            //check and save platform setting
+            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                str.erase(str.begin(),str.begin()+str.find("[")+1);
+                str.erase(str.begin()+str.find("]"),str.end());
+        
+                MbedJSONValue jsonValue;
+                parse(jsonValue,str.c_str());
+    
+                char *parameter[4] = {"platformDOMAIN","platformKEY","platformSECRET","registrationKey"};
+    
+                //save platform parameter
+                writeSetting(parameter[0],"()");    //sd card need to be initialized
+                for(int i=0; i<3; i++) {
+                    if(jsonValue.hasMember(parameter[i])) {
+                        string val = jsonValue[parameter[i]].get<std::string>();
+                        int st = writeSetting(parameter[i],val.c_str());
+                        if(st) {
+                            DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
+                        } else {
+                            DBG.printf("%s is not saved\r\n",parameter[i]);
+                        }
+                    }
+                }
+        
+                //get registrationKey
+                if(jsonValue.hasMember(parameter[3])) {
+                    string val = jsonValue[parameter[3]].get<std::string>();
+                    if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
+                        val.erase(val.begin(),val.begin()+val.find("(")+1);
+                        val.erase(val.begin()+val.find(")"),val.end());
+                        regKey = val;
+                        DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
+                        emmaGetRegKey = true;
+                    }
+                }
+            }
+        }
+        
+        //calculate hmac
+        for(int i=0; i<sizeof(r); i++) {
+            r[i]=0; }
+        sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
+        hmac = calculateMD5(r);
+        DBG.printf("hmac:%s\r\n",hmac.c_str());
+        
+        //set connData
+        if(useProxy) {
+            DBG.printf("use proxy\r\n");
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            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);
+            connData = s;
+        } else {
+            DBG.printf("no proxy\r\n");
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            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);
+            connData = s;
+        }
+        
+        //verify registration
+        while(!emmaRegistered && loop < 12){
+            str = "";
+            str = ethGET(connHost,connPort,connData);
+            DBG.printf("rsp vrf:%s\r\n",str.c_str());
+                
+            //check verification
+            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                str.erase(str.begin(),str.begin()+str.find("[")+1);
+                str.erase(str.begin()+str.find("]"),str.end());
+                    
+                MbedJSONValue jsonValue;
+                parse(jsonValue,str.c_str());
+                if(jsonValue.hasMember("user")) {
+                    string val = jsonValue["user"].get<std::string>();
+                    DBG.printf("%s is registered\r\n",val.c_str());
+                    emmaRegistered = true;
+                }
+            }
+            wait(5);
+            loop++;
+        }
+            
+        //check whether registration success
+        if(emmaRegistered) {
+            DBG.printf("registration successful\r\n");
+        } else {
+            DBG.printf("registration unsuccessful\r\n");
+        }
+        while(1);
+        
+    } else if(gprsAvailable) {
+        DBG.printf("emmaModeRegister - gprs\r\n");
+        
+    } else {
+        DBG.printf("no wifi, eth, or gprs found\r\n");
+    }
+}
+
+void emmaModeOperation(void) {
+    char mqttClientId[32];
+    char mqttTopic[64];
+    char mqttPayload[64];
+    char s[512];
+    int loop=0;
+    string hmac;
+    string str;
+    Timer t;
+    Timer tEnergy;
+    Timer tNodeTemp;
+    
+    //DBG.printf("emmaModeOperation\r\n");
+    
+    //check connected interface
+    connectedIface();
+    DBG.printf("ethConnected:%d\r\n",ethConnected);
+    DBG.printf("wifiConnected:%d\r\n",wifiConnected);
+    
+    //check firmware update
+    
+    //execute last state of switches on board
+    
+    //get list of nodes from server
+    
+    //calculate hmac
+    for(int j=0; j<sizeof(s); j++) {
+        s[j]=0; }
+    sprintf(s,"emma-%s",emmaUID.c_str());   
+    hmac = calculateMD5(s);
+    DBG.printf("hmac:%s\r\n",hmac.c_str());
+    
+    //operation wifi
+    if(wifiConnected) {
+        DBG.printf("emmaModeOperation - wifi\r\n");
+    
+        //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work
+        //_ESP.printf("MODE=B");
+        //wait(1);
+        //while(!esp.ready());
+        
+        DBG.printf("emma: setup mqtt client\r\n");
+        sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
+    
+        if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
+            mqtt.lwt("/lwt", "offline", 0, 0);
+            mqtt.connectedCb.attach(&mqttConnected);
+            mqtt.disconnectedCb.attach(&mqttDisconnected);
+            mqtt.publishedCb.attach(&mqttPublished);
+            mqtt.dataCb.attach(&mqttData);
+            mqtt.connect(MQTT_HOST,MQTT_PORT,false);
+            DBG.printf("emma: success to setup mqtt\r\n");    
+        }
+        
+        DBG.printf("emma: system started\r\n");
+        
+        //define thread
+        osThreadDef(energyThread, osPriorityBelowNormal, (4*DEFAULT_STACK_SIZE));
+        
+        //set ade7758 parameter
+        ADE.begin();
+        ADE.AVRMSCalib = 1526873.00;
+        ADE.BVRMSCalib = 534202.00;
+        ADE.CVRMSCalib = 456990.00;
+        ADE.AIRMSCalib = 39248.00;
+        ADE.BIRMSCalib = 654.00;
+        ADE.CIRMSCalib = 111.00;
+        
+        ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET);
+        
+        ADE.write16bits(AWG, 0);
+        ADE.write16bits(BWG, 0);
+        ADE.write16bits(CWG, 0);
+        ADE.write16bits(AVAG, 0);
+        ADE.write16bits(BVAG, 0);
+        ADE.write16bits(CVAG, 0);
+        
+        ADE.AWhLSB = 0.00010846;
+        ADE.BWhLSB = 0.25075167;
+        ADE.CWhLSB = 0.25075167;
+        
+        ADE.AVAhLSB = 0.00008370;
+        ADE.BVAhLSB = 0;
+        ADE.CVAhLSB = 0;
+        
+        //create thread
+        //osThreadCreate(osThread(energyThread),NULL);
+        
+        t.start();
+        tEnergy.start();
+        tNodeTemp.start();
+        while(1) {
+            esp.process();
+            
+            //fatal exception handler - use only if rest function is used?
+            /*
+            if(!esp.ready()) {  //ready is true
+                DBG.printf("exception handler - start\r\n");
+                while(1) {
+                    _ESP.printf("MODE=B");
+                    char rcv[16] = {};
+                    rcvReply(rcv,1000);
+                    str = rcv;
+                    if(str.find("MODE=B_OK") != std::string::npos)
+                        break;
+                }
+                wait(1);
+                if(mqtt.begin(mqttClientId, platformKEY.c_str(), platformSECRET.c_str(), 120, 1)) {
+                    mqtt.lwt("/lwt", "offline", 0, 0);
+                    mqtt.connectedCb.attach(&mqttConnected);
+                    mqtt.disconnectedCb.attach(&mqttDisconnected);
+                    mqtt.publishedCb.attach(&mqttPublished);
+                    mqtt.dataCb.attach(&mqttData);
+                    mqtt.connect(MQTT_HOST,MQTT_PORT,false);
+                    DBG.printf("emma: success to setup mqtt\r\n");    
+                }
+                DBG.printf("exception handler - finish\r\n");
+            }
+            wait(0.5);
+            */
+            
+            //energy
+            if(tEnergy.read_ms() > 10000) {
+                //DBG.printf("getEnergy\r\n");
+                DBG.printf("WattHR for each phase: %.2f, %.2f, %.2f\r\n", AWattHr, BWattHr, CWattHr);
+                
+                //send energy data
+                if(rest.begin("192.168.131.200",8080,false)) {
+                    DBG.printf("sending energy\r\n");
+                    for(int j=0; j<sizeof(s); j++) {
+                        s[j]=0; }
+                    sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
+                    emmaUID.c_str(),hmac.c_str(),AWattHr,AWattHr,AWattHr);
+                    rest.post("/emma/api/controller/energy/1",s);
+                    for(int j=0; j<sizeof(s); j++) {
+                        s[j]=0; }
+                    rest.getResponse(s,sizeof(s));
+                    DBG.printf("Response: %s\r\n",s);
+                } else {
+                    DBG.printf("not sending energy\r\n");    
+                }
+                
+                tEnergy.reset();    
+            }
+            
+            //nodeTemp
+            /*
+            if(tNodeTemp.read_ms() > 30000) {
+                DBG.printf("getNodeTemperature\r\n");
+                    
+                //get list of nodes
+                string *node;
+                node = readNodeList();
+                    
+                int idx=0;
+                while(!(node[idx].empty())) {
+                    //get node's temperature
+                    string nodeTemp = wifiGetNodeTemp(node[idx]);
+                    DBG.printf("nodeTemp[%d]:%s\r\n",idx,nodeTemp.c_str());
+                        
+                    //send node's temperature
+                    if(rest.begin("192.168.131.200",8080,false)) {
+                        for(int j=0; j<sizeof(s); j++) {
+                            s[j]=0; }
+                        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"mac\":\"%s\",\"value\":%s}",
+                        emmaUID.c_str(),hmac.c_str(),node[idx].c_str(),nodeTemp.c_str());
+                        rest.post("/emma/api/controller/nodetemp",s);
+                        for(int j=0; j<sizeof(s); j++) {
+                            s[j]=0; }
+                        rest.getResponse(s,sizeof(s));
+                        DBG.printf("Response: %s\r\n",s);
+                    }
+                    idx++;
+                }
+                tNodeTemp.reset(); 
+            }
+            */    
+            //publish data
+            //if(t.read_ms() > 30000) {
+            //    sprintf(mqttTopic,"%s/%s/panelTemp",platformDOMAIN.c_str(),emmaUID.c_str());
+            //    sprintf(mqttPayload,"{\"time\":\"20150615-12:32:12\",\"value\":%d}",loop);
+            //    DBG.printf(mqttPayload);
+            //    DBG.printf("\r\n");
+            //    mqtt.publish(mqttTopic,mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelTemp",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelHum",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelGas",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelCh1",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelCh2",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/panelCh3",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/nodeTemp",mqttPayload);
+                //mqtt.publish("gaisbwqreqrfxjc/066eff575349896767073038/cmdExecution","success");
+            //    loop++;
+            //    t.reset();    
+            //}
+            
+            //check for new command
+            /*
+            if(newCommand) {
+                DBG.printf("newCommand\r\n");
+                MbedJSONValue jsonValue;
+                parse(jsonValue,globalCommand.c_str());
+                char *parameter[5] = {"id","nType","nAddr","dType","cmd"};
+                
+                //check if command is valid
+                bool validCommand = true;
+                for(int i=0; i<5; i++) {
+                    validCommand = validCommand && jsonValue.hasMember(parameter[i]);
+                }
+                DBG.printf("command validity:%d\r\n",validCommand);
+                
+                if(validCommand) {
+                    string commandId = jsonValue[parameter[0]].get<std::string>();
+                    string commandNType = jsonValue[parameter[1]].get<std::string>();
+                    string commandNAddr = jsonValue[parameter[2]].get<std::string>();
+                    string commandDType = jsonValue[parameter[3]].get<std::string>();
+                    string commandCmd = jsonValue[parameter[4]].get<std::string>();
+                    
+                    if(commandNType == "0") {       //switch on panel controller
+                        DBG.printf("command for switch\r\n");
+                    }
+                    else if(commandNType == "1") {  //node with mac address
+                        DBG.printf("command for node\r\n");
+                        //get node ip address based on node mac address
+                        string nodeIP = readNodeIP(commandNAddr);
+                        //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
+                        
+                        //get cmd string based on device type and command number
+                        string nodeCmd = readNodeCmd(commandDType,commandCmd);
+                        //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
+                        
+                        //execute command
+                        if(rest.begin(nodeIP.c_str(),REMOTE_TCP_PORT,false)) {
+                            sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
+                            
+                            int trial=0;
+                            string execStatus = "failed";
+                            while(1) {
+                                char rcv[1024] = {};
+                                rest.get("/",s);
+                                rcvReply(rcv,2000);
+                                str = rcv;
+                                if(str.find("REST: Sent") != std::string::npos) {
+                                    DBG.printf("cmd executed\r\n");
+                                    execStatus = "success";
+                                    break;    
+                                }
+                                if(trial>0) {   //two times trial
+                                    DBG.printf("cmd is not executed\r\n");
+                                    break;    
+                                }
+                                trial++;
+                                wait(3);
+                            }
+                            
+                            //send execution status - mqtt
+                            //sprintf(mqttTopic,"%s/%s/executionResult",platformDOMAIN.c_str(),emmaUID.c_str());
+                            //sprintf(mqttPayload,"{\"id\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\"}",
+                            //commandId.c_str(),commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execStatus.c_str());
+                            //DBG.printf(mqttPayload);
+                            //DBG.printf("\r\n");
+                            //mqtt.publish(mqttTopic,mqttPayload);
+                            
+                            //send execution status - rest
+                            wait(5);
+                            if(rest.begin("192.168.131.200",8080,false)) {
+                                sprintf(mqttPayload,"{\"id\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\"}",
+                                commandId.c_str(),commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execStatus.c_str());
+                                //rest.post("/emma/api/controller/test","{\"test\": \"post\"}");
+                                rest.post("/emma/api/controller/test",mqttPayload);
+                                rest.getResponse(s,sizeof(s));
+                                DBG.printf("Response: %s\r\n",s);
+                                for(int j=0; j<sizeof(s); j++) {
+                                    s[j]=0; }
+                            }
+                        }
+                    }
+                }
+                newCommand = false;
+            }
+            */    
+        }
+    }
+    //operation eth
+/*
+    DBG.printf("IP Address:%s\r\n",ipstack.getEth().getIPAddress());
+    DBG.printf("MAC Address:%s\r\n",ipstack.getEth().getMACAddress());
+    ethMQTTAttemptConnect(&client, &ipstack);
+    t.start();
+    DBG.printf("start\r\n");
+    while(true) {
+        if(!ipstack.getEth().linkstatus()) {
+            NVIC_SystemReset();  
+        }
+        //if(t.read_ms() > 5000) {
+        //    if(publish(&client,&ipstack) != 0)
+        //        ethMQTTAttemptConnect(&client, &ipstack);
+        //    t.reset();
+        //}
+        
+        //check for new command
+        if(newCommand) {
+            DBG.printf("newCommand\r\n");
+            DBG.printf("globalCommand: %s\r\n",globalCommand.c_str());
+            //string qw(globCmd);
+            
+            //if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) {
+            //    cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1);
+            //    cmd.erase(cmd.begin()+cmd.find("]"),cmd.end());
+                //globalCommand = cmd;
+                //newCommand = true;
+            //}
+            
+            MbedJSONValue jsonValue;
+            parse(jsonValue,globalCommand.c_str());
+            char *parameter[5] = {"id","nType","nAddr","dType","cmd"};
+                
+            //check if command is valid
+            bool validCommand = true;
+            for(int i=0; i<5; i++) {
+                validCommand = validCommand && jsonValue.hasMember(parameter[i]);
+            }
+            DBG.printf("is command valid:%d\r\n",validCommand);
+                
+            if(validCommand) {
+                string commandId = jsonValue[parameter[0]].get<std::string>();
+                string commandNType = jsonValue[parameter[1]].get<std::string>();
+                string commandNAddr = jsonValue[parameter[2]].get<std::string>();
+                string commandDType = jsonValue[parameter[3]].get<std::string>();
+                string commandCmd = jsonValue[parameter[4]].get<std::string>();
+                    
+                if(commandNType == "0") {       //switch on panel controller
+                    DBG.printf("command for switch\r\n");
+                }
+                else if(commandNType == "1") {  //node with mac address
+                    DBG.printf("command for node\r\n");
+                    //get node ip address based on node mac address
+                    string nodeIP = readNodeIP(commandNAddr);
+                    DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
+                        
+                    //get cmd string based on device type and command number
+                    string nodeCmd = readNodeCmd(commandDType,commandCmd);
+                    DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
+                        
+                    //execute command
+                    int trial=0;
+                    string execStatus="failed";
+                    
+                    while(1) {
+                        sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
+                        str = s;
+                        string rcv = ethGET(nodeIP,REMOTE_TCP_PORT,str);
+                        DBG.printf("response:%s\r\n",rcv.c_str());
+                        str = rcv;
+                        if(str.find("OK") != std::string::npos) {
+                            DBG.printf("cmd executed\r\n");
+                            execStatus = "success";
+                            break;    
+                        }
+                        if(trial>0) {   //two times trial
+                            DBG.printf("cmd is not executed\r\n");
+                            break;    
+                        }
+                        trial++;
+                        wait(3);
+                    }
+                    
+                    //send execution status
+                }
+            }
+            
+            newCommand = false;    
+        } 
+        client.yield(100);  //allow MQTT client to receive message
+    }
+*/    
+    
+    //----------------------------------------------------------------------------------    
+}
+void emmaModeFirmwareDownload(void) {
+    bool emmaGetFirmwareParam = false;
+    
+    DBG.printf("emmaModeFirmwareDownload\r\n");
+    
+    char s[384];
+    string str;
+    string connData;
+    string chunk;
+    
+    //firmware parameter
+    string firmwareVer;
+    string firmwareName;
+    int numPart;
+    
+    //downloading
+    string firmwarePart;
+    string calcMD5;
+    string srvrMD5;
+    bool nextPart;
+    
+    //set wifi to mode bridge
+    _ESP.printf("MODE=B");
+    DBG.printf("set mode bridge\r\n");
+    while(1) {
+        char rcv[128] = {};
+        rcvReply(rcv,3000);
+        str = rcv;
+        if(str.find("MODE=B_OK") != std::string::npos)
+            break;
+    }
+    DBG.printf("MODE=B\r\n");
+    
+    esp.enable();
+    wait(1);
+    while(!esp.ready());
+    
+    if(!rest.begin("candra.tritronik.com",3128,false)) {
+        DBG.printf("EMMA: fail to setup rest");
+        while(1);    
+    }
+    
+    //wifiConnected = true; //with custom firmware, panel should connect wifi automatically
+    useProxy = true;
+    
+    esp.process();
+    //set connData
+    if(useProxy) {
+        for(int i=0; i<sizeof(s); i++) {
+            s[i]=0; }
+        //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());
+        sprintf(s,"http://192.168.128.69/emmaController/firmware/firmwareParameter");
+        connData = s;
+    } else {
+        for(int i=0; i<sizeof(s); i++) {
+            s[i]=0; }
+        //sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
+        sprintf(s,"/emmaController/firmware/firmwareParameter");
+        connData = s;
+    }
+    
+    //get parameter of firmware to be downloaded
+    while(!emmaGetFirmwareParam) {
+        rest.get(connData.c_str());
+        for(int i=0; i<sizeof(s); i++) {
+            s[i]=0; }
+        rest.getResponse(s,sizeof(s));
+        //DBG.printf("rsp param:%s\r\n",s);
+        
+        str = s;
+        if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+            str.erase(str.begin(),str.begin()+str.find("[")+1);
+            str.erase(str.begin()+str.find("]"),str.end());
+            
+            MbedJSONValue jsonValue;
+            parse(jsonValue,str.c_str());
+            
+            char *parameter[2] = {"firmwareVer","numPart"};
+            
+            for(int i=0; i<2; i++) {
+                if(jsonValue.hasMember(parameter[i])) {
+                    string val = jsonValue[parameter[i]].get<std::string>();
+                    if(i==0) {
+                        firmwareVer = val;    
+                    } else if(i==1) {
+                        sscanf(val.c_str(),"%d",&numPart);
+                    }
+                }
+            }
+            
+            if(!firmwareVer.empty() && numPart!=0) {
+                emmaGetFirmwareParam = true;    
+            }
+        }
+    }
+    DBG.printf("firmwareVer:%s\r\n",firmwareVer.c_str());
+    DBG.printf("numPart:%d\r\n",numPart);
+    
+    //clear firmware file
+    while(1) {
+        if(clearFirmware()){
+            DBG.printf("clear firmware on sd card\r\n\r\n");
+            break;
+        }
+        wait(1);
+    }
+    
+    //set connData
+    if(useProxy) {
+        for(int i=0; i<sizeof(s); i++) {
+            s[i]=0; }
+        sprintf(s,"http://192.168.128.69/emmaController/firmware/%s",firmwareVer.c_str());
+        connData = s;
+    } else {
+        for(int i=0; i<sizeof(s); i++) {
+            s[i]=0; }
+        sprintf(s,"/emmaController/firmware/%s",firmwareVer.c_str());
+        connData = s;
+    }
+    
+    //download firmware
+    for(int n=0; n<numPart; n++) {
+        nextPart = false;
+        while(!nextPart) {
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"%s/firmware_Hex_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
+            rest.get(s);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            rest.getResponse(s,sizeof(s));
+            //DBG.printf("rsp[%d]:%s\r\n",n,s);
+        
+            str = s;
+            if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
+                str.erase(str.begin(),str.begin()+str.find("{")+1);
+                str.erase(str.begin()+str.find("}"),str.end());
+                //DBG.printf("firmwarePart[%d]:%s\r\n",n,str.c_str());
+                firmwarePart = str;
+            
+                //calculated MD5
+                calcMD5 = calculateMD5(firmwarePart);
+                //DBG.printf("calcMD5[%d]:%s\r\n",n,calcMD5.c_str());
+            
+                //MD5 from server
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                sprintf(s,"%s/MD5/firmware_MD5_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
+                rest.get(s);
+                for(int i=0; i<sizeof(s); i++) {
+                    s[i]=0; }
+                rest.getResponse(s,sizeof(s));
+            
+                str = s;
+                if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
+                    str.erase(str.begin(),str.begin()+str.find("{")+1);
+                    str.erase(str.begin()+str.find("}"),str.end());
+                    srvrMD5 = str;
+                    //DBG.printf("srvrMD5[%d]:%s\r\n",n,srvrMD5.c_str());
+                
+                    //compare original MD5 vs MD5 from server
+                    if(strcmp(calcMD5.c_str(),srvrMD5.c_str()) == 0) {
+                        //DBG.printf("MD5 correct\r\n");
+                        
+                        //save to sd card
+                        int st = writeFirmwareHexToChar(firmwarePart);
+                        if(st) {
+                            DBG.printf("firmwarePart[%d/%d] written\r\n",n,numPart-1);
+                            nextPart = true;    
+                        }
+                    
+                    } else {
+                        DBG.printf("MD5 incorrect\r\n");    
+                    }
+                }   
+            } else {
+                DBG.printf("retry to fetch firmwarePart[%d]\r\n",n);    
+            }
+            wait(0.5);
+        }
+    }
+    DBG.printf("download finished\r\n");
+}
+
+/*start energyThread*/
+void energyThread(void const*) {
+    while(1) {
+        //DBG.printf("insideEnergyThread\r\n");
+        //osDelay(5000);
+        
+        //calculate energy
+        float period = 0;
+        AWattHrSum = 0;
+        BWattHrSum = 0;
+        CWattHrSum = 0;
+        while(period < 1*60.0) {
+            period += ADE.getAccumulationTime(PHASE_A);
+            ADE.getAccumulatedEnergy(PHASE_A, &AWattHrValue, &BWattHrValue, &CWattHrValue, &AVAHrValue, &BVAHrValue, &CVAHrValue);
+            AWattHrSum += AWattHrValue;
+            BWattHrSum += BWattHrValue;
+            CWattHrSum += CWattHrValue;
+        }
+        AWattHr = AWattHrSum * ADE.AWhLSB;
+        BWattHr = BWattHrSum * ADE.BWhLSB;
+        CWattHr = CWattHrSum * ADE.CWhLSB;
+        //osDelay(5000);
+        
+        DBG.printf("[energyThread finish]\r\n");
+    }
+}
+/*end energyThread*/
+
+/*start wifi mqtt*/
+void wifiCb(void* response) {
+    uint32_t status;
+    RESPONSE res(response);
+    
+    if(res.getArgc() == 1) {
+        res.popArgs((uint8_t*)&status,4);
+        if(status == STATION_GOT_IP) {
+            DBG.printf("WIFI Connected\r\n");
+            mqtt.connect(MQTT_HOST,MQTT_PORT,false);
+            wifiConnected = true;
+        }
+        else {
+            wifiConnected = false;
+            mqtt.disconnect();    
+        }
+    }
+}
+void mqttConnected(void* response) {
+    DBG.printf("Connected\r\n");
+    char mqttTopic[64];
+    sprintf(mqttTopic,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str());
+    mqtt.subscribe(mqttTopic);
+}
+void mqttDisconnected(void* response) {
+    DBG.printf("Disconnected\r\n");    
+}
+void mqttData(void* response) {
+    RESPONSE res(response);
+    
+    //DBG.printf("Received:\r\n");
+    //DBG.printf("topic=");
+    string topic = res.popString();
+    DBG.printf(topic.c_str());
+    DBG.printf("\r\n");
+    
+    //DBG.printf("command=");
+    string cmd = res.popString();
+    DBG.printf(cmd.c_str());
+    DBG.printf("\r\n");
+    
+    //check whether cmd is json
+    if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) {
+        cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1);
+        cmd.erase(cmd.begin()+cmd.find("]"),cmd.end());
+        globalCommand = cmd;
+        newCommand = true;
+    }
+}
+void mqttPublished(void* response) {    
+}
+/*end wifi mqtt*/
+
+/*start wifi rest*/
+void restWifiCb(void* response) {
+    uint32_t status;
+    RESPONSE res(response);
+    
+    if(res.getArgc() == 1) {
+        res.popArgs((uint8_t*)&status,4);
+        if(status == STATION_GOT_IP) {
+            DBG.printf("WIFI Connected\r\n");
+            wifiConnected = true;
+        }
+        else {
+            wifiConnected = false;    
+        }
+    }
+}
+/*end wifi rest*/
+
+/*start eth mqtt*/
+void ethMQTTMessageArrived(MQTT::MessageData& md) {
+    MQTT::Message &message = md.message;
+    DBG.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n",
+    message.qos, message.retained, message.dup, message.id);
+    DBG.printf("Payload: %.*s\r\n", message.payloadlen, (char*)message.payload);
+    //DBG.printf("Payload %s\r\n",message.payload);
+    
+    
+    //check whether cmd is json
+    //char r[255];
+    char *s = (char*)message.payload;
+    string sp(s);
+    //sprintf(s,"%.*s",message.payloadlen, (char*)message.payload);
+    //sprintf(globCmd,"%s",s);
+    //DBG.printf("Payload: %s\r\n", r);
+    //string cmd = r;
+    //globalCommand = cmd;
+    globalCommand = sp;
+    //globCmd = (char*)message.payload;
+    //string globCmd((char*)message.payload);
+    //std::string *sp = static_cast<std::char*>(message.payload);
+    //string sp((char*)message.payload);
+    DBG.printf("String: %s\r\n",sp.c_str());
+    //globCmd = sp;
+    
+    newCommand = true;
+    /*
+    if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) {
+        cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1);
+        cmd.erase(cmd.begin()+cmd.find("]"),cmd.end());
+        globalCommand = cmd;
+        newCommand = true;
+    }
+    */
+}
+
+int ethMQTTConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {
+    //char hostname[] = "q.thingfabric.com";
+    //char hostname[] = "192.168.131.200";
+    //int rc = ipstack->connect(hostname, MQTT_PORT);
+    int rc = ipstack->connect(MQTT_HOST, MQTT_PORT);
+    
+    if(rc!=0)
+        return rc;
+        
+    //MQTT Connect
+    //char clientId[] = "emma/0674ff575349896767072538";
+    char clientId [32];
+    sprintf(clientId,"emma/%s",emmaUID.c_str());
+    //DBG.printf("clientId:%s\r\n",clientId);
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = clientId;
+    data.username.cstring = "761b233e-a49a-4830-a8ae-87cec3dc1086";
+    data.password.cstring = "ef25cf4567fbc07113252f8d72b7faf2";
+    
+    if((rc = client->connect(&data)) == 0) {
+        DBG.printf("connected\r\n");
+    }
+    
+    //MQTT Subscribe
+    //char* topic = "gaisbwqreqrfxjc/0674ff575349896767072538/command";
+    char s[64];
+    sprintf(s,"%s/%s/command",platformDOMAIN.c_str(),emmaUID.c_str());
+    string topic = s;
+    //DBG.printf("topic:%s\r\n",topic.c_str());
+    if((rc=client->subscribe(topic.c_str(),MQTT::QOS0,ethMQTTMessageArrived)) == 0) {
+        DBG.printf("subscribe success!\r\n");    
+    }
+    return rc;
+}
+void ethMQTTAttemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {
+    int retryAttempt = 0;
+    
+    while(!ipstack->getEth().linkstatus()) {
+        DBG.printf("Ethernet link not present. Check cable connection\r\n");
+        wait(1);    
+    }
+    
+    while(ethMQTTConnect(client,ipstack) != 0) {
+        int timeout = 3;
+        DBG.printf("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout);
+        wait(timeout);
+        retryAttempt++;
+    }
+}
+/*end eth mqtt*/
+
+/*start emma settings*/
+string getUID(void) {
+    char s[32];
+    unsigned long *unique = (unsigned long *)BASE_ADDR;
+    sprintf(s,"%08x%08x%08x",unique[0], unique[1], unique[2]);
+    return s;    
+}
+
+string readSetting(string parameter) {
+    FILE *fp;
+    signed char c;
+    int i=0;
+    char s[64];
+    string strS;
+    
+    sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
+    
+    fp = fopen(s,"r");
+    memset(s,0,sizeof(s));
+    if(fp != NULL) {
+        while(1) {
+            c = fgetc(fp);
+            if(c == EOF){
+                break;
+            }
+            s[i] = c;
+            i++;
+        }
+        strS = s;
+        if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
+            strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
+            strS.erase(strS.begin()+strS.find(")"),strS.end());
+        } else {
+            strS = "";    
+        }
+    }
+    fclose(fp);
+    return strS;    
+}
+
+bool writeSetting(string parameter, string value) {
+    FILE *fp;
+    char s[255];
+    
+    sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
+    fp = fopen(s,"w");
+    if(fp != NULL) {
+        fprintf(fp,value.c_str());
+        fclose(fp);
+        return true;
+    }
+    return false;
+}
+/*end emma settings*/
+
+/*start emma node*/
+string readNodeIP(string macAddr) {
+    FILE *fp;
+    signed char c;
+    int i=0;
+    char s[64];
+    string strS;
+    
+    sprintf(s,"/sd/nodeList/%s/nodeIP.txt",macAddr.c_str());
+    
+    fp = fopen(s,"r");
+    memset(s,0,sizeof(s));
+    if(fp != NULL) {
+        while(1) {
+            c = fgetc(fp);
+            if(c == EOF){
+                break;
+            }
+            s[i] = c;
+            i++;
+        }
+        strS = s;
+        if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
+            strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
+            strS.erase(strS.begin()+strS.find(")"),strS.end());
+        } else {
+            strS = "";    
+        }
+    }
+    fclose(fp);
+    return strS;
+}
+
+string readNodeCmd(string dType, string cmd) {
+    FILE *fp;
+    signed char c;
+    int i=0;
+    char s[128];
+    string strS;
+    
+    sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmd.c_str());
+    
+    fp = fopen(s,"r");
+    memset(s,0,sizeof(s));
+    if(fp != NULL) {
+        while(1) {
+            c = fgetc(fp);
+            if(c == EOF){
+                break;
+            }
+            s[i] = c;
+            i++;
+        }
+        strS = s;
+        if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
+            strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
+            strS.erase(strS.begin()+strS.find(")"),strS.end());
+        } else {
+            strS = "";    
+        }
+    }
+    fclose(fp);
+    return strS;    
+}
+
+string *readNodeList(void) {
+    static string nd[10];   //max node
+    DIR *d;
+    struct dirent *p;
+    string q;
+    int i=0;
+    
+    d = opendir("/sd/nodeList");
+    if(d != NULL) {
+        while((p = readdir(d)) != NULL) {
+            q = p->d_name;
+            nd[i] = q;
+            i++;    
+        }    
+    }
+    closedir(d);
+    return nd;
+}
+
+string wifiGetNodeTemp(string macAddr) {
+    int trial=0;
+    string nodeIP = readNodeIP(macAddr);
+    string str;
+    string temp = "0";
+    
+    if(rest.begin(nodeIP.c_str(),REMOTE_TCP_PORT,false)) {
+        while(1) {
+            char rcv[256] = {};
+            rest.get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
+            rest.getResponse(rcv,sizeof(rcv));
+            str = rcv;
+            if(str.find("temp=") != std::string::npos) {
+                str.erase(str.begin(),str.begin()+str.find("temp=")+6);
+                str.erase(str.begin()+str.find("\""),str.end());
+                temp = str;
+                break;
+            }
+            if(trial>1) {   //three times trial
+                break;    
+            }
+            trial++;
+            wait(2);
+        }
+    }
+    return temp;
+}
+/*end emma node*/
+
+/*start emma connection function*/
+string ethGET(string host, int port, string url) {
+    char buf[1024];
+    char s[256];
+    int ret;
+    TCPSocketConnection sock;
+    Timer t;
+    
+    sprintf(s,"%s",url.c_str());
+    sock.connect(host.c_str(),port);
+    sock.send_all(s,sizeof(s)-1);
+    wait(2);
+    
+    //receive return
+    t.start();
+    while(1) {
+        ret = sock.receive(buf, sizeof(buf));
+        if(ret<=0 || t.read_ms() > 10000) {
+            t.stop();
+            break;
+        }    
+    }
+    sock.close();
+    return buf;
+}
+/*end emma connection function*/
+
+/*start emma private function*/
+void connectedIface(void) { //WARNING: should be run in emmaModeRegister and emmaModeOperation
+    char s[512];
+    int connPort;
+    string connHost;
+    string str;
+    Timer t;
+    
+    //wifi interface
+    if(wifiAvailable) {
+        _ESP.printf("MODE=B");
+        if(useProxy) {
+            connHost = proxySERVER;
+            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"http://%s:%d/emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
+        } else {
+            connHost = EMMA_SERVER_HOST;
+            connPort = EMMA_SERVER_PORT;
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"/emma/api/web/test");
+        }
+        wait(1);
+        t.start();
+        while(!esp.ready() && t.read_ms() < 5000);
+        t.stop();
+        if(rest.begin(connHost.c_str(),connPort,false)) {
+            //DBG.printf("rest begin\r\n");
+            esp.process();
+            rest.get(s);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            rest.getResponse(s,sizeof(s));
+            str = s;
+            //DBG.printf("response:%s\r\n",s);
+            if(str.find("OK") != std::string::npos) {
+                wifiConnected = true;
+            }
+        } else {
+            wifiConnected = false;    
+        }
+    } else {
+        wifiConnected = false;    
+    }
+    
+    //eth interface
+    if(ethAvailable) {
+        if(useProxy) {
+            connHost = proxySERVER;
+            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"GET http://%s:%d/emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,EMMA_SERVER_HOST);
+        } else {
+            connHost = EMMA_SERVER_HOST;
+            connPort = EMMA_SERVER_PORT;
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            strcpy(s,"GET /emma/api/web/test HTTP/1.0\nHost: %s\r\n\r\n");
+        }
+        
+        t.start();
+        while(1) {
+            str = ethGET(connHost,connPort,s);
+            if(str.find("OK") != std::string::npos) {
+                t.stop();
+                ethConnected = true;
+                break;    
+            }
+            if(t.read_ms() > 5000) {
+                t.stop();
+                ethConnected = false;
+                break;    
+            }
+        }
+    } else {
+        ethConnected = false;    
+    }
+    
+    //gprs interface    
+}
+
+void addChar(char *s, char c) {
+    uint16_t k;     //customized for EMS
+    k = strlen(s);
+    s[k] = c;
+    s[k + 1] = 0;
+}
+
+void rcvReply(char *r, int to) {
+    Timer t;
+    bool ended = false;
+    char c;
+    
+    strcpy(r,"");
+    t.start();
+    while(!ended) {
+        if(_ESP.readable()) {
+            c = _ESP.getc();
+            addChar(r,c);
+            t.start();
+        }
+        if(t.read_ms() > to) {
+            ended = true;    
+        }    
+    }
+    addChar(r, 0x00);    
+}
+
+string calculateMD5(string text) {
+    char s[64];
+    memset(s,0,sizeof(s));  //for unknown reason, after reading UID, the 's' will contaion UID data
+    uint8_t hash[16];
+    MD5::computeHash(hash, (uint8_t*)text.c_str(), strlen(text.c_str()));
+    for(int i=0; i<16; ++i) {
+        sprintf(s,"%s%02x",s,hash[i]);
+    }
+    return s;
+}
+
+bool writeFirmwareHexToChar(string value) {
+    FILE *fp;
+    char s[32];
+    int number;
+    string chunk;
+    
+    sprintf(s,"/sd/newFirmware/firmware.bin");
+    fp = fopen(s,"a");
+    if(fp != NULL) {
+        for(int ch=0; ch<value.size(); ch+=2) {
+            chunk = value.substr(ch,2);
+            sscanf(chunk.c_str(),"%x",&number);
+            fprintf(fp,"%c",number);
+        }
+        fclose(fp);
+        return true;
+    }
+    return false;
+}
+
+bool clearFirmware(void) {
+    FILE *fp;
+    char s[32];
+    
+    sprintf(s,"/sd/newFirmware/firmware.bin");
+    fp = fopen(s,"w");
+    if(fp != NULL) {
+        fprintf(fp,"");
+        fclose(fp);
+        return true;
+    }
+    return false;
+}
+/*end emma private function*/
\ No newline at end of file