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

Files at this revision

API Documentation at this revision

Comitter:
arsenalist
Date:
Sat Jul 11 09:38:12 2015 +0000
Parent:
7:7e8c6ad3fd64
Child:
9:a58c04da4476
Commit message:
Working settings mode with eth.

Changed in this revision

ADE7758.lib Show annotated file Show diff for this revision Revisions of this file
emmaCode.cpp Show annotated file Show diff for this revision Revisions of this file
emmaCode.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
tuanpm.lib Show annotated file Show diff for this revision Revisions of this file
--- a/ADE7758.lib	Fri Jul 10 05:26:04 2015 +0000
+++ b/ADE7758.lib	Sat Jul 11 09:38:12 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Emma/code/ADE7758_Edited/#73d2be0396c0
+http://developer.mbed.org/teams/Emma/code/ADE7758_Edited/#d89923406a71
--- a/emmaCode.cpp	Fri Jul 10 05:26:04 2015 +0000
+++ b/emmaCode.cpp	Sat Jul 11 09:38:12 2015 +0000
@@ -5,8 +5,8 @@
 
 //init wifi port
 Serial _ESP(PA_2, PA_3);    //tx, rx
-//init espduino
-ESP esp(&_ESP, &DBG, ESP_CH_PD, ESP_BAUD);
+//init espduino - without ch_pd pin
+ESP esp(&_ESP, &DBG, ESP_BAUD);
 //init wifi mqtt
 ESPMQTT mqtt(&esp);
 //init wifi rest
@@ -18,10 +18,10 @@
 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
+SDFileSystem sd(PA_7, PA_6, PA_5, PB_3, "sd"); //mosi, miso, sck, cs
 
-//init ade7758
-ADE7758 ADE(PB_5, PD_2, PB_4, PB_6, PB_7);
+//init ade7758 - without cs pin
+ADE7758 ADE(PB_6, PB_4, PB_5, PB_2);  //mosi, miso, sck, irq
 
 //emma settings
 string emmaUID;
@@ -115,9 +115,12 @@
     useProxy = false;
     DBG.printf("proxy:%d\r\n",useProxy);
     
-    //preset available interface
-    wifiAvailable = true;
-    ethAvailable = true;
+    //check available interface
+    isEthAvailable();           //check whether cable is connected
+    wifiAvailable = true;       //we assume wifi will always ready to use
+    DBG.printf("eth:%d\r\n",ethAvailable);
+    DBG.printf("wifi:%d\r\n",wifiAvailable);
+    DBG.printf("gprs:%d\r\n",gprsAvailable);
 }
 void emmaModeWiFiConfig(void) {
     string str;
@@ -174,58 +177,16 @@
     char s[32];
     string str;
     
+    //create settings dir
     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) {
+    if(ethAvailable) {
         DBG.printf("emmaModeSettings - eth\r\n");
         
         TCPSocketServer svr;
@@ -300,8 +261,52 @@
                 clientSock.close();
             }
         }
+    } else 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 {
-        DBG.printf("no wifi or eth found\r\n");
+        DBG.printf("no eth or wifi available\r\n");
     }
 }
 void emmaModeRegister(void) {
@@ -319,7 +324,10 @@
     Timer t;
     
     //check connected interface
-    connectedIface();
+    //connectedIface();
+    isEthConnected();
+    isWiFiConnected();
+    isGprsConnected();
     DBG.printf("ethConnected:%d\r\n",ethConnected);
     DBG.printf("wifiConnected:%d\r\n",wifiConnected);
     
@@ -608,7 +616,10 @@
     Timer tNodes;
     
     //check connected interface
-    connectedIface();
+    //connectedIface();
+    isEthConnected();
+    isWiFiConnected();
+    isGprsConnected();
     DBG.printf("ethConnected:%d\r\n",ethConnected);
     DBG.printf("wifiConnected:%d\r\n",wifiConnected);
     
@@ -1325,23 +1336,6 @@
 /*end energy related*/
 
 /*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("MQTT Connected\r\n");
     char mqttTopic[64];
@@ -1351,48 +1345,9 @@
 void mqttDisconnected(void* response) {
     DBG.printf("MQTT 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;    
-        }
-    }
-}
 void rxInterrupt(void) {
     char c;
     
@@ -1706,7 +1661,8 @@
 /*end emma connection function*/
 
 /*start emma private function*/
-void connectedIface(void) { //WARNING: should be run in emmaModeRegister and emmaModeOperation
+/*
+void connectedIface(void) { //WARNING: should be run in emmaModeRegister and emmaModeOperation only - problem with esp, after MODE=B, cannot go back to MODE=S
     char s[512];
     int connPort;
     string connHost;
@@ -1788,6 +1744,105 @@
     
     //gprs interface    
 }
+*/
+void isEthAvailable(void) {
+    if(ipstack.getEth().linkstatus()) {
+        ethAvailable = true;    
+    } else {
+        ethAvailable = false;    
+    }
+}
+
+void isEthConnected(void) {
+    char s[512];
+    int connPort;
+    string connHost;
+    string str;
+    Timer t;
+    
+    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/controller/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/controller/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;    
+    }
+}
+
+void isWiFiConnected(void) {    //WARNING: should be run in emmaModeRegister and emmaModeOperation only - limitation with esp, after MODE=B, cannot go to MODE=S
+    char s[512];
+    int connPort;
+    string connHost;
+    string str;
+    Timer t;
+    
+    if(wifiAvailable) {
+        _ESP.printf("MODE=B");
+        if(useProxy) {
+            connHost = proxySERVER;
+            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"http://%s:%d/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",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/controller/test");
+        }
+        wait(1);
+        t.start();
+        while(!esp.ready() && t.read_ms() < 5000);
+        t.stop();
+        if(rest.begin(connHost.c_str(),connPort,false)) {
+            //DBG.printf("rest begin\r\n");
+            esp.process();
+            rest.get(s);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            rest.getResponse(s,sizeof(s));
+            str = s;
+            //DBG.printf("response:%s\r\n",s);
+            if(str.find("OK") != std::string::npos) {
+                wifiConnected = true;
+            }
+        } else {
+            wifiConnected = false;    
+        }
+    } else {
+        wifiConnected = false;    
+    }
+}
+
+void isGprsConnected(void) {
+        
+}
 
 void addChar(char *s, char c) {
     uint16_t k;     //customized for EMS
--- a/emmaCode.h	Fri Jul 10 05:26:04 2015 +0000
+++ b/emmaCode.h	Sat Jul 11 09:38:12 2015 +0000
@@ -34,7 +34,6 @@
 #define IF_GPRS         0x02
 
 //wifi
-#define ESP_CH_PD   PA_4
 #define ESP_BAUD    19200
 
 //mqtt
@@ -81,16 +80,11 @@
 /*end energy related*/
 
 /*start wifi mqtt*/
-void wifiCb(void* response);
 void mqttConnected(void* response);
 void mqttDisconnected(void* response);
-void mqttData(void* response);
-void mqttDataManual(void);
-void mqttPublished(void* response);
 /*end wifi mqtt*/
 
 /*start wifi rest*/
-void restWifiCb(void* response);
 void rxInterrupt(void);
 void checkRxBuffer(void);
 /*end wifi rest*/
@@ -119,7 +113,11 @@
 /*end emma connection function*/
 
 /*start emma private function*/
-void connectedIface(void);
+//void connectedIface(void);
+void isEthAvailable(void);
+void isEthConnected(void);
+void isWiFiConnected(void);
+void isGprsConnected(void);
 void addChar(char *s, char c);
 void rcvReply(char *r, int to);
 string calculateMD5(string text);
--- a/main.cpp	Fri Jul 10 05:26:04 2015 +0000
+++ b/main.cpp	Sat Jul 11 09:38:12 2015 +0000
@@ -1,6 +1,6 @@
 #include "emmaCode.h"
 
-int mode = MODE_OPERATION;
+int mode = MODE_SETTINGS;
 int main() {
     //init touchscreen and read input
     emmaInit();
--- a/tuanpm.lib	Fri Jul 10 05:26:04 2015 +0000
+++ b/tuanpm.lib	Sat Jul 11 09:38:12 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Emma/code/tuanpm/#1a8836a12df4
+http://developer.mbed.org/teams/Emma/code/tuanpm/#90e7378c1eda