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:
39:f51aa948acee
Parent:
38:e44f35f8129d
Child:
40:77bd44f57ad3
--- a/emmaCode.cpp	Wed Aug 19 12:20:07 2015 +0000
+++ b/emmaCode.cpp	Wed Aug 19 15:04:50 2015 +0000
@@ -647,8 +647,10 @@
 void emmaModeRegister(void) {
     bool emmaGetRegKey = false;
     bool emmaRegistered = false;
+    char connBody[256];
     char s[512];
     char r[256];
+    int connBodyLen;
     int connPort;
     int loop = 0;
     string connData;
@@ -675,14 +677,18 @@
         TFT.locate(0,0);
         TFT.printf(" emmaModeRegister");
         
-        //set connHost, connPort, connData
+        //set connBody, connHost, connPort, connData
+        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+        
         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:%s/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
+            //sprintf(s,"GET http://%s:%s/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
+            //sprintf(s,"POST http://%s:%s/emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: 76\n\n{\"uid\":\"%s\",\"hmac\":\"%s\"}\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),emmaUID.c_str(),hmac.c_str());
+            sprintf(s,"POST http://%s:%s/emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),connBodyLen,connBody);
             connData = s;
         } else {
             DBG.printf("no proxy\r\n");
@@ -691,8 +697,9 @@
             sscanf(restPORT.c_str(),"%d",&connPort);
             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(),restSERVER.c_str());
-            //sprintf(s,"POST /emma/api/controller/register HTTP/1.0\nHost: %s\n\n{\"uid\":\"%s\",\"hmac\":\"%s\"}\r\n\r\n",restSERVER.c_str(),emmaUID.c_str(),hmac.c_str());
+            //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(),restSERVER.c_str());
+            //sprintf(s,"POST /emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: 76\n\n{\"uid\":\"%s\",\"hmac\":\"%s\"}\r\n\r\n",restSERVER.c_str(),emmaUID.c_str(),hmac.c_str());
+            sprintf(s,"POST /emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),connBodyLen,connBody);
             connData = s;
         }
         
@@ -752,18 +759,22 @@
         hmac = calculateMD5(r);
         DBG.printf("hmac:%s\r\n",hmac.c_str());
         
-        //set connData
+        //set connBody and connData
+        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
+        
         if(useProxy) {
             DBG.printf("use proxy\r\n");
             for(int i=0; i<sizeof(s); i++) {
                 s[i]=0; }
-            sprintf(s,"GET http://%s:%s/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),regKey.c_str(),hmac.c_str(),restSERVER.c_str());
+            //sprintf(s,"GET http://%s:%s/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),regKey.c_str(),hmac.c_str(),restSERVER.c_str());
+            sprintf(s,"POST http://%s:%s/emma/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),connBodyLen,connBody);
             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(),restSERVER.c_str());
+            //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(),restSERVER.c_str());
+            sprintf(s,"POST /emma/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),connBodyLen,connBody);
             connData = s;
         }
         
@@ -847,10 +858,10 @@
         if(wifiConnected) {
             //check proxy
             if(useProxy) {
-                sprintf(r,"http://%s:%d/emma/api/controller/register",restSERVER.c_str(),restPORT.c_str());
+                sprintf(r,"http://%s:%s/emma/api/controller/register",restSERVER.c_str(),restPORT.c_str());
                 //sprintf(r,"http://%s:%d/emma/api/controller/register HTTP/1.0\nHost: %s:%d\nProxy-Authorization: Basic %s\nCache-Control: no-cache\nContent-Length: %d\n\n",restSERVER,restPORT,restSERVER,restPORT,proxyAUTH.c_str(),76);
                 //sprintf(r,"http://%s:%d/emma/api/controller/register HTTP/1.0\nAccept: */*\nAccept-Encoding: gzip,deflate\nContent-Length: %d\r\n\r\n",restSERVER,restPORT,76);
-                //sprintf(r,"http://%s:%d/emma/coba",restSERVER,REST_SERVER_POR);
+                //sprintf(r,"http://%s:%d/emma/coba",restSERVER,REST_SERVER_PORT);
             } else {
                 sprintf(r,"/emma/api/controller/register");
             }
@@ -858,7 +869,7 @@
             //register
             while(!emmaGetRegKey) {
                 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
-                DBG.printf("s:%s\r\n",s);
+                //DBG.printf("s:%s\r\n",s);
                 //string iniStr = "{\"uid\":\"005300553533510334313732\",\"hmac\":\"45fc6a4447989a9434d1f21087a78061\"}";
                 //DBG.printf("s:%s\r\n",iniStr.c_str());