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:
Tue Jul 14 16:48:24 2015 +0000
Parent:
13:e8adfe305dbc
Child:
15:136526c28afb
Commit message:
Working RTC with backup.

Changed in this revision

SetRTC.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
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SetRTC.lib	Tue Jul 14 16:48:24 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/kenjiArai/code/SetRTC/#fc7223031196
--- a/emmaCode.cpp	Tue Jul 14 04:43:01 2015 +0000
+++ b/emmaCode.cpp	Tue Jul 14 16:48:24 2015 +0000
@@ -21,7 +21,7 @@
 SDFileSystem sd(PA_7, PA_6, PA_5, PB_3, "sd"); //mosi, miso, sck, cs
 
 //init ade7758 - without cs pin
-ADE7758 ADE(PB_6, PB_4, PB_5, PB_2);  //mosi, miso, sck, irq
+ADE7758 ADE(PB_6, PB_4, PB_5, PB_7);  //mosi, miso, sck, irq
 
 //init tft lcd
 SPI_TFT_ILI9341 TFT(PA_7, PA_6, PA_5, PA_4, PC_5, PC_4,"TFT");  //mosi, miso, sclk, cs, reset, dc
@@ -395,7 +395,7 @@
                                 MbedJSONValue jsonValue;
                                 parse(jsonValue,str.c_str());
                         
-                                char *parameter[4] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH"};
+                                char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
                         
                                 for(int i=0; i<4; i++) {
                                     if(jsonValue.hasMember(parameter[i])) {
@@ -408,6 +408,15 @@
                                         }
                                     }
                                 }
+                                
+                                //set time
+                                if(jsonValue.hasMember(parameter[4])) {
+                                    string epTime = jsonValue[parameter[4]].get<std::string>();
+                                    int seconds;
+                                    sscanf(epTime.c_str(),"%d",&seconds);
+                                    set_time(seconds);
+                                    DBG.printf("time is set\r\n");
+                                }
                             }
                             break;     
                     }    
@@ -442,7 +451,7 @@
                     MbedJSONValue jsonValue;
                     parse(jsonValue,str.c_str());
                         
-                    char *parameter[4] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH"};
+                    char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
                         
                     for(int i=0; i<4; i++) {
                         if(jsonValue.hasMember(parameter[i])) {
@@ -455,6 +464,15 @@
                             }
                         }
                     }
+                    
+                    //set time
+                    if(jsonValue.hasMember(parameter[4])) {
+                        string epTime = jsonValue[parameter[4]].get<std::string>();
+                        time_t seconds;
+                        sscanf(epTime.c_str(),"%d",&seconds);
+                        set_time(seconds);
+                        DBG.printf("time is set\r\n");
+                    }
                 }    
             } else if(str.find("connect") != std::string::npos) {
                 DBG.printf("connection success!\r\n");
@@ -762,11 +780,13 @@
 
 void emmaModeOperation(void) {
     char mqttClientId[32];
+    char q[32];
     char r[32];
     char s[512];
     int loop=0;
     //string hmac;
     string str;
+    time_t seconds;
     Timer t;
     Timer tNodes;
     
@@ -947,7 +967,7 @@
         ADE.write16bits(BVAG, 0);
         ADE.write16bits(CVAG, 0);
         
-        ADE.AWhLSB = 0.00006025556;
+        ADE.AWhLSB = 0.000001192;//0.00006025556;
         ADE.BWhLSB = 0.25075167;
         ADE.CWhLSB = 0.25075167;
         
@@ -1091,8 +1111,13 @@
                         XWatt = CWatt;
                     }
                     sprintf(r,"/emma/api/controller/energy/%d",i);
-                    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(),XWattHr,XVrms,XWatt);
+                    seconds = time(NULL);
+                    for(int j=0; j<sizeof(q); j++) {
+                        q[j]=0; }
+                    strftime(q, 32, "%Y-%m-%d %H:%M:%S\r\n",localtime(&seconds));
+                    sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
+                    emmaUID.c_str(),hmac.c_str(),q,XWattHr,XVrms,XWatt);
+                    DBG.printf("dataEnergy:%s\r\n",s);
                     rest.post(r,s);
                     wait(2);
                     if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
@@ -1151,8 +1176,13 @@
                         DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
                     
                         //send node's temp
-                        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"2015-06-09 12:32:12\",\"mac\":\"%s\",\"value\":%s}",
-                        emmaUID.c_str(),hmac.c_str(),nodes[i].macAddr.c_str(),temp.c_str());
+                        seconds = time(NULL);
+                        for(int j=0; j<sizeof(q); j++) {
+                            q[j]=0; }
+                        strftime(q, 32, "%Y-%m-%d %H:%M:%S\r\n",localtime(&seconds));
+                        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":\"%s\",\"value\":%s}",
+                        emmaUID.c_str(),hmac.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
+                        DBG.printf("dataNodeTemp:%s\r\n",s);
                         rest.post("/emma/api/controller/nodetemp",s);
                         wait(2);
                         temp = rxBuf;
@@ -1174,30 +1204,27 @@
             //command
             if(newCommand) {
                 DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
-                TFT.foreground(Orange);
                 TFT.locate(0,160);
                 TFT.printf("                                        ");
                 TFT.locate(0,160);
                 TFT.printf("newCommand");
-                TFT.foreground(White);
                 
                 MbedJSONValue jsonValue;
                 parse(jsonValue,globalCommand.c_str());
-                char *parameter[5] = {"id","nType","nAddr","dType","cmd"};
+                char *parameter[4] = {"nType","nAddr","dType","cmd"};
                 
                 //check whether command is valid
                 bool validCommand = true;
-                for(int i=0; i<5; i++) {
+                for(int i=0; i<4; 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>();
+                    string commandNType = jsonValue[parameter[0]].get<std::string>();
+                    string commandNAddr = jsonValue[parameter[1]].get<std::string>();
+                    string commandDType = jsonValue[parameter[2]].get<std::string>();
+                    string commandCmd = jsonValue[parameter[3]].get<std::string>();
                     
                     if(commandNType == "0") {       //switch on panel controller
                         DBG.printf("command for switch\r\n");
@@ -1226,8 +1253,8 @@
                         
                             //get cmd string based on device type and command number
                             string nodeCmd;
-                            //nodeCmd = readNodeCmd(commandDType,commandCmd);
-                            nodeCmd = "020129A0163B161315131613153C151316131514143C153C16141414141415151315141414141514141415141414143D1514143D141415141414143D14000D"; //turn off
+                            nodeCmd = readNodeCmd(commandDType,commandCmd);
+                            //nodeCmd = "020129A0163B161315131613153C151316131514143C153C16141414141415151315141414141514141415141414143D1514143D141415141414143D14000D"; //turn off
                             //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
                         
                             //execute command
@@ -1244,6 +1271,7 @@
                                     TFT.printf("                                        ");
                                     TFT.locate(0,180);
                                     TFT.printf("cmd is not executed");
+                                    wait(1);
                                     TFT.locate(0,180);
                                     TFT.printf("                                        ");
                                     TFT.foreground(White);
@@ -1258,6 +1286,7 @@
                                     TFT.printf("                                        ");
                                     TFT.locate(0,180);
                                     TFT.printf("cmd is executed");
+                                    wait(1);
                                     TFT.locate(0,180);
                                     TFT.printf("                                        ");
                                     TFT.foreground(White);
@@ -1266,13 +1295,23 @@
                                 }
                                 trial++;
                             }    
+                        } else {
+                            TFT.foreground(Red);
+                            TFT.locate(0,180);
+                            TFT.printf("                                        ");
+                            TFT.locate(0,180);
+                            TFT.printf("node is invalid");
+                            wait(1);
+                            TFT.locate(0,180);
+                            TFT.printf("                                        ");
+                            TFT.foreground(White);    
                         }
                     
                         wait(2);       
                         //send execution result
                         DBG.printf("send execution result\r\n");
-                        sprintf(s,"{\"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(),execResult.c_str());
+                        sprintf(s,"{\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\"}",
+                        commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execResult.c_str());
                     
                         trial=0;
                         while(1) {
@@ -1493,6 +1532,7 @@
         //osDelay(5000);
         
         //calculate energy
+        /*
         DBG.printf("energyThread-start\r\n");
         float period = 0;
         AWattHrSum = 0;
@@ -1505,11 +1545,22 @@
             BWattHrSum += BWattHrValue;
             CWattHrSum += CWattHrValue;
         }
-        AWattHr = AWattHrSum * ADE.AWhLSB;
+        AWattHr = AWattHrSum;// * ADE.AWhLSB;
         BWattHr = BWattHrSum * ADE.BWhLSB;
         CWattHr = CWattHrSum * ADE.CWhLSB;
         
         DBG.printf("energyThread-finish\r\n");
+        */
+        
+        //testing
+        DBG.printf("thread called\r\n");
+        Timer t;
+        t.start();
+        while(t.read_ms() < 5000) {
+            t.stop();
+            t.reset();    
+        }
+        DBG.printf("thread ended\r\n");
     }
 }
 void checkVoltagePower() {
--- a/emmaCode.h	Tue Jul 14 04:43:01 2015 +0000
+++ b/emmaCode.h	Tue Jul 14 16:48:24 2015 +0000
@@ -15,6 +15,7 @@
 #include "SPI_TFT_ILI9341.h"    //lcd
 #include "Touch.h"              //touch
 #include "Arial12x12.h"         //font
+#include "SetRTC.h"             //rtc
 #include <string>
 
 //platform
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Tue Jul 14 16:48:24 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#3a06a10151a6
--- a/mbed.bld	Tue Jul 14 04:43:01 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file