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

Committer:
arsenalist
Date:
Fri Sep 04 07:29:52 2015 +0000
Revision:
51:ad6410dcaf52
Parent:
50:50d98fe6cc15
Child:
52:cbe6d1d1efdc
Working checkVoltagePower using GPRS interface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arsenalist 0:f4e449fa34d7 1 #include "emmaCode.h"
arsenalist 0:f4e449fa34d7 2
arsenalist 0:f4e449fa34d7 3 //init debug port
arsenalist 0:f4e449fa34d7 4 Serial DBG(PA_9, PA_10); //tx, rx
arsenalist 0:f4e449fa34d7 5
arsenalist 0:f4e449fa34d7 6 //init wifi port
arsenalist 0:f4e449fa34d7 7 Serial _ESP(PA_2, PA_3); //tx, rx
arsenalist 8:51a0ca9079ca 8 //init espduino - without ch_pd pin
arsenalist 8:51a0ca9079ca 9 ESP esp(&_ESP, &DBG, ESP_BAUD);
arsenalist 0:f4e449fa34d7 10 //init wifi mqtt
arsenalist 27:562a95c120cc 11 //ESPMQTT mqtt(&esp);
arsenalist 0:f4e449fa34d7 12 //init wifi rest
arsenalist 0:f4e449fa34d7 13 REST rest(&esp);
arsenalist 0:f4e449fa34d7 14
arsenalist 0:f4e449fa34d7 15 //init eth port
arsenalist 0:f4e449fa34d7 16 SPI spi(PB_15, PB_14, PB_13); //mosi, miso, sck
arsenalist 44:c1d11c491237 17 EthernetInterface eth(&spi, PB_12, PC_6); //spi, cs, reset
arsenalist 44:c1d11c491237 18 //MQTTEthernet ipstack(&spi, PB_12, PC_6); //spi, cs, reset
arsenalist 37:93f196daf5cf 19 //MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
arsenalist 45:2406c2b6995a 20 TCPSocketConnection sock;
arsenalist 0:f4e449fa34d7 21
arsenalist 48:053e92b6dc1e 22 //init gprs
arsenalist 48:053e92b6dc1e 23 SoftSerial sim900(PC_10, PA_15); //tx, rx
arsenalist 48:053e92b6dc1e 24 DigitalOut pwrKey(PA_8);
arsenalist 48:053e92b6dc1e 25
arsenalist 0:f4e449fa34d7 26 //init sd card
arsenalist 8:51a0ca9079ca 27 SDFileSystem sd(PA_7, PA_6, PA_5, PB_3, "sd"); //mosi, miso, sck, cs
arsenalist 0:f4e449fa34d7 28
arsenalist 8:51a0ca9079ca 29 //init ade7758 - without cs pin
arsenalist 14:8287f0f5d987 30 ADE7758 ADE(PB_6, PB_4, PB_5, PB_7); //mosi, miso, sck, irq
arsenalist 0:f4e449fa34d7 31
arsenalist 12:96f637ed37f9 32 //init tft lcd
arsenalist 12:96f637ed37f9 33 SPI_TFT_ILI9341 TFT(PA_7, PA_6, PA_5, PA_4, PC_5, PC_4,"TFT"); //mosi, miso, sclk, cs, reset, dc
arsenalist 12:96f637ed37f9 34 //init touch screen - without cs pin
arsenalist 12:96f637ed37f9 35 TouchScreenADS7843 TP(PB_10, PB_0, PB_1, PB_2, &TFT); //mosi, miso, sclk, irq, tft
arsenalist 12:96f637ed37f9 36
arsenalist 21:33bd8b82560f 37 //init dht sensor
arsenalist 21:33bd8b82560f 38 DHT11 d(PD_2);
arsenalist 21:33bd8b82560f 39
arsenalist 0:f4e449fa34d7 40 //emma settings
arsenalist 0:f4e449fa34d7 41 string emmaUID;
arsenalist 4:76ab12e2f8a3 42 string hmac;
arsenalist 38:e44f35f8129d 43 string appNAME;
arsenalist 38:e44f35f8129d 44 string mqttDOMAIN;
arsenalist 38:e44f35f8129d 45 string mqttKEY;
arsenalist 38:e44f35f8129d 46 string mqttSECRET;
arsenalist 38:e44f35f8129d 47 string mqttSERVER;
arsenalist 50:50d98fe6cc15 48 int mqttPORT;
arsenalist 38:e44f35f8129d 49 string restSERVER;
arsenalist 50:50d98fe6cc15 50 int restPORT;
arsenalist 0:f4e449fa34d7 51 string wifiSSID;
arsenalist 0:f4e449fa34d7 52 string wifiPASS;
arsenalist 0:f4e449fa34d7 53 string gprsAPN;
arsenalist 0:f4e449fa34d7 54 string proxySERVER;
arsenalist 50:50d98fe6cc15 55 int proxyPORT;
arsenalist 0:f4e449fa34d7 56 string proxyAUTH;
arsenalist 49:d11a1914ee8b 57 int powerPHASE;
arsenalist 0:f4e449fa34d7 58
arsenalist 4:76ab12e2f8a3 59 //nodes settings
arsenalist 4:76ab12e2f8a3 60 class NODES {
arsenalist 4:76ab12e2f8a3 61 public:
arsenalist 4:76ab12e2f8a3 62 REST *restConn;
arsenalist 4:76ab12e2f8a3 63 NODES(REST *r);
arsenalist 40:77bd44f57ad3 64 int type;
arsenalist 4:76ab12e2f8a3 65 string macAddr;
arsenalist 4:76ab12e2f8a3 66 string ipAddr;
arsenalist 4:76ab12e2f8a3 67 };
arsenalist 4:76ab12e2f8a3 68 NODES::NODES(REST *r) {
arsenalist 4:76ab12e2f8a3 69 restConn = r;
arsenalist 4:76ab12e2f8a3 70 }
arsenalist 4:76ab12e2f8a3 71 REST restObj[NODES_MAX] = {REST(&esp),REST(&esp),REST(&esp),REST(&esp),REST(&esp)};
arsenalist 4:76ab12e2f8a3 72 NODES nodes[NODES_MAX] = {NODES(&restObj[0]),NODES(&restObj[1]),NODES(&restObj[2]),NODES(&restObj[3]),NODES(&restObj[4])};
arsenalist 4:76ab12e2f8a3 73
arsenalist 12:96f637ed37f9 74 //mode box class for main menu
arsenalist 12:96f637ed37f9 75 class modeBox {
arsenalist 12:96f637ed37f9 76 public:
arsenalist 12:96f637ed37f9 77 int xTL; //TopLeft
arsenalist 12:96f637ed37f9 78 int yTL;
arsenalist 12:96f637ed37f9 79 int xBR; //BottomRight
arsenalist 12:96f637ed37f9 80 int yBR;
arsenalist 29:4d94a03d38e0 81 string text;
arsenalist 29:4d94a03d38e0 82 string name;
arsenalist 12:96f637ed37f9 83 };
arsenalist 12:96f637ed37f9 84
arsenalist 0:f4e449fa34d7 85 //ade7758 variables
arsenalist 25:36c6a5db50ed 86 uint32_t AWattHrValue, BWattHrValue, CWattHrValue;
arsenalist 25:36c6a5db50ed 87 uint32_t AVAHrValue, BVAHrValue, CVAHrValue;
arsenalist 19:7e3e9332f719 88 uint32_t AWattHrSum = 0;
arsenalist 19:7e3e9332f719 89 uint32_t BWattHrSum = 0;
arsenalist 19:7e3e9332f719 90 uint32_t CWattHrSum = 0;
arsenalist 0:f4e449fa34d7 91 float AWattHr, BWattHr, CWattHr;
arsenalist 4:76ab12e2f8a3 92 float AVrms, BVrms, CVrms;
arsenalist 4:76ab12e2f8a3 93 float AIrms, BIrms, CIrms;
arsenalist 4:76ab12e2f8a3 94 float AWatt, BWatt, CWatt;
arsenalist 4:76ab12e2f8a3 95 float XWattHr,XVrms,XWatt;
arsenalist 0:f4e449fa34d7 96
arsenalist 47:97eadcb478b2 97 //alert variables
arsenalist 47:97eadcb478b2 98 bool allowAlertAVrms = true;
arsenalist 47:97eadcb478b2 99 bool allowAlertAWatt = true;
arsenalist 47:97eadcb478b2 100 bool allowAlertBVrms = true;
arsenalist 47:97eadcb478b2 101 bool allowAlertBWatt = true;
arsenalist 47:97eadcb478b2 102 bool allowAlertCVrms = true;
arsenalist 47:97eadcb478b2 103 bool allowAlertCWatt = true;
arsenalist 47:97eadcb478b2 104
arsenalist 0:f4e449fa34d7 105 //variables
arsenalist 0:f4e449fa34d7 106 bool ethAvailable = false;
arsenalist 0:f4e449fa34d7 107 bool wifiAvailable = false;
arsenalist 0:f4e449fa34d7 108 bool gprsAvailable = false;
arsenalist 0:f4e449fa34d7 109 bool ethConnected = false;
arsenalist 0:f4e449fa34d7 110 bool wifiConnected = false;
arsenalist 9:a58c04da4476 111 bool gprsConnected = false;
arsenalist 0:f4e449fa34d7 112 bool useProxy = false;
arsenalist 0:f4e449fa34d7 113 bool newCommand = false;
arsenalist 37:93f196daf5cf 114 bool espFreeMemory = true; //for after bootup
arsenalist 27:562a95c120cc 115 bool espDHCPClientStart = false;
arsenalist 22:e18d361bf688 116 bool newEnergyData = false;
arsenalist 0:f4e449fa34d7 117 string globalCommand;
arsenalist 4:76ab12e2f8a3 118 string rxBuf;
arsenalist 27:562a95c120cc 119 string rxLog;
arsenalist 27:562a95c120cc 120 string rxLogA;
arsenalist 0:f4e449fa34d7 121
arsenalist 12:96f637ed37f9 122 /*start lcd and touch*/
arsenalist 12:96f637ed37f9 123 int emmaModeSelection(void) {
arsenalist 12:96f637ed37f9 124 bool modeSelected = false;
arsenalist 12:96f637ed37f9 125 int md=0;
arsenalist 12:96f637ed37f9 126 int TPx;
arsenalist 12:96f637ed37f9 127 int TPy;
arsenalist 31:e2abded4d85f 128 Timer t;
arsenalist 12:96f637ed37f9 129
arsenalist 12:96f637ed37f9 130 TFT.background(Black);
arsenalist 12:96f637ed37f9 131 TFT.foreground(White);
arsenalist 12:96f637ed37f9 132
arsenalist 12:96f637ed37f9 133 TFT.set_font((unsigned char*) Arial12x12);
arsenalist 12:96f637ed37f9 134 TFT.set_orientation(1);
arsenalist 12:96f637ed37f9 135 TFT.cls();
arsenalist 12:96f637ed37f9 136 TFT.locate(0,0);
arsenalist 12:96f637ed37f9 137 TFT.printf("Hello, I'm Emma!");
arsenalist 13:e8adfe305dbc 138 wait(2);
arsenalist 12:96f637ed37f9 139 TFT.cls();
arsenalist 12:96f637ed37f9 140
arsenalist 12:96f637ed37f9 141 Matrix matrix;
arsenalist 12:96f637ed37f9 142 Coordinate ScreenSample[3];
arsenalist 12:96f637ed37f9 143
arsenalist 44:c1d11c491237 144 //lcd type 1
arsenalist 44:c1d11c491237 145 //matrix.An = 580;
arsenalist 44:c1d11c491237 146 //matrix.Bn = 75980;
arsenalist 44:c1d11c491237 147 //matrix.Cn = -3410580;
arsenalist 44:c1d11c491237 148 //matrix.Dn = 57855;
arsenalist 44:c1d11c491237 149 //matrix.En = -2465;
arsenalist 44:c1d11c491237 150 //matrix.Fn = -3483515;
arsenalist 44:c1d11c491237 151 //matrix.Divider = 209144;
arsenalist 44:c1d11c491237 152
arsenalist 44:c1d11c491237 153 //ScreenSample[0].x = 230;
arsenalist 44:c1d11c491237 154 //ScreenSample[0].y = 167;
arsenalist 44:c1d11c491237 155 //ScreenSample[1].x = 754;
arsenalist 44:c1d11c491237 156 //ScreenSample[1].y = 163;
arsenalist 44:c1d11c491237 157 //ScreenSample[2].x = 771;
arsenalist 44:c1d11c491237 158 //ScreenSample[2].y = 562;
arsenalist 12:96f637ed37f9 159
arsenalist 44:c1d11c491237 160 //lcd type 2
arsenalist 44:c1d11c491237 161 matrix.An = 1305;
arsenalist 44:c1d11c491237 162 matrix.Bn = -77430;
arsenalist 44:c1d11c491237 163 matrix.Cn = 75296670;
arsenalist 44:c1d11c491237 164 matrix.Dn = -57275;
arsenalist 44:c1d11c491237 165 matrix.En = -1160;
arsenalist 44:c1d11c491237 166 matrix.Fn = 55285220;
arsenalist 44:c1d11c491237 167 matrix.Divider = 211002;
arsenalist 44:c1d11c491237 168
arsenalist 44:c1d11c491237 169 ScreenSample[0].x = 782;
arsenalist 44:c1d11c491237 170 ScreenSample[0].y = 863;
arsenalist 44:c1d11c491237 171 ScreenSample[1].x = 248;
arsenalist 44:c1d11c491237 172 ScreenSample[1].y = 854;
arsenalist 44:c1d11c491237 173 ScreenSample[2].x = 256;
arsenalist 44:c1d11c491237 174 ScreenSample[2].y = 459;
arsenalist 12:96f637ed37f9 175
arsenalist 12:96f637ed37f9 176 TP.SetCalibration(&matrix, &ScreenSample[0]);
arsenalist 12:96f637ed37f9 177
arsenalist 12:96f637ed37f9 178 //draw border
arsenalist 12:96f637ed37f9 179 TFT.line(15,15,310,15,Orange);
arsenalist 12:96f637ed37f9 180 TFT.line(310,15,310,250,Orange);
arsenalist 12:96f637ed37f9 181 TFT.line(310,250,15,250,Orange);
arsenalist 12:96f637ed37f9 182 TFT.line(15,250,15,15,Orange);
arsenalist 12:96f637ed37f9 183
arsenalist 12:96f637ed37f9 184 //init main menu
arsenalist 12:96f637ed37f9 185 modeBox menu[6];
arsenalist 12:96f637ed37f9 186
arsenalist 12:96f637ed37f9 187 //wifi config mode
arsenalist 12:96f637ed37f9 188 menu[MODE_WIFI_CONFIG].xTL = 25;
arsenalist 12:96f637ed37f9 189 menu[MODE_WIFI_CONFIG].yTL = 25;
arsenalist 12:96f637ed37f9 190 menu[MODE_WIFI_CONFIG].xBR = 110;
arsenalist 12:96f637ed37f9 191 menu[MODE_WIFI_CONFIG].yBR = 90;
arsenalist 29:4d94a03d38e0 192 menu[MODE_WIFI_CONFIG].text = "1.wifi config";
arsenalist 29:4d94a03d38e0 193 menu[MODE_WIFI_CONFIG].name = "wifi config";
arsenalist 12:96f637ed37f9 194
arsenalist 12:96f637ed37f9 195 //setting mode
arsenalist 29:4d94a03d38e0 196 menu[MODE_SETTINGS].xTL = 25;
arsenalist 29:4d94a03d38e0 197 menu[MODE_SETTINGS].yTL = 100;
arsenalist 29:4d94a03d38e0 198 menu[MODE_SETTINGS].xBR = 110;
arsenalist 29:4d94a03d38e0 199 menu[MODE_SETTINGS].yBR = 165;
arsenalist 29:4d94a03d38e0 200 menu[MODE_SETTINGS].text = "2.settings ";
arsenalist 29:4d94a03d38e0 201 menu[MODE_SETTINGS].name = "settings";
arsenalist 12:96f637ed37f9 202
arsenalist 12:96f637ed37f9 203 //register mode
arsenalist 29:4d94a03d38e0 204 menu[MODE_REGISTER].xTL = 120;
arsenalist 29:4d94a03d38e0 205 menu[MODE_REGISTER].yTL = 25;
arsenalist 29:4d94a03d38e0 206 menu[MODE_REGISTER].xBR = 205;
arsenalist 29:4d94a03d38e0 207 menu[MODE_REGISTER].yBR = 90;
arsenalist 29:4d94a03d38e0 208 menu[MODE_REGISTER].text = "3.register ";
arsenalist 29:4d94a03d38e0 209 menu[MODE_REGISTER].name = "register";
arsenalist 12:96f637ed37f9 210
arsenalist 12:96f637ed37f9 211 //operational mode
arsenalist 12:96f637ed37f9 212 menu[MODE_OPERATION].xTL = 120;
arsenalist 12:96f637ed37f9 213 menu[MODE_OPERATION].yTL = 100;
arsenalist 12:96f637ed37f9 214 menu[MODE_OPERATION].xBR = 205;
arsenalist 12:96f637ed37f9 215 menu[MODE_OPERATION].yBR = 165;
arsenalist 29:4d94a03d38e0 216 menu[MODE_OPERATION].text = "4.operation ";
arsenalist 29:4d94a03d38e0 217 menu[MODE_OPERATION].name = "operation";
arsenalist 12:96f637ed37f9 218
arsenalist 12:96f637ed37f9 219 //firmware download mode
arsenalist 12:96f637ed37f9 220 menu[MODE_FIRMWARE_DOWNLOAD].xTL = 215;
arsenalist 12:96f637ed37f9 221 menu[MODE_FIRMWARE_DOWNLOAD].yTL = 25;
arsenalist 12:96f637ed37f9 222 menu[MODE_FIRMWARE_DOWNLOAD].xBR = 300;
arsenalist 12:96f637ed37f9 223 menu[MODE_FIRMWARE_DOWNLOAD].yBR = 90;
arsenalist 29:4d94a03d38e0 224 menu[MODE_FIRMWARE_DOWNLOAD].text = "5.fw dwld ";
arsenalist 29:4d94a03d38e0 225 menu[MODE_FIRMWARE_DOWNLOAD].name = "fw dwld";
arsenalist 12:96f637ed37f9 226
arsenalist 12:96f637ed37f9 227 //reserved mode
arsenalist 44:c1d11c491237 228 menu[MODE_RESERVED].xTL = 215;
arsenalist 44:c1d11c491237 229 menu[MODE_RESERVED].yTL = 100;
arsenalist 44:c1d11c491237 230 menu[MODE_RESERVED].xBR = 300;
arsenalist 44:c1d11c491237 231 menu[MODE_RESERVED].yBR = 165;
arsenalist 44:c1d11c491237 232 menu[MODE_RESERVED].text = "6.reserved ";
arsenalist 44:c1d11c491237 233 menu[MODE_RESERVED].name = "reserved";
arsenalist 12:96f637ed37f9 234
arsenalist 17:b3ced0e67916 235 //draw main menu
arsenalist 12:96f637ed37f9 236 for(int i=0; i<6; i++) {
arsenalist 12:96f637ed37f9 237 TFT.fillrect(menu[i].xTL,menu[i].yTL,menu[i].xBR,menu[i].yBR,Orange);
arsenalist 12:96f637ed37f9 238 }
arsenalist 12:96f637ed37f9 239
arsenalist 17:b3ced0e67916 240 //add text to main menu
arsenalist 17:b3ced0e67916 241 for(int i=0; i<6; i++) {
arsenalist 17:b3ced0e67916 242 TFT.locate(menu[i].xTL,menu[i].yTL);
arsenalist 31:e2abded4d85f 243 TFT.printf("%s",menu[i].text.c_str());
arsenalist 31:e2abded4d85f 244 }
arsenalist 31:e2abded4d85f 245
arsenalist 31:e2abded4d85f 246 //read emma settings
arsenalist 31:e2abded4d85f 247 emmaReadSettings();
arsenalist 31:e2abded4d85f 248
arsenalist 38:e44f35f8129d 249 //mqttDOMAIN is not empty -> has been registered
arsenalist 38:e44f35f8129d 250 if(!mqttDOMAIN.empty()) {
arsenalist 31:e2abded4d85f 251 TFT.locate(25,170);
arsenalist 31:e2abded4d85f 252 TFT.printf(" auto select enabled");
arsenalist 31:e2abded4d85f 253 t.start();
arsenalist 17:b3ced0e67916 254 }
arsenalist 17:b3ced0e67916 255
arsenalist 12:96f637ed37f9 256 while(!modeSelected) {
arsenalist 12:96f637ed37f9 257 if(!TP._tp_irq) {
arsenalist 12:96f637ed37f9 258 if(TP.Read_Ads7843()) {
arsenalist 12:96f637ed37f9 259 TP.getDisplayPoint();
arsenalist 12:96f637ed37f9 260 TPx = TP.display.x;
arsenalist 12:96f637ed37f9 261 TPy = TP.display.y;
arsenalist 12:96f637ed37f9 262 TP.TP_DrawPoint(TPx,TPy, Blue);
arsenalist 12:96f637ed37f9 263
arsenalist 12:96f637ed37f9 264 for(int i=0; i<6; i++) {
arsenalist 12:96f637ed37f9 265 if((menu[i].xTL < TPx && TPx < menu[i].xBR) && (menu[i].yTL < TPy && TPy < menu[i].yBR)) {
arsenalist 12:96f637ed37f9 266 md = i;
arsenalist 12:96f637ed37f9 267 modeSelected = true;
arsenalist 12:96f637ed37f9 268 }
arsenalist 12:96f637ed37f9 269 }
arsenalist 12:96f637ed37f9 270 }
arsenalist 31:e2abded4d85f 271 }
arsenalist 31:e2abded4d85f 272
arsenalist 31:e2abded4d85f 273 //auto select (to emmaModeOperation) after some times
arsenalist 38:e44f35f8129d 274 if(!mqttDOMAIN.empty()) {
arsenalist 31:e2abded4d85f 275 if(t.read()>20) {
arsenalist 31:e2abded4d85f 276 md = MODE_OPERATION;
arsenalist 31:e2abded4d85f 277 modeSelected = true;
arsenalist 31:e2abded4d85f 278 t.stop();
arsenalist 31:e2abded4d85f 279 t.reset();
arsenalist 31:e2abded4d85f 280 }
arsenalist 31:e2abded4d85f 281 }
arsenalist 31:e2abded4d85f 282
arsenalist 12:96f637ed37f9 283 }
arsenalist 12:96f637ed37f9 284
arsenalist 12:96f637ed37f9 285 TFT.locate(25,170);
arsenalist 12:96f637ed37f9 286 TFT.printf(" ");
arsenalist 12:96f637ed37f9 287 TFT.locate(25,170);
arsenalist 29:4d94a03d38e0 288 TFT.printf("mode: %s is selected",menu[md].name.c_str());
arsenalist 13:e8adfe305dbc 289 wait(2);
arsenalist 12:96f637ed37f9 290 TFT.cls();
arsenalist 12:96f637ed37f9 291
arsenalist 12:96f637ed37f9 292 return md;
arsenalist 12:96f637ed37f9 293 }
arsenalist 12:96f637ed37f9 294 /*end lcd and touch*/
arsenalist 12:96f637ed37f9 295
arsenalist 31:e2abded4d85f 296 /*start emma read settings*/
arsenalist 31:e2abded4d85f 297 void emmaReadSettings(void) {
arsenalist 4:76ab12e2f8a3 298 char s[64];
arsenalist 49:d11a1914ee8b 299 string str;
arsenalist 0:f4e449fa34d7 300 DBG.baud(19200);
arsenalist 31:e2abded4d85f 301 DBG.printf("\r\nemmaReadSettings\r\n");
arsenalist 0:f4e449fa34d7 302
arsenalist 0:f4e449fa34d7 303 //read settings
arsenalist 9:a58c04da4476 304 emmaUID = readSetting("emmaUID");
arsenalist 0:f4e449fa34d7 305 DBG.printf("emmaUID:%s\r\n",emmaUID.c_str());
arsenalist 37:93f196daf5cf 306
arsenalist 4:76ab12e2f8a3 307 //calculate hmac
arsenalist 4:76ab12e2f8a3 308 for(int i=0; i<sizeof(s); i++) {
arsenalist 4:76ab12e2f8a3 309 s[i]=0; }
arsenalist 4:76ab12e2f8a3 310 sprintf(s,"emma-%s",emmaUID.c_str());
arsenalist 4:76ab12e2f8a3 311 hmac = calculateMD5(s);
arsenalist 4:76ab12e2f8a3 312 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 37:93f196daf5cf 313
arsenalist 38:e44f35f8129d 314 appNAME = readSetting("appNAME");
arsenalist 38:e44f35f8129d 315 DBG.printf("appNAME:%s\r\n",appNAME.c_str());
arsenalist 38:e44f35f8129d 316
arsenalist 38:e44f35f8129d 317 mqttDOMAIN = readSetting("mqttDOMAIN");
arsenalist 38:e44f35f8129d 318 DBG.printf("mqttDOMAIN:%s\r\n",mqttDOMAIN.c_str());
arsenalist 38:e44f35f8129d 319 mqttKEY = readSetting("mqttKEY");
arsenalist 38:e44f35f8129d 320 DBG.printf("mqttKEY:%s\r\n",mqttKEY.c_str());
arsenalist 38:e44f35f8129d 321 mqttSECRET = readSetting("mqttSECRET");
arsenalist 38:e44f35f8129d 322 DBG.printf("mqttSECRET:%s\r\n",mqttSECRET.c_str());
arsenalist 38:e44f35f8129d 323 mqttSERVER = readSetting("mqttSERVER");
arsenalist 38:e44f35f8129d 324 DBG.printf("mqttSERVER:%s\r\n",mqttSERVER.c_str());
arsenalist 50:50d98fe6cc15 325 str = readSetting("mqttPORT");
arsenalist 50:50d98fe6cc15 326 sscanf(str.c_str(),"%d",&mqttPORT);
arsenalist 50:50d98fe6cc15 327 DBG.printf("mqttPORT:%d\r\n",mqttPORT);
arsenalist 38:e44f35f8129d 328
arsenalist 38:e44f35f8129d 329 restSERVER = readSetting("restSERVER");
arsenalist 38:e44f35f8129d 330 DBG.printf("restSERVER:%s\r\n",restSERVER.c_str());
arsenalist 50:50d98fe6cc15 331 str = readSetting("restPORT");
arsenalist 50:50d98fe6cc15 332 sscanf(str.c_str(),"%d",&restPORT);
arsenalist 50:50d98fe6cc15 333 DBG.printf("restPORT:%d\r\n",restPORT);
arsenalist 38:e44f35f8129d 334
arsenalist 48:053e92b6dc1e 335 gprsAPN = readSetting("gprsAPN");
arsenalist 48:053e92b6dc1e 336 DBG.printf("gprsAPN:%s\r\n",gprsAPN.c_str());
arsenalist 48:053e92b6dc1e 337
arsenalist 9:a58c04da4476 338 proxySERVER = readSetting("proxySERVER");
arsenalist 0:f4e449fa34d7 339 DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str());
arsenalist 50:50d98fe6cc15 340 str = readSetting("proxyPORT");
arsenalist 50:50d98fe6cc15 341 sscanf(str.c_str(),"%d",&proxyPORT);
arsenalist 50:50d98fe6cc15 342 DBG.printf("proxyPORT:%s\r\n",proxyPORT);
arsenalist 9:a58c04da4476 343 proxyAUTH = readSetting("proxyAUTH");
arsenalist 0:f4e449fa34d7 344 DBG.printf("proxyAUTH:%s\r\n",proxyAUTH.c_str());
arsenalist 28:7561035e3df5 345 wifiSSID = readSetting("wifiSSID");
arsenalist 28:7561035e3df5 346 DBG.printf("wifiSSID:%s\r\n",wifiSSID.c_str());
arsenalist 28:7561035e3df5 347 wifiPASS = readSetting("wifiPASS");
arsenalist 28:7561035e3df5 348 DBG.printf("wifiPASS:%s\r\n",wifiPASS.c_str());
arsenalist 49:d11a1914ee8b 349 str = readSetting("powerPHASE");
arsenalist 49:d11a1914ee8b 350 sscanf(str.c_str(),"%d",&powerPHASE);
arsenalist 49:d11a1914ee8b 351 DBG.printf("powerPHASE:%d\r\n",powerPHASE);
arsenalist 31:e2abded4d85f 352 }
arsenalist 31:e2abded4d85f 353 /*end emma read settings*/
arsenalist 31:e2abded4d85f 354
arsenalist 31:e2abded4d85f 355 /*start emma mode*/
arsenalist 31:e2abded4d85f 356 void emmaInit(int mode) {
arsenalist 31:e2abded4d85f 357 DBG.printf("\r\nemmaInit\r\n");
arsenalist 31:e2abded4d85f 358 DBG.printf("mode:%d\r\n",mode);
arsenalist 0:f4e449fa34d7 359
arsenalist 0:f4e449fa34d7 360 //check proxy
arsenalist 50:50d98fe6cc15 361 if(!proxySERVER.empty() && !proxyAUTH.empty()) {
arsenalist 0:f4e449fa34d7 362 useProxy = true;
arsenalist 0:f4e449fa34d7 363 } else {
arsenalist 0:f4e449fa34d7 364 useProxy = false;
arsenalist 0:f4e449fa34d7 365 }
arsenalist 0:f4e449fa34d7 366 //testing purpose
arsenalist 32:78633fb9b2a1 367 //useProxy = false;
arsenalist 0:f4e449fa34d7 368 DBG.printf("proxy:%d\r\n",useProxy);
arsenalist 0:f4e449fa34d7 369
arsenalist 8:51a0ca9079ca 370 //check available interface
arsenalist 8:51a0ca9079ca 371 isEthAvailable(); //check whether cable is connected
arsenalist 48:053e92b6dc1e 372
arsenalist 49:d11a1914ee8b 373 wifiAvailable = true; //wifi module will always on the board
arsenalist 49:d11a1914ee8b 374
arsenalist 48:053e92b6dc1e 375
arsenalist 31:e2abded4d85f 376 gprsAvailable = true; //gprs module will always on the board
arsenalist 8:51a0ca9079ca 377 DBG.printf("eth:%d\r\n",ethAvailable);
arsenalist 8:51a0ca9079ca 378 DBG.printf("wifi:%d\r\n",wifiAvailable);
arsenalist 8:51a0ca9079ca 379 DBG.printf("gprs:%d\r\n",gprsAvailable);
arsenalist 0:f4e449fa34d7 380 }
arsenalist 0:f4e449fa34d7 381 void emmaModeWiFiConfig(void) {
arsenalist 0:f4e449fa34d7 382 string str;
arsenalist 18:87f30ba9ddc5 383 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 384 TFT.printf(" please wait");
arsenalist 0:f4e449fa34d7 385
arsenalist 0:f4e449fa34d7 386 if(wifiAvailable) {
arsenalist 0:f4e449fa34d7 387 DBG.printf("emmaModeWiFiConfig\r\n");
arsenalist 0:f4e449fa34d7 388
arsenalist 0:f4e449fa34d7 389 //set wifi module to configuration
arsenalist 0:f4e449fa34d7 390 _ESP.printf("MODE=C");
arsenalist 0:f4e449fa34d7 391 while(1) {
arsenalist 0:f4e449fa34d7 392 char rcv[128] = {};
arsenalist 48:053e92b6dc1e 393 wifiRcvReply(rcv,3000);
arsenalist 0:f4e449fa34d7 394 str = rcv;
arsenalist 0:f4e449fa34d7 395 if(str.find("SC_STATUS_FIND_CHANNEL") != std::string::npos)
arsenalist 0:f4e449fa34d7 396 break;
arsenalist 0:f4e449fa34d7 397 }
arsenalist 18:87f30ba9ddc5 398
arsenalist 18:87f30ba9ddc5 399 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 400 TFT.printf(" emmaModeWiFiConfig");
arsenalist 18:87f30ba9ddc5 401 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 402 TFT.printf(" connect with emma app now");
arsenalist 18:87f30ba9ddc5 403
arsenalist 0:f4e449fa34d7 404 DBG.printf("entering wifi configuration mode\r\n");
arsenalist 0:f4e449fa34d7 405 while(1) {
arsenalist 0:f4e449fa34d7 406 char rcv[128] = {};
arsenalist 48:053e92b6dc1e 407 wifiRcvReply(rcv,3000);
arsenalist 0:f4e449fa34d7 408 str = rcv;
arsenalist 0:f4e449fa34d7 409 if(str.find("MODE=C OK") != std::string::npos) {
arsenalist 0:f4e449fa34d7 410 //save wifiSSID and wifiPASS
arsenalist 0:f4e449fa34d7 411 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 412 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 413 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 414
arsenalist 0:f4e449fa34d7 415 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 416 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 417
arsenalist 0:f4e449fa34d7 418 char *parameter[2] = {"wifiSSID","wifiPASS"};
arsenalist 0:f4e449fa34d7 419
arsenalist 0:f4e449fa34d7 420 for(int i=0; i<2; i++) {
arsenalist 0:f4e449fa34d7 421 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 422 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 11:2311b2d5157d 423 int st = writeSetting(parameter[i],val.c_str());
arsenalist 11:2311b2d5157d 424 if(st) {
arsenalist 11:2311b2d5157d 425 DBG.printf("%s is saved\r\n",parameter[i]);
arsenalist 18:87f30ba9ddc5 426 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 427 TFT.printf(" %s is saved",parameter[i]);
arsenalist 18:87f30ba9ddc5 428 wait(3);
arsenalist 18:87f30ba9ddc5 429 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 430 TFT.printf(" ");
arsenalist 11:2311b2d5157d 431 } else {
arsenalist 11:2311b2d5157d 432 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 18:87f30ba9ddc5 433 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 434 TFT.printf(" %s is not saved",parameter[i]);
arsenalist 18:87f30ba9ddc5 435 wait(3);
arsenalist 18:87f30ba9ddc5 436 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 437 TFT.printf(" ");
arsenalist 11:2311b2d5157d 438 }
arsenalist 0:f4e449fa34d7 439 }
arsenalist 0:f4e449fa34d7 440 }
arsenalist 18:87f30ba9ddc5 441
arsenalist 18:87f30ba9ddc5 442 //wificonfig finish
arsenalist 18:87f30ba9ddc5 443 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 444 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 445 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 446 TFT.printf(" wificonfig finish. please restart.");
arsenalist 18:87f30ba9ddc5 447
arsenalist 0:f4e449fa34d7 448 }
arsenalist 18:87f30ba9ddc5 449 } else if(str.find("SC_STATUS_GETTING_SSID_PSWD") != std::string::npos){
arsenalist 18:87f30ba9ddc5 450 DBG.printf("app connected\r\n");
arsenalist 18:87f30ba9ddc5 451 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 452 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 453 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 454 TFT.printf(" app connected");
arsenalist 0:f4e449fa34d7 455 }
arsenalist 0:f4e449fa34d7 456 }
arsenalist 0:f4e449fa34d7 457 } else {
arsenalist 0:f4e449fa34d7 458 DBG.printf("no wifi found\r\n");
arsenalist 18:87f30ba9ddc5 459 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 460 TFT.printf(" no wifi found");
arsenalist 0:f4e449fa34d7 461 }
arsenalist 0:f4e449fa34d7 462 }
arsenalist 0:f4e449fa34d7 463 void emmaModeSettings(void) {
arsenalist 0:f4e449fa34d7 464 bool clientIsConnected = false;
arsenalist 0:f4e449fa34d7 465 bool serverIsListened = false;
arsenalist 0:f4e449fa34d7 466 char s[32];
arsenalist 0:f4e449fa34d7 467 string str;
arsenalist 0:f4e449fa34d7 468
arsenalist 18:87f30ba9ddc5 469 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 470 TFT.printf(" please wait");
arsenalist 18:87f30ba9ddc5 471
arsenalist 8:51a0ca9079ca 472 //create settings dir
arsenalist 0:f4e449fa34d7 473 mkdir("/sd/settings",0777);
arsenalist 8:51a0ca9079ca 474
arsenalist 0:f4e449fa34d7 475 //get and write emmaUID
arsenalist 0:f4e449fa34d7 476 string uid = getUID();
arsenalist 0:f4e449fa34d7 477 sprintf(s,"(%s)",uid.c_str());
arsenalist 0:f4e449fa34d7 478 uid = s;
arsenalist 0:f4e449fa34d7 479 writeSetting("emmaUID",uid);
arsenalist 0:f4e449fa34d7 480
arsenalist 8:51a0ca9079ca 481 if(ethAvailable) {
arsenalist 0:f4e449fa34d7 482 DBG.printf("emmaModeSettings - eth\r\n");
arsenalist 47:97eadcb478b2 483 eth.init();
arsenalist 47:97eadcb478b2 484 eth.connect();
arsenalist 37:93f196daf5cf 485 wait(2);
arsenalist 37:93f196daf5cf 486 TFT.locate(0,0);
arsenalist 37:93f196daf5cf 487 TFT.printf(" ");
arsenalist 37:93f196daf5cf 488 TFT.locate(0,0);
arsenalist 37:93f196daf5cf 489 TFT.printf(" emmaModeSettings");
arsenalist 0:f4e449fa34d7 490
arsenalist 0:f4e449fa34d7 491 TCPSocketServer svr;
arsenalist 0:f4e449fa34d7 492 TCPSocketConnection clientSock;
arsenalist 0:f4e449fa34d7 493
arsenalist 0:f4e449fa34d7 494 if(svr.bind(SERVER_PORT) < 0) {
arsenalist 0:f4e449fa34d7 495 DBG.printf("tcp server bind failed\r\n");
arsenalist 0:f4e449fa34d7 496 } else {
arsenalist 0:f4e449fa34d7 497 DBG.printf("tcp server bind success\r\n");
arsenalist 0:f4e449fa34d7 498 serverIsListened = true;
arsenalist 0:f4e449fa34d7 499 }
arsenalist 0:f4e449fa34d7 500
arsenalist 44:c1d11c491237 501 //DBG.printf("please connect to %s\r\n",ipstack.getEth().getIPAddress());
arsenalist 44:c1d11c491237 502 DBG.printf("please connect to %s\r\n",eth.getIPAddress());
arsenalist 0:f4e449fa34d7 503
arsenalist 0:f4e449fa34d7 504 if(svr.listen(1) < 0) {
arsenalist 37:93f196daf5cf 505 DBG.printf("tcp server listen failed\r\n");
arsenalist 37:93f196daf5cf 506 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 507 TFT.printf(" settings error. please restart.");
arsenalist 37:93f196daf5cf 508 while(1);
arsenalist 0:f4e449fa34d7 509 } else {
arsenalist 37:93f196daf5cf 510 DBG.printf("tcp server is listening...\r\n");
arsenalist 37:93f196daf5cf 511 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 512 TFT.printf(" connect with emma app now!");
arsenalist 0:f4e449fa34d7 513 }
arsenalist 0:f4e449fa34d7 514
arsenalist 0:f4e449fa34d7 515 clientSock.set_blocking(false,30000); //timeout after 30sec
arsenalist 0:f4e449fa34d7 516
arsenalist 0:f4e449fa34d7 517 //listening
arsenalist 0:f4e449fa34d7 518 while (serverIsListened) {
arsenalist 0:f4e449fa34d7 519 if(svr.accept(clientSock) < 0) {
arsenalist 37:93f196daf5cf 520 DBG.printf("failed to accept connection\r\n");
arsenalist 37:93f196daf5cf 521 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 522 TFT.printf(" ");
arsenalist 37:93f196daf5cf 523 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 524 TFT.printf("failed to accept connection");
arsenalist 0:f4e449fa34d7 525 } else {
arsenalist 0:f4e449fa34d7 526 DBG.printf("connection success!\r\nIP: %s\r\n",clientSock.get_address());
arsenalist 37:93f196daf5cf 527 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 528 TFT.printf(" ");
arsenalist 37:93f196daf5cf 529 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 530 //TFT.printf(" connection success! IP: %s",clientSock.get_address());
arsenalist 37:93f196daf5cf 531 TFT.printf(" connection success!");
arsenalist 0:f4e449fa34d7 532 clientIsConnected = true;
arsenalist 0:f4e449fa34d7 533
arsenalist 0:f4e449fa34d7 534 while(clientIsConnected) {
arsenalist 0:f4e449fa34d7 535 char buffer[1024] = {};
arsenalist 0:f4e449fa34d7 536 switch(clientSock.receive(buffer,1023)) {
arsenalist 0:f4e449fa34d7 537 case 0:
arsenalist 0:f4e449fa34d7 538 DBG.printf("received buffer is empty\r\n");
arsenalist 0:f4e449fa34d7 539 clientIsConnected = false;
arsenalist 0:f4e449fa34d7 540 break;
arsenalist 0:f4e449fa34d7 541 case -1:
arsenalist 0:f4e449fa34d7 542 DBG.printf("failed to read data from client\r\n");
arsenalist 0:f4e449fa34d7 543 clientIsConnected = false;
arsenalist 0:f4e449fa34d7 544 break;
arsenalist 0:f4e449fa34d7 545 default:
arsenalist 0:f4e449fa34d7 546 //DBG.printf("received data: %d\r\n%s\r\n",strlen(buffer),buffer);
arsenalist 0:f4e449fa34d7 547 DBG.printf("\r\n");
arsenalist 0:f4e449fa34d7 548
arsenalist 0:f4e449fa34d7 549 str = buffer;
arsenalist 0:f4e449fa34d7 550 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 551 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 552 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 553
arsenalist 0:f4e449fa34d7 554 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 555 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 556
arsenalist 14:8287f0f5d987 557 char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
arsenalist 0:f4e449fa34d7 558
arsenalist 0:f4e449fa34d7 559 for(int i=0; i<4; i++) {
arsenalist 0:f4e449fa34d7 560 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 561 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 0:f4e449fa34d7 562 int st = writeSetting(parameter[i],val.c_str());
arsenalist 0:f4e449fa34d7 563 if(st) {
arsenalist 0:f4e449fa34d7 564 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 37:93f196daf5cf 565 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 566 TFT.printf(" %s: %s is saved",parameter[i],val.c_str());
arsenalist 37:93f196daf5cf 567 wait(1);
arsenalist 37:93f196daf5cf 568 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 569 TFT.printf(" ");
arsenalist 0:f4e449fa34d7 570 } else {
arsenalist 0:f4e449fa34d7 571 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 37:93f196daf5cf 572 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 573 TFT.printf(" %s is not saved",parameter[i]);
arsenalist 37:93f196daf5cf 574 wait(1);
arsenalist 37:93f196daf5cf 575 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 576 TFT.printf(" ");
arsenalist 0:f4e449fa34d7 577 }
arsenalist 0:f4e449fa34d7 578 }
arsenalist 0:f4e449fa34d7 579 }
arsenalist 14:8287f0f5d987 580
arsenalist 14:8287f0f5d987 581 //set time
arsenalist 14:8287f0f5d987 582 if(jsonValue.hasMember(parameter[4])) {
arsenalist 14:8287f0f5d987 583 string epTime = jsonValue[parameter[4]].get<std::string>();
arsenalist 20:ea14f175bbb4 584 time_t seconds;
arsenalist 14:8287f0f5d987 585 sscanf(epTime.c_str(),"%d",&seconds);
arsenalist 14:8287f0f5d987 586 set_time(seconds);
arsenalist 14:8287f0f5d987 587 DBG.printf("time is set\r\n");
arsenalist 37:93f196daf5cf 588 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 589 TFT.printf(" time is set");
arsenalist 37:93f196daf5cf 590 wait(1);
arsenalist 37:93f196daf5cf 591 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 592 TFT.printf(" ");
arsenalist 14:8287f0f5d987 593 }
arsenalist 0:f4e449fa34d7 594 }
arsenalist 0:f4e449fa34d7 595 break;
arsenalist 0:f4e449fa34d7 596 }
arsenalist 0:f4e449fa34d7 597 }
arsenalist 0:f4e449fa34d7 598 DBG.printf("close connection\r\n");
arsenalist 37:93f196daf5cf 599 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 600 TFT.printf(" ");
arsenalist 37:93f196daf5cf 601 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 602 TFT.printf(" settings finish. please restart.");
arsenalist 0:f4e449fa34d7 603 clientSock.close();
arsenalist 0:f4e449fa34d7 604 }
arsenalist 0:f4e449fa34d7 605 }
arsenalist 8:51a0ca9079ca 606 } else if(wifiAvailable) {
arsenalist 8:51a0ca9079ca 607 DBG.printf("emmaModeSettings - wifi\r\n");
arsenalist 8:51a0ca9079ca 608
arsenalist 8:51a0ca9079ca 609 _ESP.printf("MODE=S");
arsenalist 8:51a0ca9079ca 610 while(1) {
arsenalist 8:51a0ca9079ca 611 char rcv[128] = {};
arsenalist 48:053e92b6dc1e 612 wifiRcvReply(rcv,3000);
arsenalist 8:51a0ca9079ca 613 str = rcv;
arsenalist 8:51a0ca9079ca 614 if(str.find("MODE=S_OK") != std::string::npos)
arsenalist 8:51a0ca9079ca 615 break;
arsenalist 8:51a0ca9079ca 616 }
arsenalist 8:51a0ca9079ca 617 DBG.printf("entering settings mode\r\n");
arsenalist 17:b3ced0e67916 618 TFT.locate(0,0);
arsenalist 17:b3ced0e67916 619 TFT.printf(" emmaModeSettings");
arsenalist 17:b3ced0e67916 620 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 621 TFT.printf(" connect with emma app now!");
arsenalist 17:b3ced0e67916 622
arsenalist 8:51a0ca9079ca 623 while(1) {
arsenalist 8:51a0ca9079ca 624 char rcv[512] = {};
arsenalist 48:053e92b6dc1e 625 wifiRcvReply(rcv,3000);
arsenalist 8:51a0ca9079ca 626 //DBG.printf("rcv:%s\r\n",rcv);
arsenalist 8:51a0ca9079ca 627 str = rcv;
arsenalist 8:51a0ca9079ca 628 if(str.find("MODE=S_Config") != std::string::npos) {
arsenalist 8:51a0ca9079ca 629 //save gprs and proxy setting
arsenalist 8:51a0ca9079ca 630 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 8:51a0ca9079ca 631 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 8:51a0ca9079ca 632 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 8:51a0ca9079ca 633
arsenalist 8:51a0ca9079ca 634 MbedJSONValue jsonValue;
arsenalist 8:51a0ca9079ca 635 parse(jsonValue,str.c_str());
arsenalist 8:51a0ca9079ca 636
arsenalist 14:8287f0f5d987 637 char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
arsenalist 8:51a0ca9079ca 638
arsenalist 8:51a0ca9079ca 639 for(int i=0; i<4; i++) {
arsenalist 8:51a0ca9079ca 640 if(jsonValue.hasMember(parameter[i])) {
arsenalist 8:51a0ca9079ca 641 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 8:51a0ca9079ca 642 int st = writeSetting(parameter[i],val.c_str());
arsenalist 8:51a0ca9079ca 643 if(st) {
arsenalist 8:51a0ca9079ca 644 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 17:b3ced0e67916 645 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 646 TFT.printf(" %s: %s is saved",parameter[i],val.c_str());
arsenalist 37:93f196daf5cf 647 wait(1);
arsenalist 17:b3ced0e67916 648 TFT.locate(0,40);
arsenalist 17:b3ced0e67916 649 TFT.printf(" ");
arsenalist 8:51a0ca9079ca 650 } else {
arsenalist 8:51a0ca9079ca 651 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 17:b3ced0e67916 652 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 653 TFT.printf(" %s is not saved",parameter[i]);
arsenalist 37:93f196daf5cf 654 wait(1);
arsenalist 17:b3ced0e67916 655 TFT.locate(0,40);
arsenalist 17:b3ced0e67916 656 TFT.printf(" ");
arsenalist 8:51a0ca9079ca 657 }
arsenalist 8:51a0ca9079ca 658 }
arsenalist 8:51a0ca9079ca 659 }
arsenalist 14:8287f0f5d987 660
arsenalist 14:8287f0f5d987 661 //set time
arsenalist 14:8287f0f5d987 662 if(jsonValue.hasMember(parameter[4])) {
arsenalist 14:8287f0f5d987 663 string epTime = jsonValue[parameter[4]].get<std::string>();
arsenalist 14:8287f0f5d987 664 time_t seconds;
arsenalist 14:8287f0f5d987 665 sscanf(epTime.c_str(),"%d",&seconds);
arsenalist 14:8287f0f5d987 666 set_time(seconds);
arsenalist 14:8287f0f5d987 667 DBG.printf("time is set\r\n");
arsenalist 17:b3ced0e67916 668 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 669 TFT.printf(" time is set");
arsenalist 37:93f196daf5cf 670 wait(1);
arsenalist 17:b3ced0e67916 671 TFT.locate(0,40);
arsenalist 17:b3ced0e67916 672 TFT.printf(" ");
arsenalist 14:8287f0f5d987 673 }
arsenalist 18:87f30ba9ddc5 674
arsenalist 18:87f30ba9ddc5 675 //setting finish
arsenalist 18:87f30ba9ddc5 676 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 677 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 678 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 679 TFT.printf(" settings finish. please restart.");
arsenalist 8:51a0ca9079ca 680 }
arsenalist 8:51a0ca9079ca 681 } else if(str.find("connect") != std::string::npos) {
arsenalist 8:51a0ca9079ca 682 DBG.printf("connection success!\r\n");
arsenalist 17:b3ced0e67916 683 TFT.locate(0,20);
arsenalist 17:b3ced0e67916 684 TFT.printf(" ");
arsenalist 17:b3ced0e67916 685 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 686 TFT.printf(" connection success!");
arsenalist 8:51a0ca9079ca 687 }
arsenalist 8:51a0ca9079ca 688 }
arsenalist 0:f4e449fa34d7 689 } else {
arsenalist 9:a58c04da4476 690 DBG.printf("no eth or wifi is available\r\n");
arsenalist 17:b3ced0e67916 691 TFT.locate(0,0);
arsenalist 48:053e92b6dc1e 692 TFT.printf(" no eth or wifi, please restart!");
arsenalist 0:f4e449fa34d7 693 }
arsenalist 0:f4e449fa34d7 694 }
arsenalist 0:f4e449fa34d7 695 void emmaModeRegister(void) {
arsenalist 0:f4e449fa34d7 696 bool emmaGetRegKey = false;
arsenalist 0:f4e449fa34d7 697 bool emmaRegistered = false;
arsenalist 39:f51aa948acee 698 char connBody[256];
arsenalist 0:f4e449fa34d7 699 char s[512];
arsenalist 0:f4e449fa34d7 700 char r[256];
arsenalist 39:f51aa948acee 701 int connBodyLen;
arsenalist 0:f4e449fa34d7 702 int connPort;
arsenalist 0:f4e449fa34d7 703 int loop = 0;
arsenalist 0:f4e449fa34d7 704 string connData;
arsenalist 0:f4e449fa34d7 705 string connHost;
arsenalist 0:f4e449fa34d7 706 string str;
arsenalist 0:f4e449fa34d7 707 string regKey;
arsenalist 0:f4e449fa34d7 708 Timer t;
arsenalist 0:f4e449fa34d7 709
arsenalist 18:87f30ba9ddc5 710 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 711 TFT.printf(" please wait");
arsenalist 18:87f30ba9ddc5 712
arsenalist 0:f4e449fa34d7 713 //check connected interface
arsenalist 8:51a0ca9079ca 714 isEthConnected();
arsenalist 8:51a0ca9079ca 715 isWiFiConnected();
arsenalist 8:51a0ca9079ca 716 isGprsConnected();
arsenalist 0:f4e449fa34d7 717 DBG.printf("ethConnected:%d\r\n",ethConnected);
arsenalist 0:f4e449fa34d7 718 DBG.printf("wifiConnected:%d\r\n",wifiConnected);
arsenalist 48:053e92b6dc1e 719 DBG.printf("gprsConnected:%d\r\n",gprsConnected);
arsenalist 0:f4e449fa34d7 720
arsenalist 9:a58c04da4476 721 if(ethConnected) {
arsenalist 9:a58c04da4476 722 DBG.printf("emmaModeRegister - eth\r\n");
arsenalist 38:e44f35f8129d 723 wait(2);
arsenalist 38:e44f35f8129d 724 TFT.locate(0,0);
arsenalist 38:e44f35f8129d 725 TFT.printf(" ");
arsenalist 38:e44f35f8129d 726 TFT.locate(0,0);
arsenalist 38:e44f35f8129d 727 TFT.printf(" emmaModeRegister");
arsenalist 9:a58c04da4476 728
arsenalist 39:f51aa948acee 729 //set connBody, connHost, connPort, connData
arsenalist 39:f51aa948acee 730 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 731
arsenalist 9:a58c04da4476 732 if(useProxy) {
arsenalist 9:a58c04da4476 733 DBG.printf("use proxy\r\n");
arsenalist 9:a58c04da4476 734 connHost = proxySERVER;
arsenalist 50:50d98fe6cc15 735 connPort = proxyPORT;
arsenalist 9:a58c04da4476 736 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 737 s[i]=0; }
arsenalist 50:50d98fe6cc15 738 sprintf(s,"POST http://%s:%d/%s/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 739 connData = s;
arsenalist 9:a58c04da4476 740 } else {
arsenalist 9:a58c04da4476 741 DBG.printf("no proxy\r\n");
arsenalist 38:e44f35f8129d 742 connHost = restSERVER;
arsenalist 50:50d98fe6cc15 743 connPort = restPORT;
arsenalist 9:a58c04da4476 744 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 745 s[i]=0; }
arsenalist 39:f51aa948acee 746 //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());
arsenalist 39:f51aa948acee 747 //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());
arsenalist 43:612547648ed1 748 sprintf(s,"POST /%s/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 749 connData = s;
arsenalist 9:a58c04da4476 750 }
arsenalist 9:a58c04da4476 751
arsenalist 9:a58c04da4476 752 //register
arsenalist 9:a58c04da4476 753 while(!emmaGetRegKey) {
arsenalist 38:e44f35f8129d 754 //DBG.printf("post:%s\r\n",s);
arsenalist 38:e44f35f8129d 755 str.clear();
arsenalist 40:77bd44f57ad3 756 str = ethREST(connHost,connPort,connData);
arsenalist 9:a58c04da4476 757 DBG.printf("rsp reg:%s\r\n",str.c_str());
arsenalist 9:a58c04da4476 758
arsenalist 48:053e92b6dc1e 759 //check and save settings
arsenalist 9:a58c04da4476 760 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 9:a58c04da4476 761 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 9:a58c04da4476 762 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 9:a58c04da4476 763
arsenalist 9:a58c04da4476 764 MbedJSONValue jsonValue;
arsenalist 9:a58c04da4476 765 parse(jsonValue,str.c_str());
arsenalist 9:a58c04da4476 766
arsenalist 38:e44f35f8129d 767 char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"};
arsenalist 9:a58c04da4476 768
arsenalist 38:e44f35f8129d 769 //save mqtt parameter
arsenalist 9:a58c04da4476 770 writeSetting(parameter[0],"()"); //sd card need to be initialized
arsenalist 9:a58c04da4476 771 for(int i=0; i<3; i++) {
arsenalist 9:a58c04da4476 772 if(jsonValue.hasMember(parameter[i])) {
arsenalist 9:a58c04da4476 773 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 9:a58c04da4476 774 int st = writeSetting(parameter[i],val.c_str());
arsenalist 9:a58c04da4476 775 if(st) {
arsenalist 9:a58c04da4476 776 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 9:a58c04da4476 777 } else {
arsenalist 9:a58c04da4476 778 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 9:a58c04da4476 779 }
arsenalist 9:a58c04da4476 780 }
arsenalist 9:a58c04da4476 781 }
arsenalist 9:a58c04da4476 782
arsenalist 9:a58c04da4476 783 //get registrationKey
arsenalist 9:a58c04da4476 784 if(jsonValue.hasMember(parameter[3])) {
arsenalist 9:a58c04da4476 785 string val = jsonValue[parameter[3]].get<std::string>();
arsenalist 9:a58c04da4476 786 if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
arsenalist 9:a58c04da4476 787 val.erase(val.begin(),val.begin()+val.find("(")+1);
arsenalist 9:a58c04da4476 788 val.erase(val.begin()+val.find(")"),val.end());
arsenalist 9:a58c04da4476 789 regKey = val;
arsenalist 9:a58c04da4476 790 DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 38:e44f35f8129d 791
arsenalist 38:e44f35f8129d 792 TFT.locate(0,20);
arsenalist 38:e44f35f8129d 793 TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 38:e44f35f8129d 794
arsenalist 9:a58c04da4476 795 emmaGetRegKey = true;
arsenalist 9:a58c04da4476 796 }
arsenalist 9:a58c04da4476 797 }
arsenalist 9:a58c04da4476 798 }
arsenalist 9:a58c04da4476 799 }
arsenalist 9:a58c04da4476 800
arsenalist 9:a58c04da4476 801 //calculate hmac
arsenalist 9:a58c04da4476 802 for(int i=0; i<sizeof(r); i++) {
arsenalist 9:a58c04da4476 803 r[i]=0; }
arsenalist 9:a58c04da4476 804 sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
arsenalist 9:a58c04da4476 805 hmac = calculateMD5(r);
arsenalist 9:a58c04da4476 806 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 9:a58c04da4476 807
arsenalist 39:f51aa948acee 808 //set connBody and connData
arsenalist 39:f51aa948acee 809 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 810
arsenalist 9:a58c04da4476 811 if(useProxy) {
arsenalist 9:a58c04da4476 812 DBG.printf("use proxy\r\n");
arsenalist 9:a58c04da4476 813 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 814 s[i]=0; }
arsenalist 50:50d98fe6cc15 815 sprintf(s,"POST http://%s:%d/%s/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 816 connData = s;
arsenalist 9:a58c04da4476 817 } else {
arsenalist 9:a58c04da4476 818 DBG.printf("no proxy\r\n");
arsenalist 9:a58c04da4476 819 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 820 s[i]=0; }
arsenalist 39:f51aa948acee 821 //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());
arsenalist 43:612547648ed1 822 sprintf(s,"POST /%s/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 823 connData = s;
arsenalist 9:a58c04da4476 824 }
arsenalist 9:a58c04da4476 825
arsenalist 9:a58c04da4476 826 //verify registration
arsenalist 9:a58c04da4476 827 while(!emmaRegistered && loop < 12){
arsenalist 9:a58c04da4476 828 str.clear();
arsenalist 40:77bd44f57ad3 829 str = ethREST(connHost,connPort,connData);
arsenalist 9:a58c04da4476 830 DBG.printf("rsp vrf:%s\r\n",str.c_str());
arsenalist 38:e44f35f8129d 831
arsenalist 38:e44f35f8129d 832 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 833 TFT.printf(" ");
arsenalist 38:e44f35f8129d 834 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 835 TFT.printf(" wait:%d\r\n",loop);
arsenalist 9:a58c04da4476 836
arsenalist 9:a58c04da4476 837 //check verification
arsenalist 9:a58c04da4476 838 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 9:a58c04da4476 839 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 9:a58c04da4476 840 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 9:a58c04da4476 841
arsenalist 9:a58c04da4476 842 MbedJSONValue jsonValue;
arsenalist 9:a58c04da4476 843 parse(jsonValue,str.c_str());
arsenalist 9:a58c04da4476 844 if(jsonValue.hasMember("user")) {
arsenalist 9:a58c04da4476 845 string val = jsonValue["user"].get<std::string>();
arsenalist 9:a58c04da4476 846 DBG.printf("%s is registered\r\n",val.c_str());
arsenalist 38:e44f35f8129d 847 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 848 TFT.printf(" ");
arsenalist 38:e44f35f8129d 849 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 850 TFT.printf(" %s is registered\r\n",val.c_str());
arsenalist 9:a58c04da4476 851 emmaRegistered = true;
arsenalist 9:a58c04da4476 852 }
arsenalist 9:a58c04da4476 853 }
arsenalist 9:a58c04da4476 854 wait(5);
arsenalist 9:a58c04da4476 855 loop++;
arsenalist 9:a58c04da4476 856 }
arsenalist 9:a58c04da4476 857
arsenalist 9:a58c04da4476 858 //check whether registration success
arsenalist 9:a58c04da4476 859 if(emmaRegistered) {
arsenalist 9:a58c04da4476 860 DBG.printf("registration successful\r\n");
arsenalist 38:e44f35f8129d 861 TFT.locate(0,60);
arsenalist 48:053e92b6dc1e 862 TFT.printf(" registration successful. please restart.\r\n");
arsenalist 9:a58c04da4476 863 } else {
arsenalist 9:a58c04da4476 864 DBG.printf("registration unsuccessful\r\n");
arsenalist 38:e44f35f8129d 865 TFT.locate(0,60);
arsenalist 38:e44f35f8129d 866 TFT.printf(" registration unsuccessful. please restart.\r\n");
arsenalist 9:a58c04da4476 867 }
arsenalist 9:a58c04da4476 868 while(1);
arsenalist 9:a58c04da4476 869
arsenalist 9:a58c04da4476 870 } else if(wifiConnected) {
arsenalist 0:f4e449fa34d7 871 DBG.printf("emmaModeRegister - wifi\r\n");
arsenalist 0:f4e449fa34d7 872
arsenalist 0:f4e449fa34d7 873 _ESP.printf("MODE=B");
arsenalist 0:f4e449fa34d7 874 wait(1);
arsenalist 0:f4e449fa34d7 875 while(!esp.ready());
arsenalist 0:f4e449fa34d7 876
arsenalist 0:f4e449fa34d7 877 //set connHost, connPort
arsenalist 0:f4e449fa34d7 878 if(useProxy) {
arsenalist 0:f4e449fa34d7 879 connHost = proxySERVER;
arsenalist 50:50d98fe6cc15 880 connPort = proxyPORT;
arsenalist 0:f4e449fa34d7 881 } else {
arsenalist 38:e44f35f8129d 882 connHost = restSERVER;
arsenalist 50:50d98fe6cc15 883 connPort = restPORT;
arsenalist 0:f4e449fa34d7 884 }
arsenalist 18:87f30ba9ddc5 885 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 886 TFT.printf(" emmaModeRegister");
arsenalist 0:f4e449fa34d7 887
arsenalist 0:f4e449fa34d7 888 //rest begin
arsenalist 0:f4e449fa34d7 889 if(!rest.begin(connHost.c_str(),connPort,false)) {
arsenalist 0:f4e449fa34d7 890 DBG.printf("EMMA: fail to setup rest");
arsenalist 32:78633fb9b2a1 891 TFT.locate(0,20);
arsenalist 32:78633fb9b2a1 892 TFT.printf(" fail setup connection");
arsenalist 32:78633fb9b2a1 893 TFT.locate(0,40);
arsenalist 32:78633fb9b2a1 894 TFT.printf(" check your wifi connection or");
arsenalist 32:78633fb9b2a1 895 TFT.locate(0,60);
arsenalist 32:78633fb9b2a1 896 TFT.printf(" you should setup proxy!");
arsenalist 0:f4e449fa34d7 897 while(1);
arsenalist 0:f4e449fa34d7 898 }
arsenalist 0:f4e449fa34d7 899 //wifiConnected = true; //with custom firmware, wifi module should connect automatically
arsenalist 0:f4e449fa34d7 900
arsenalist 0:f4e449fa34d7 901 esp.process();
arsenalist 0:f4e449fa34d7 902 if(wifiConnected) {
arsenalist 32:78633fb9b2a1 903 //check proxy
arsenalist 0:f4e449fa34d7 904 if(useProxy) {
arsenalist 50:50d98fe6cc15 905 sprintf(r,"http://%s:%d/%s/api/controller/register",restSERVER.c_str(),restPORT,appNAME.c_str());
arsenalist 0:f4e449fa34d7 906 } else {
arsenalist 43:612547648ed1 907 sprintf(r,"/%s/api/controller/register",appNAME.c_str());
arsenalist 0:f4e449fa34d7 908 }
arsenalist 0:f4e449fa34d7 909
arsenalist 0:f4e449fa34d7 910 //register
arsenalist 0:f4e449fa34d7 911 while(!emmaGetRegKey) {
arsenalist 32:78633fb9b2a1 912 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 913 //DBG.printf("s:%s\r\n",s);
arsenalist 32:78633fb9b2a1 914 //string iniStr = "{\"uid\":\"005300553533510334313732\",\"hmac\":\"45fc6a4447989a9434d1f21087a78061\"}";
arsenalist 32:78633fb9b2a1 915 //DBG.printf("s:%s\r\n",iniStr.c_str());
arsenalist 32:78633fb9b2a1 916
arsenalist 32:78633fb9b2a1 917 //rest.post(r,iniStr.c_str());
arsenalist 32:78633fb9b2a1 918 rest.post(r,s);
arsenalist 0:f4e449fa34d7 919 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 920 s[i]=0; }
arsenalist 0:f4e449fa34d7 921 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 922 DBG.printf("rsp reg:%s\r\n",s);
arsenalist 0:f4e449fa34d7 923
arsenalist 48:053e92b6dc1e 924 //check and save settings
arsenalist 0:f4e449fa34d7 925 str = s;
arsenalist 0:f4e449fa34d7 926 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 927 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 928 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 929
arsenalist 0:f4e449fa34d7 930 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 931 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 932
arsenalist 38:e44f35f8129d 933 char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"};
arsenalist 0:f4e449fa34d7 934
arsenalist 38:e44f35f8129d 935 //save mqtt parameter
arsenalist 0:f4e449fa34d7 936 writeSetting(parameter[0],"()"); //sd card need to be initialized
arsenalist 0:f4e449fa34d7 937 for(int i=0; i<3; i++) {
arsenalist 0:f4e449fa34d7 938 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 939 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 0:f4e449fa34d7 940 int st = writeSetting(parameter[i],val.c_str());
arsenalist 0:f4e449fa34d7 941 if(st) {
arsenalist 0:f4e449fa34d7 942 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 0:f4e449fa34d7 943 } else {
arsenalist 0:f4e449fa34d7 944 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 0:f4e449fa34d7 945 }
arsenalist 0:f4e449fa34d7 946 }
arsenalist 0:f4e449fa34d7 947 }
arsenalist 0:f4e449fa34d7 948
arsenalist 0:f4e449fa34d7 949 //get registrationKey
arsenalist 0:f4e449fa34d7 950 if(jsonValue.hasMember(parameter[3])) {
arsenalist 0:f4e449fa34d7 951 string val = jsonValue[parameter[3]].get<std::string>();
arsenalist 0:f4e449fa34d7 952 if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 953 val.erase(val.begin(),val.begin()+val.find("(")+1);
arsenalist 0:f4e449fa34d7 954 val.erase(val.begin()+val.find(")"),val.end());
arsenalist 0:f4e449fa34d7 955 regKey = val;
arsenalist 0:f4e449fa34d7 956 DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 18:87f30ba9ddc5 957
arsenalist 18:87f30ba9ddc5 958 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 959 TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 18:87f30ba9ddc5 960
arsenalist 0:f4e449fa34d7 961 emmaGetRegKey = true;
arsenalist 0:f4e449fa34d7 962 }
arsenalist 0:f4e449fa34d7 963 }
arsenalist 0:f4e449fa34d7 964 }
arsenalist 32:78633fb9b2a1 965 wait(1);
arsenalist 0:f4e449fa34d7 966 }
arsenalist 0:f4e449fa34d7 967
arsenalist 0:f4e449fa34d7 968 //calculate hmac
arsenalist 0:f4e449fa34d7 969 for(int i=0; i<sizeof(r); i++) {
arsenalist 0:f4e449fa34d7 970 r[i]=0; }
arsenalist 0:f4e449fa34d7 971 sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
arsenalist 0:f4e449fa34d7 972 hmac = calculateMD5(r);
arsenalist 0:f4e449fa34d7 973 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 0:f4e449fa34d7 974
arsenalist 32:78633fb9b2a1 975 //check proxy
arsenalist 0:f4e449fa34d7 976 if(useProxy) {
arsenalist 50:50d98fe6cc15 977 sprintf(r,"http://%s:%d/%s/api/controller/verify",restSERVER.c_str(),restPORT,appNAME.c_str());
arsenalist 0:f4e449fa34d7 978 } else {
arsenalist 43:612547648ed1 979 sprintf(r,"/%s/api/controller/verify",appNAME.c_str());
arsenalist 0:f4e449fa34d7 980 }
arsenalist 0:f4e449fa34d7 981
arsenalist 0:f4e449fa34d7 982 //verify registration
arsenalist 0:f4e449fa34d7 983 while(!emmaRegistered && loop < 12){
arsenalist 32:78633fb9b2a1 984 sprintf(s,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
arsenalist 32:78633fb9b2a1 985 rest.post(r,s);
arsenalist 0:f4e449fa34d7 986 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 987 DBG.printf("rsp vrf:%s\r\n",s);
arsenalist 18:87f30ba9ddc5 988 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 989 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 990 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 991 TFT.printf(" wait:%d\r\n",loop);
arsenalist 0:f4e449fa34d7 992
arsenalist 0:f4e449fa34d7 993 //check verification
arsenalist 0:f4e449fa34d7 994 str = s;
arsenalist 0:f4e449fa34d7 995 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 996 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 997 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 998
arsenalist 0:f4e449fa34d7 999 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 1000 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 1001 if(jsonValue.hasMember("user")) {
arsenalist 0:f4e449fa34d7 1002 string val = jsonValue["user"].get<std::string>();
arsenalist 18:87f30ba9ddc5 1003 DBG.printf(" %s is registered\r\n",val.c_str());
arsenalist 18:87f30ba9ddc5 1004 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 1005 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 1006 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 1007 TFT.printf(" %s is registered\r\n",val.c_str());
arsenalist 0:f4e449fa34d7 1008 emmaRegistered = true;
arsenalist 0:f4e449fa34d7 1009 }
arsenalist 0:f4e449fa34d7 1010 }
arsenalist 0:f4e449fa34d7 1011 wait(5);
arsenalist 0:f4e449fa34d7 1012 loop++;
arsenalist 0:f4e449fa34d7 1013 }
arsenalist 0:f4e449fa34d7 1014
arsenalist 0:f4e449fa34d7 1015 //check whether registration success
arsenalist 0:f4e449fa34d7 1016 if(emmaRegistered) {
arsenalist 0:f4e449fa34d7 1017 DBG.printf("registration successful\r\n");
arsenalist 18:87f30ba9ddc5 1018 TFT.locate(0,60);
arsenalist 48:053e92b6dc1e 1019 TFT.printf(" registration successful. please restart.\r\n");
arsenalist 0:f4e449fa34d7 1020 } else {
arsenalist 0:f4e449fa34d7 1021 DBG.printf("registration unsuccessful\r\n");
arsenalist 18:87f30ba9ddc5 1022 TFT.locate(0,60);
arsenalist 38:e44f35f8129d 1023 TFT.printf(" registration unsuccessful. please restart.\r\n");
arsenalist 0:f4e449fa34d7 1024 }
arsenalist 0:f4e449fa34d7 1025 while(1);
arsenalist 0:f4e449fa34d7 1026 }
arsenalist 9:a58c04da4476 1027 } else if(gprsConnected) {
arsenalist 0:f4e449fa34d7 1028 DBG.printf("emmaModeRegister - gprs\r\n");
arsenalist 0:f4e449fa34d7 1029
arsenalist 48:053e92b6dc1e 1030 //register
arsenalist 50:50d98fe6cc15 1031 connPort = restPORT;
arsenalist 48:053e92b6dc1e 1032 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 49:d11a1914ee8b 1033 sprintf(s,"POST /%s/api/controller/register HTTP/1.1\nHost: %s:%d\nContent-Length: %d\n\n%s\n\n%c",appNAME.c_str(),restSERVER.c_str(),connPort,connBodyLen,connBody,26);
arsenalist 48:053e92b6dc1e 1034 while(!emmaGetRegKey) {
arsenalist 48:053e92b6dc1e 1035 str = gprsREST(restSERVER,connPort,s);
arsenalist 48:053e92b6dc1e 1036
arsenalist 48:053e92b6dc1e 1037 //check and save settings
arsenalist 48:053e92b6dc1e 1038 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 48:053e92b6dc1e 1039 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 48:053e92b6dc1e 1040 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 48:053e92b6dc1e 1041
arsenalist 48:053e92b6dc1e 1042 MbedJSONValue jsonValue;
arsenalist 48:053e92b6dc1e 1043 parse(jsonValue,str.c_str());
arsenalist 48:053e92b6dc1e 1044
arsenalist 48:053e92b6dc1e 1045 char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"};
arsenalist 48:053e92b6dc1e 1046
arsenalist 48:053e92b6dc1e 1047 //save mqtt parameter
arsenalist 48:053e92b6dc1e 1048 writeSetting(parameter[0],"()"); //sd card need to be initialized
arsenalist 48:053e92b6dc1e 1049 for(int i=0; i<3; i++) {
arsenalist 48:053e92b6dc1e 1050 if(jsonValue.hasMember(parameter[i])) {
arsenalist 48:053e92b6dc1e 1051 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 48:053e92b6dc1e 1052 int st = writeSetting(parameter[i],val.c_str());
arsenalist 48:053e92b6dc1e 1053 if(st) {
arsenalist 48:053e92b6dc1e 1054 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 48:053e92b6dc1e 1055 } else {
arsenalist 48:053e92b6dc1e 1056 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 48:053e92b6dc1e 1057 }
arsenalist 48:053e92b6dc1e 1058 }
arsenalist 48:053e92b6dc1e 1059 }
arsenalist 48:053e92b6dc1e 1060
arsenalist 48:053e92b6dc1e 1061 //get registrationKey
arsenalist 48:053e92b6dc1e 1062 if(jsonValue.hasMember(parameter[3])) {
arsenalist 48:053e92b6dc1e 1063 string val = jsonValue[parameter[3]].get<std::string>();
arsenalist 48:053e92b6dc1e 1064 if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
arsenalist 48:053e92b6dc1e 1065 val.erase(val.begin(),val.begin()+val.find("(")+1);
arsenalist 48:053e92b6dc1e 1066 val.erase(val.begin()+val.find(")"),val.end());
arsenalist 48:053e92b6dc1e 1067 regKey = val;
arsenalist 48:053e92b6dc1e 1068 DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 48:053e92b6dc1e 1069
arsenalist 48:053e92b6dc1e 1070 TFT.locate(0,20);
arsenalist 48:053e92b6dc1e 1071 TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 48:053e92b6dc1e 1072
arsenalist 48:053e92b6dc1e 1073 emmaGetRegKey = true;
arsenalist 48:053e92b6dc1e 1074 }
arsenalist 48:053e92b6dc1e 1075 }
arsenalist 48:053e92b6dc1e 1076 }
arsenalist 48:053e92b6dc1e 1077 wait(1);
arsenalist 48:053e92b6dc1e 1078 }
arsenalist 48:053e92b6dc1e 1079
arsenalist 48:053e92b6dc1e 1080 //calculate hmac
arsenalist 48:053e92b6dc1e 1081 sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
arsenalist 48:053e92b6dc1e 1082 hmac = calculateMD5(r);
arsenalist 48:053e92b6dc1e 1083 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 48:053e92b6dc1e 1084
arsenalist 48:053e92b6dc1e 1085 //verify registration
arsenalist 48:053e92b6dc1e 1086 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
arsenalist 49:d11a1914ee8b 1087 sprintf(s,"POST /%s/api/controller/verify HTTP/1.1\nHost: %s:%d\nContent-Length: %d\n\n%s\n\n%c",appNAME.c_str(),restSERVER.c_str(),connPort,connBodyLen,connBody,26);
arsenalist 48:053e92b6dc1e 1088 while(!emmaRegistered && loop < 12) {
arsenalist 48:053e92b6dc1e 1089 str = gprsREST(restSERVER,connPort,s);
arsenalist 48:053e92b6dc1e 1090 DBG.printf("rsp vrf:%s\r\n",str.c_str());
arsenalist 48:053e92b6dc1e 1091 TFT.locate(0,40);
arsenalist 48:053e92b6dc1e 1092 TFT.printf(" ");
arsenalist 48:053e92b6dc1e 1093 TFT.locate(0,40);
arsenalist 48:053e92b6dc1e 1094 TFT.printf(" wait:%d\r\n",loop);
arsenalist 48:053e92b6dc1e 1095
arsenalist 48:053e92b6dc1e 1096 //check verification
arsenalist 48:053e92b6dc1e 1097 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 48:053e92b6dc1e 1098 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 48:053e92b6dc1e 1099 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 48:053e92b6dc1e 1100
arsenalist 48:053e92b6dc1e 1101 MbedJSONValue jsonValue;
arsenalist 48:053e92b6dc1e 1102 parse(jsonValue,str.c_str());
arsenalist 48:053e92b6dc1e 1103 if(jsonValue.hasMember("user")) {
arsenalist 48:053e92b6dc1e 1104 string val = jsonValue["user"].get<std::string>();
arsenalist 48:053e92b6dc1e 1105 DBG.printf(" %s is registered\r\n",val.c_str());
arsenalist 48:053e92b6dc1e 1106 TFT.locate(0,40);
arsenalist 48:053e92b6dc1e 1107 TFT.printf(" ");
arsenalist 48:053e92b6dc1e 1108 TFT.locate(0,40);
arsenalist 48:053e92b6dc1e 1109 TFT.printf(" %s is registered\r\n",val.c_str());
arsenalist 48:053e92b6dc1e 1110 emmaRegistered = true;
arsenalist 48:053e92b6dc1e 1111 }
arsenalist 48:053e92b6dc1e 1112 }
arsenalist 48:053e92b6dc1e 1113 wait(5);
arsenalist 48:053e92b6dc1e 1114 loop++;
arsenalist 48:053e92b6dc1e 1115 }
arsenalist 48:053e92b6dc1e 1116
arsenalist 48:053e92b6dc1e 1117 //check whether registration success
arsenalist 48:053e92b6dc1e 1118 if(emmaRegistered) {
arsenalist 48:053e92b6dc1e 1119 DBG.printf("registration successful\r\n");
arsenalist 48:053e92b6dc1e 1120 TFT.locate(0,60);
arsenalist 48:053e92b6dc1e 1121 TFT.printf(" registration successful. please restart.\r\n");
arsenalist 48:053e92b6dc1e 1122 } else {
arsenalist 48:053e92b6dc1e 1123 DBG.printf("registration unsuccessful\r\n");
arsenalist 48:053e92b6dc1e 1124 TFT.locate(0,60);
arsenalist 48:053e92b6dc1e 1125 TFT.printf(" registration unsuccessful. please restart.\r\n");
arsenalist 48:053e92b6dc1e 1126 }
arsenalist 48:053e92b6dc1e 1127 while(1);
arsenalist 48:053e92b6dc1e 1128
arsenalist 0:f4e449fa34d7 1129 } else {
arsenalist 9:a58c04da4476 1130 DBG.printf("no eth, wifi, or gprs is connected\r\n");
arsenalist 18:87f30ba9ddc5 1131 TFT.locate(0,60);
arsenalist 18:87f30ba9ddc5 1132 TFT.printf(" no iface connected. please restart.\r\n");
arsenalist 0:f4e449fa34d7 1133 }
arsenalist 0:f4e449fa34d7 1134 }
arsenalist 0:f4e449fa34d7 1135
arsenalist 0:f4e449fa34d7 1136 void emmaModeOperation(void) {
arsenalist 27:562a95c120cc 1137 //char mqttClientId[32];
arsenalist 40:77bd44f57ad3 1138 char connBody[256];
arsenalist 22:e18d361bf688 1139 char p[64];
arsenalist 14:8287f0f5d987 1140 char q[32];
arsenalist 4:76ab12e2f8a3 1141 char r[32];
arsenalist 45:2406c2b6995a 1142 char s[4096];
arsenalist 40:77bd44f57ad3 1143 int connBodyLen;
arsenalist 38:e44f35f8129d 1144 int connPort;
arsenalist 0:f4e449fa34d7 1145 int loop=0;
arsenalist 21:33bd8b82560f 1146 int trial=0;
arsenalist 22:e18d361bf688 1147 string hmacTime;
arsenalist 23:fb369e171b7d 1148 string hmacCmd;
arsenalist 0:f4e449fa34d7 1149 string str;
arsenalist 14:8287f0f5d987 1150 time_t seconds;
arsenalist 0:f4e449fa34d7 1151 Timer t;
arsenalist 47:97eadcb478b2 1152 Timer tAlert;
arsenalist 24:5d58515ba510 1153 Timer tPanelEnergy;
arsenalist 24:5d58515ba510 1154 Timer tPanel;
arsenalist 4:76ab12e2f8a3 1155 Timer tNodes;
arsenalist 0:f4e449fa34d7 1156
arsenalist 19:7e3e9332f719 1157 TFT.locate(0,0);
arsenalist 19:7e3e9332f719 1158 TFT.printf(" please wait");
arsenalist 19:7e3e9332f719 1159
arsenalist 0:f4e449fa34d7 1160 //check connected interface
arsenalist 8:51a0ca9079ca 1161 isEthConnected();
arsenalist 8:51a0ca9079ca 1162 isWiFiConnected();
arsenalist 8:51a0ca9079ca 1163 isGprsConnected();
arsenalist 0:f4e449fa34d7 1164 DBG.printf("ethConnected:%d\r\n",ethConnected);
arsenalist 0:f4e449fa34d7 1165 DBG.printf("wifiConnected:%d\r\n",wifiConnected);
arsenalist 48:053e92b6dc1e 1166 DBG.printf("gprsConnected:%d\r\n",gprsConnected);
arsenalist 0:f4e449fa34d7 1167
arsenalist 13:e8adfe305dbc 1168 TFT.locate(0,0);
arsenalist 13:e8adfe305dbc 1169 TFT.printf(" emmaModeOperation");
arsenalist 13:e8adfe305dbc 1170
arsenalist 13:e8adfe305dbc 1171 TFT.locate(0,20);
arsenalist 44:c1d11c491237 1172 TFT.printf(" Interface:");
arsenalist 44:c1d11c491237 1173
arsenalist 44:c1d11c491237 1174 TFT.locate(75,20);
arsenalist 13:e8adfe305dbc 1175 if(ethConnected) {
arsenalist 44:c1d11c491237 1176 TFT.printf("ETH");
arsenalist 44:c1d11c491237 1177 } else if(wifiConnected) {
arsenalist 44:c1d11c491237 1178 TFT.printf("WiFi");
arsenalist 44:c1d11c491237 1179 } else if(gprsConnected) {
arsenalist 44:c1d11c491237 1180 TFT.printf("GPRS");
arsenalist 13:e8adfe305dbc 1181 } else {
arsenalist 44:c1d11c491237 1182 TFT.printf("N/A");
arsenalist 13:e8adfe305dbc 1183 }
arsenalist 13:e8adfe305dbc 1184
arsenalist 44:c1d11c491237 1185 //set ade7758 parameter
arsenalist 44:c1d11c491237 1186 ADE.begin();
arsenalist 44:c1d11c491237 1187 ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET);
arsenalist 44:c1d11c491237 1188 ADE.write16bits(AWG, 0);
arsenalist 44:c1d11c491237 1189 ADE.write16bits(BWG, 0);
arsenalist 44:c1d11c491237 1190 ADE.write16bits(CWG, 0);
arsenalist 44:c1d11c491237 1191 ADE.write16bits(AVAG, 0);
arsenalist 44:c1d11c491237 1192 ADE.write16bits(BVAG, 0);
arsenalist 44:c1d11c491237 1193 ADE.write16bits(CVAG, 0);
arsenalist 13:e8adfe305dbc 1194
arsenalist 10:1dc69e0ce7fd 1195 if(ethConnected) {
arsenalist 10:1dc69e0ce7fd 1196 DBG.printf("emmaModeOperation - eth\r\n");
arsenalist 44:c1d11c491237 1197
arsenalist 44:c1d11c491237 1198 //new log indicator
arsenalist 40:77bd44f57ad3 1199 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1200 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1201 DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++Ethernet++++++++++"));
arsenalist 40:77bd44f57ad3 1202
arsenalist 40:77bd44f57ad3 1203 //check firmware update
arsenalist 40:77bd44f57ad3 1204
arsenalist 40:77bd44f57ad3 1205 //execute last state of switches on board
arsenalist 40:77bd44f57ad3 1206
arsenalist 40:77bd44f57ad3 1207 //get list of nodes from server
arsenalist 40:77bd44f57ad3 1208 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 46:86a0c2133130 1209 sprintf(s,"POST /%s/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 50:50d98fe6cc15 1210 connPort = restPORT;
arsenalist 40:77bd44f57ad3 1211 str.clear();
arsenalist 40:77bd44f57ad3 1212 str = ethREST(restSERVER,connPort,s);
arsenalist 44:c1d11c491237 1213 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 46:86a0c2133130 1214
arsenalist 40:77bd44f57ad3 1215 if(str.rfind("[{\"type\"") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1216 DBG.printf("get nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1217 str.erase(str.begin(),str.begin()+str.rfind("[{\"type\""));
arsenalist 40:77bd44f57ad3 1218 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 40:77bd44f57ad3 1219
arsenalist 40:77bd44f57ad3 1220 MbedJSONValue jsonValue;
arsenalist 40:77bd44f57ad3 1221 parse(jsonValue,str.c_str());
arsenalist 40:77bd44f57ad3 1222 char *parameter[3] = {"type","mac","ip"};
arsenalist 40:77bd44f57ad3 1223
arsenalist 40:77bd44f57ad3 1224 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1225 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1226 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1227 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 40:77bd44f57ad3 1228 wait(0.5);
arsenalist 40:77bd44f57ad3 1229 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1230 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1231
arsenalist 40:77bd44f57ad3 1232 //check whether nodes valid
arsenalist 40:77bd44f57ad3 1233 bool validNodes = true;
arsenalist 40:77bd44f57ad3 1234 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1235 for(int j=0; j<3; j++) {
arsenalist 40:77bd44f57ad3 1236 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 40:77bd44f57ad3 1237 }
arsenalist 40:77bd44f57ad3 1238 }
arsenalist 40:77bd44f57ad3 1239 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 40:77bd44f57ad3 1240
arsenalist 40:77bd44f57ad3 1241 if(validNodes) {
arsenalist 40:77bd44f57ad3 1242 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 44:c1d11c491237 1243 int typeValue = jsonValue[i][parameter[0]].get<int>();
arsenalist 40:77bd44f57ad3 1244 string macValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 40:77bd44f57ad3 1245 string ipValue = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 40:77bd44f57ad3 1246 nodes[i].type = typeValue;
arsenalist 40:77bd44f57ad3 1247 nodes[i].macAddr = macValue;
arsenalist 40:77bd44f57ad3 1248 nodes[i].ipAddr = ipValue;
arsenalist 44:c1d11c491237 1249 DBG.printf("nodes[%d]type:%d\r\n",i,nodes[i].type);
arsenalist 40:77bd44f57ad3 1250 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 40:77bd44f57ad3 1251 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 40:77bd44f57ad3 1252 }
arsenalist 36:ed696fd1b44e 1253 }
arsenalist 40:77bd44f57ad3 1254
arsenalist 40:77bd44f57ad3 1255 } else {
arsenalist 40:77bd44f57ad3 1256 DBG.printf("no nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1257 }
arsenalist 40:77bd44f57ad3 1258
arsenalist 40:77bd44f57ad3 1259 //define thread
arsenalist 40:77bd44f57ad3 1260 osThreadDef(energyThread, osPriorityBelowNormal, (8*DEFAULT_STACK_SIZE));
arsenalist 40:77bd44f57ad3 1261 //create thread
arsenalist 40:77bd44f57ad3 1262 osThreadCreate(osThread(energyThread),NULL);
arsenalist 40:77bd44f57ad3 1263
arsenalist 47:97eadcb478b2 1264 tAlert.start();
arsenalist 40:77bd44f57ad3 1265 tPanelEnergy.start();
arsenalist 40:77bd44f57ad3 1266 tPanel.start();
arsenalist 40:77bd44f57ad3 1267 tNodes.start();
arsenalist 40:77bd44f57ad3 1268 wait(1);
arsenalist 40:77bd44f57ad3 1269 while(1) {
arsenalist 49:d11a1914ee8b 1270 checkVoltagePower();
arsenalist 47:97eadcb478b2 1271
arsenalist 47:97eadcb478b2 1272 //set allowAlertXxxx to enable controller send alert
arsenalist 47:97eadcb478b2 1273 if(tAlert.read() > 300.0f) { //300.0f is 5 minutes
arsenalist 47:97eadcb478b2 1274 allowAlertAVrms = true;
arsenalist 47:97eadcb478b2 1275 allowAlertAWatt = true;
arsenalist 47:97eadcb478b2 1276 allowAlertBVrms = true;
arsenalist 47:97eadcb478b2 1277 allowAlertBWatt = true;
arsenalist 47:97eadcb478b2 1278 allowAlertCVrms = true;
arsenalist 47:97eadcb478b2 1279 allowAlertCWatt = true;
arsenalist 47:97eadcb478b2 1280 tAlert.reset();
arsenalist 47:97eadcb478b2 1281 }
arsenalist 40:77bd44f57ad3 1282
arsenalist 40:77bd44f57ad3 1283 //panelEnergy, panelVoltage, and panelPower
arsenalist 40:77bd44f57ad3 1284 if(tPanelEnergy.read() > 30.0f) {
arsenalist 40:77bd44f57ad3 1285 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
arsenalist 40:77bd44f57ad3 1286 TFT.locate(0,60);
arsenalist 40:77bd44f57ad3 1287 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1288 TFT.locate(0,60);
arsenalist 40:77bd44f57ad3 1289 TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
arsenalist 40:77bd44f57ad3 1290
arsenalist 40:77bd44f57ad3 1291 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
arsenalist 40:77bd44f57ad3 1292 TFT.locate(0,80);
arsenalist 40:77bd44f57ad3 1293 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1294 TFT.locate(0,80);
arsenalist 40:77bd44f57ad3 1295 TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
arsenalist 40:77bd44f57ad3 1296
arsenalist 40:77bd44f57ad3 1297 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
arsenalist 40:77bd44f57ad3 1298 TFT.locate(0,100);
arsenalist 40:77bd44f57ad3 1299 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1300 TFT.locate(0,100);
arsenalist 40:77bd44f57ad3 1301 TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
arsenalist 40:77bd44f57ad3 1302
arsenalist 40:77bd44f57ad3 1303 if(newEnergyData) {
arsenalist 40:77bd44f57ad3 1304 //for(int i=1; i<4; i++) {
arsenalist 49:d11a1914ee8b 1305 for(int i=1; i<(powerPHASE+1); i++) {
arsenalist 40:77bd44f57ad3 1306 DBG.printf("sending channel: %d\r\n",i);
arsenalist 40:77bd44f57ad3 1307 if(i==1){
arsenalist 40:77bd44f57ad3 1308 XWattHr = AWattHr;
arsenalist 40:77bd44f57ad3 1309 XVrms = AVrms;
arsenalist 40:77bd44f57ad3 1310 XWatt = AWatt;
arsenalist 40:77bd44f57ad3 1311 } else if(i==2) {
arsenalist 40:77bd44f57ad3 1312 XWattHr = BWattHr;
arsenalist 40:77bd44f57ad3 1313 XVrms = BVrms;
arsenalist 40:77bd44f57ad3 1314 XWatt = BWatt;
arsenalist 40:77bd44f57ad3 1315 } else {
arsenalist 40:77bd44f57ad3 1316 XWattHr = CWattHr;
arsenalist 40:77bd44f57ad3 1317 XVrms = CVrms;
arsenalist 40:77bd44f57ad3 1318 XWatt = CWatt;
arsenalist 40:77bd44f57ad3 1319 }
arsenalist 40:77bd44f57ad3 1320
arsenalist 40:77bd44f57ad3 1321 if(XWattHr != 0.0f) {
arsenalist 40:77bd44f57ad3 1322 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1323 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1324
arsenalist 40:77bd44f57ad3 1325 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 40:77bd44f57ad3 1326 hmacTime = calculateMD5(p);
arsenalist 40:77bd44f57ad3 1327
arsenalist 40:77bd44f57ad3 1328 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 40:77bd44f57ad3 1329 emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
arsenalist 46:86a0c2133130 1330 sprintf(s,"POST /%s/api/controller/energy/%d HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),i,restSERVER.c_str(),connBodyLen,connBody);
arsenalist 40:77bd44f57ad3 1331
arsenalist 46:86a0c2133130 1332 //DBG.printf("dataEnergy:\r\n%s\r\n",connBody);
arsenalist 40:77bd44f57ad3 1333
arsenalist 50:50d98fe6cc15 1334 connPort = restPORT;
arsenalist 40:77bd44f57ad3 1335 str.clear();
arsenalist 46:86a0c2133130 1336 str = ethREST(restSERVER,connPort,s);
arsenalist 46:86a0c2133130 1337 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 46:86a0c2133130 1338
arsenalist 40:77bd44f57ad3 1339 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1340 //logging purpose
arsenalist 40:77bd44f57ad3 1341 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1342 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1343 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData success"));
arsenalist 40:77bd44f57ad3 1344
arsenalist 40:77bd44f57ad3 1345 DBG.printf("send channel: %d success\r\n",i);
arsenalist 40:77bd44f57ad3 1346 TFT.foreground(Green);
arsenalist 40:77bd44f57ad3 1347 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1348 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1349 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1350 TFT.printf("send ch%d success",i);
arsenalist 40:77bd44f57ad3 1351 wait(1);
arsenalist 40:77bd44f57ad3 1352 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1353 TFT.printf(" ");
arsenalist 44:c1d11c491237 1354 TFT.foreground(White);
arsenalist 40:77bd44f57ad3 1355 } else {
arsenalist 40:77bd44f57ad3 1356 //logging purpose
arsenalist 40:77bd44f57ad3 1357 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1358 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1359 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData failed"));
arsenalist 40:77bd44f57ad3 1360
arsenalist 40:77bd44f57ad3 1361 DBG.printf("send channel: %d failed\r\n",i);
arsenalist 40:77bd44f57ad3 1362 TFT.foreground(Red);
arsenalist 40:77bd44f57ad3 1363 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1364 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1365 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1366 TFT.printf("send ch%d failed",i);
arsenalist 40:77bd44f57ad3 1367 wait(1);
arsenalist 40:77bd44f57ad3 1368 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1369 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1370 TFT.foreground(White);
arsenalist 40:77bd44f57ad3 1371 }
arsenalist 40:77bd44f57ad3 1372 }
arsenalist 40:77bd44f57ad3 1373 }
arsenalist 40:77bd44f57ad3 1374 newEnergyData = false;
arsenalist 40:77bd44f57ad3 1375 }
arsenalist 40:77bd44f57ad3 1376
arsenalist 40:77bd44f57ad3 1377 tPanelEnergy.reset();
arsenalist 40:77bd44f57ad3 1378 loop++;
arsenalist 36:ed696fd1b44e 1379 }
arsenalist 40:77bd44f57ad3 1380
arsenalist 40:77bd44f57ad3 1381 //panel environment
arsenalist 46:86a0c2133130 1382 if(tPanel.read() > 900.0f) { //900.0f is 15 minutes
arsenalist 46:86a0c2133130 1383 int dTemp=0;
arsenalist 46:86a0c2133130 1384 int dHum=0;
arsenalist 46:86a0c2133130 1385 int dGas=0;
arsenalist 46:86a0c2133130 1386
arsenalist 46:86a0c2133130 1387 DBG.printf("getPanelEnvironment\r\n");
arsenalist 46:86a0c2133130 1388
arsenalist 46:86a0c2133130 1389 //get environment sensor
arsenalist 46:86a0c2133130 1390 trial=0;
arsenalist 46:86a0c2133130 1391 while(1) {
arsenalist 46:86a0c2133130 1392 if(trial>=2) { //two times trial
arsenalist 46:86a0c2133130 1393 break;
arsenalist 46:86a0c2133130 1394 }
arsenalist 46:86a0c2133130 1395 if(d.readData() == DHT11::OK) {
arsenalist 46:86a0c2133130 1396 dTemp = d.readTemperature();
arsenalist 46:86a0c2133130 1397 dHum = d.readHumidity();
arsenalist 46:86a0c2133130 1398 break;
arsenalist 46:86a0c2133130 1399 }
arsenalist 46:86a0c2133130 1400 trial++;
arsenalist 46:86a0c2133130 1401 wait(3);
arsenalist 46:86a0c2133130 1402 }
arsenalist 46:86a0c2133130 1403
arsenalist 46:86a0c2133130 1404 //send environment sensor
arsenalist 46:86a0c2133130 1405 if(dTemp!=0 && dHum!=0) {
arsenalist 46:86a0c2133130 1406 seconds = time(NULL);
arsenalist 46:86a0c2133130 1407 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 46:86a0c2133130 1408
arsenalist 46:86a0c2133130 1409 //calculate hmacTime
arsenalist 46:86a0c2133130 1410 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 46:86a0c2133130 1411 hmacTime = calculateMD5(p);
arsenalist 46:86a0c2133130 1412
arsenalist 46:86a0c2133130 1413 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
arsenalist 46:86a0c2133130 1414 emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas);
arsenalist 46:86a0c2133130 1415 sprintf(s,"POST /%s/api/controller/environment HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 46:86a0c2133130 1416
arsenalist 46:86a0c2133130 1417 //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
arsenalist 46:86a0c2133130 1418
arsenalist 50:50d98fe6cc15 1419 connPort = restPORT;
arsenalist 46:86a0c2133130 1420 str.clear();
arsenalist 46:86a0c2133130 1421 str = ethREST(restSERVER,connPort,s);
arsenalist 46:86a0c2133130 1422 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 46:86a0c2133130 1423
arsenalist 46:86a0c2133130 1424 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 46:86a0c2133130 1425 //logging purpose
arsenalist 46:86a0c2133130 1426 seconds = time(NULL);
arsenalist 46:86a0c2133130 1427 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 46:86a0c2133130 1428 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv success"));
arsenalist 46:86a0c2133130 1429 DBG.printf("send panel environment success\r\n");
arsenalist 46:86a0c2133130 1430 } else {
arsenalist 46:86a0c2133130 1431 //logging purpose
arsenalist 46:86a0c2133130 1432 seconds = time(NULL);
arsenalist 46:86a0c2133130 1433 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 46:86a0c2133130 1434 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv failed"));
arsenalist 46:86a0c2133130 1435 DBG.printf("send panel environment failed\r\n");
arsenalist 46:86a0c2133130 1436 }
arsenalist 46:86a0c2133130 1437 }
arsenalist 46:86a0c2133130 1438 tPanel.reset();
arsenalist 46:86a0c2133130 1439 }
arsenalist 46:86a0c2133130 1440
arsenalist 46:86a0c2133130 1441 //node temp
arsenalist 46:86a0c2133130 1442 if(tNodes.read() > 60.0f) { //900 is 15 minutes
arsenalist 46:86a0c2133130 1443 DBG.printf("getNodesTemperature\r\n");
arsenalist 46:86a0c2133130 1444
arsenalist 46:86a0c2133130 1445 for(int i=0; i<NODES_MAX; i++) {
arsenalist 46:86a0c2133130 1446 if(!nodes[i].ipAddr.empty() && nodes[i].type == 1) {
arsenalist 46:86a0c2133130 1447 //get node's temp
arsenalist 46:86a0c2133130 1448 string temp;
arsenalist 46:86a0c2133130 1449 str.clear();
arsenalist 46:86a0c2133130 1450 str = ethREST(nodes[i].ipAddr,REMOTE_TCP_PORT,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
arsenalist 46:86a0c2133130 1451 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 46:86a0c2133130 1452
arsenalist 46:86a0c2133130 1453 if(str.find("200 OK")) {
arsenalist 46:86a0c2133130 1454 if(str.rfind("temp=") != std::string::npos) {
arsenalist 46:86a0c2133130 1455 str.erase(str.begin(),str.begin()+str.rfind("temp=")+6);
arsenalist 46:86a0c2133130 1456 str.erase(str.begin()+str.find("\""),str.end());
arsenalist 46:86a0c2133130 1457 temp = str;
arsenalist 46:86a0c2133130 1458 } else {
arsenalist 46:86a0c2133130 1459 temp = "0"; //connect to node, but receive none
arsenalist 46:86a0c2133130 1460 }
arsenalist 46:86a0c2133130 1461 } else {
arsenalist 46:86a0c2133130 1462 temp = "0"; //not connected to node
arsenalist 46:86a0c2133130 1463 }
arsenalist 46:86a0c2133130 1464 DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
arsenalist 46:86a0c2133130 1465
arsenalist 46:86a0c2133130 1466 //send node's temp
arsenalist 46:86a0c2133130 1467 if(temp != "0") {
arsenalist 46:86a0c2133130 1468 seconds = time(NULL);
arsenalist 46:86a0c2133130 1469 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 46:86a0c2133130 1470
arsenalist 46:86a0c2133130 1471 //calculate hmacTime
arsenalist 46:86a0c2133130 1472 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 46:86a0c2133130 1473 hmacTime = calculateMD5(p);
arsenalist 46:86a0c2133130 1474
arsenalist 46:86a0c2133130 1475 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":%s,\"value\":%s}",
arsenalist 46:86a0c2133130 1476 emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
arsenalist 46:86a0c2133130 1477 sprintf(s,"POST /%s/api/controller/nodetemp HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 46:86a0c2133130 1478
arsenalist 46:86a0c2133130 1479 //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
arsenalist 46:86a0c2133130 1480
arsenalist 50:50d98fe6cc15 1481 connPort = restPORT;
arsenalist 46:86a0c2133130 1482 str.clear();
arsenalist 46:86a0c2133130 1483 str = ethREST(restSERVER,connPort,s);
arsenalist 46:86a0c2133130 1484 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 46:86a0c2133130 1485
arsenalist 46:86a0c2133130 1486 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 46:86a0c2133130 1487 DBG.printf("send nodeTemp success\r\n");
arsenalist 46:86a0c2133130 1488 } else {
arsenalist 46:86a0c2133130 1489 DBG.printf("send nodeTemp failed\r\n");
arsenalist 46:86a0c2133130 1490 }
arsenalist 46:86a0c2133130 1491 }
arsenalist 46:86a0c2133130 1492 }
arsenalist 46:86a0c2133130 1493 }
arsenalist 46:86a0c2133130 1494 tNodes.reset();
arsenalist 46:86a0c2133130 1495 }
arsenalist 40:77bd44f57ad3 1496
arsenalist 44:c1d11c491237 1497 //command
arsenalist 45:2406c2b6995a 1498 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 45:2406c2b6995a 1499 sprintf(s,"POST /%s/api/controller/command HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 50:50d98fe6cc15 1500 connPort = restPORT;
arsenalist 45:2406c2b6995a 1501 str = ethREST(restSERVER,connPort,s);
arsenalist 45:2406c2b6995a 1502 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 45:2406c2b6995a 1503
arsenalist 45:2406c2b6995a 1504 if(str.rfind("[{\"from\"") != std::string::npos) {
arsenalist 45:2406c2b6995a 1505 str.erase(str.begin(),str.begin()+str.rfind("[{\"from\""));
arsenalist 45:2406c2b6995a 1506 if(str.find("[{") != std::string::npos && str.rfind("}]") != std::string::npos) {
arsenalist 45:2406c2b6995a 1507 str.erase(str.begin(),str.begin()+str.find("[{"));
arsenalist 45:2406c2b6995a 1508 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 45:2406c2b6995a 1509 }
arsenalist 45:2406c2b6995a 1510 //DBG.printf("newCommand:\r\n%s\r\n",str.c_str());
arsenalist 45:2406c2b6995a 1511 TFT.locate(0,160);
arsenalist 45:2406c2b6995a 1512 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1513 TFT.locate(0,160);
arsenalist 45:2406c2b6995a 1514 TFT.printf("newCommand");
arsenalist 45:2406c2b6995a 1515
arsenalist 45:2406c2b6995a 1516 MbedJSONValue jsonValue;
arsenalist 45:2406c2b6995a 1517 parse(jsonValue,str.c_str());
arsenalist 45:2406c2b6995a 1518 char *parameter[5] = {"from","nType","nAddr","dType","cmd"};
arsenalist 45:2406c2b6995a 1519
arsenalist 45:2406c2b6995a 1520 DBG.printf("get %d command\r\n",jsonValue.size());
arsenalist 45:2406c2b6995a 1521
arsenalist 45:2406c2b6995a 1522 //processing each command
arsenalist 45:2406c2b6995a 1523 bool validCommand;
arsenalist 45:2406c2b6995a 1524 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 45:2406c2b6995a 1525 //check whether command is valid
arsenalist 45:2406c2b6995a 1526 DBG.printf("\r\nprocessing cmd[%d]\r\n",i);
arsenalist 45:2406c2b6995a 1527 validCommand = true;
arsenalist 45:2406c2b6995a 1528 for(int j=0; j<5; j++) {
arsenalist 45:2406c2b6995a 1529 validCommand = validCommand && jsonValue[i].hasMember(parameter[j]);
arsenalist 45:2406c2b6995a 1530 }
arsenalist 45:2406c2b6995a 1531 DBG.printf("command validity:%d\r\n",validCommand);
arsenalist 45:2406c2b6995a 1532
arsenalist 45:2406c2b6995a 1533 if(validCommand) {
arsenalist 45:2406c2b6995a 1534 string commandFrom = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 45:2406c2b6995a 1535 string commandNType = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 45:2406c2b6995a 1536 string commandNAddr = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 45:2406c2b6995a 1537 string commandDType = jsonValue[i][parameter[3]].get<std::string>();
arsenalist 45:2406c2b6995a 1538 string commandCmd = jsonValue[i][parameter[4]].get<std::string>();
arsenalist 45:2406c2b6995a 1539
arsenalist 45:2406c2b6995a 1540 if(commandNType == "0") { //switch on panel controller
arsenalist 45:2406c2b6995a 1541 DBG.printf("command for switch\r\n");
arsenalist 45:2406c2b6995a 1542 }
arsenalist 46:86a0c2133130 1543 else if(commandNType == "1" || commandNType == "2") { //ir&rf remote control or wifi smart plug
arsenalist 45:2406c2b6995a 1544 DBG.printf("command for remote control\r\n");
arsenalist 45:2406c2b6995a 1545
arsenalist 45:2406c2b6995a 1546 //get index of node list based on mac address
arsenalist 45:2406c2b6995a 1547 int idx = NODES_INVALID;
arsenalist 45:2406c2b6995a 1548 for(int i=0; i<NODES_MAX; i++) {
arsenalist 45:2406c2b6995a 1549 if(!nodes[i].macAddr.compare(commandNAddr)) {
arsenalist 45:2406c2b6995a 1550 idx = i;
arsenalist 45:2406c2b6995a 1551 }
arsenalist 45:2406c2b6995a 1552 }
arsenalist 45:2406c2b6995a 1553
arsenalist 45:2406c2b6995a 1554 //execution process
arsenalist 45:2406c2b6995a 1555 string execResult = "failed";
arsenalist 45:2406c2b6995a 1556 if(idx != NODES_INVALID) {
arsenalist 45:2406c2b6995a 1557 DBG.printf("index found at %d\r\n",idx);
arsenalist 45:2406c2b6995a 1558
arsenalist 46:86a0c2133130 1559 if(commandNType == "1"){
arsenalist 46:86a0c2133130 1560 //get cmd string based on device type and command number
arsenalist 46:86a0c2133130 1561 string nodeCmd;
arsenalist 46:86a0c2133130 1562 nodeCmd = readNodeCmd(commandDType,commandCmd);
arsenalist 46:86a0c2133130 1563 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
arsenalist 46:86a0c2133130 1564 } else if(commandNType == "2") {
arsenalist 46:86a0c2133130 1565 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str());
arsenalist 46:86a0c2133130 1566 }
arsenalist 46:86a0c2133130 1567
arsenalist 45:2406c2b6995a 1568 //execute command
arsenalist 45:2406c2b6995a 1569 DBG.printf("executing command\r\n");
arsenalist 46:86a0c2133130 1570
arsenalist 45:2406c2b6995a 1571 trial=0;
arsenalist 45:2406c2b6995a 1572 while(1) {
arsenalist 45:2406c2b6995a 1573 if(trial>=2) { //two times trial
arsenalist 45:2406c2b6995a 1574 DBG.printf("cmd is not executed\r\n");
arsenalist 45:2406c2b6995a 1575 TFT.foreground(Red);
arsenalist 45:2406c2b6995a 1576 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1577 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1578 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1579 TFT.printf("cmd is not executed");
arsenalist 45:2406c2b6995a 1580 wait(1);
arsenalist 45:2406c2b6995a 1581 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1582 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1583 TFT.foreground(White);
arsenalist 45:2406c2b6995a 1584 break;
arsenalist 45:2406c2b6995a 1585 }
arsenalist 45:2406c2b6995a 1586 str = ethREST(nodes[idx].ipAddr,REMOTE_TCP_PORT,s);
arsenalist 45:2406c2b6995a 1587 wait(2);
arsenalist 45:2406c2b6995a 1588 if(str.find("200 OK") != std::string::npos) {
arsenalist 45:2406c2b6995a 1589 DBG.printf("cmd is executed\r\n");
arsenalist 45:2406c2b6995a 1590 TFT.foreground(Green);
arsenalist 45:2406c2b6995a 1591 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1592 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1593 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1594 TFT.printf("cmd is executed");
arsenalist 45:2406c2b6995a 1595 wait(1);
arsenalist 45:2406c2b6995a 1596 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1597 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1598 TFT.foreground(White);
arsenalist 45:2406c2b6995a 1599 execResult = "success";
arsenalist 45:2406c2b6995a 1600 break;
arsenalist 45:2406c2b6995a 1601 }
arsenalist 45:2406c2b6995a 1602 str.clear();
arsenalist 45:2406c2b6995a 1603 trial++;
arsenalist 45:2406c2b6995a 1604 }
arsenalist 45:2406c2b6995a 1605 } else {
arsenalist 45:2406c2b6995a 1606 TFT.foreground(Red);
arsenalist 45:2406c2b6995a 1607 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1608 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1609 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1610 TFT.printf("node is invalid");
arsenalist 45:2406c2b6995a 1611 wait(1);
arsenalist 45:2406c2b6995a 1612 TFT.locate(0,180);
arsenalist 45:2406c2b6995a 1613 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1614 TFT.foreground(White);
arsenalist 45:2406c2b6995a 1615 }
arsenalist 45:2406c2b6995a 1616 wait(2);
arsenalist 45:2406c2b6995a 1617
arsenalist 45:2406c2b6995a 1618 //send execution result
arsenalist 45:2406c2b6995a 1619 sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());
arsenalist 45:2406c2b6995a 1620 hmacCmd = calculateMD5(p);
arsenalist 45:2406c2b6995a 1621
arsenalist 45:2406c2b6995a 1622 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 45:2406c2b6995a 1623 emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str());
arsenalist 45:2406c2b6995a 1624
arsenalist 45:2406c2b6995a 1625 sprintf(s,"POST /%s/api/controller/result HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 50:50d98fe6cc15 1626 connPort = restPORT;
arsenalist 45:2406c2b6995a 1627
arsenalist 45:2406c2b6995a 1628 trial=0;
arsenalist 45:2406c2b6995a 1629 while(1) {
arsenalist 45:2406c2b6995a 1630 if(trial>=2) { //two times trial
arsenalist 45:2406c2b6995a 1631 DBG.printf("failed to send execution result\r\n");
arsenalist 45:2406c2b6995a 1632 break;
arsenalist 45:2406c2b6995a 1633 }
arsenalist 45:2406c2b6995a 1634 str = ethREST(restSERVER,connPort,s);
arsenalist 45:2406c2b6995a 1635 wait(2);
arsenalist 45:2406c2b6995a 1636 if(str.rfind("[{\"status\"") != std::string::npos) {
arsenalist 45:2406c2b6995a 1637 str.erase(str.begin(),str.begin()+str.rfind("[{\"status\""));
arsenalist 45:2406c2b6995a 1638 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 45:2406c2b6995a 1639 DBG.printf("success to send execution result\r\n");
arsenalist 45:2406c2b6995a 1640 break;
arsenalist 45:2406c2b6995a 1641 }
arsenalist 45:2406c2b6995a 1642 }
arsenalist 45:2406c2b6995a 1643 str.clear();
arsenalist 45:2406c2b6995a 1644 trial++;
arsenalist 45:2406c2b6995a 1645 }
arsenalist 45:2406c2b6995a 1646 }
arsenalist 45:2406c2b6995a 1647 }
arsenalist 45:2406c2b6995a 1648 }
arsenalist 45:2406c2b6995a 1649 //clear text on lcd
arsenalist 45:2406c2b6995a 1650 TFT.locate(0,160);
arsenalist 45:2406c2b6995a 1651 TFT.printf(" ");
arsenalist 45:2406c2b6995a 1652 }
arsenalist 40:77bd44f57ad3 1653 osDelay(5000);
arsenalist 10:1dc69e0ce7fd 1654 }
arsenalist 10:1dc69e0ce7fd 1655 } else if(wifiConnected) {
arsenalist 0:f4e449fa34d7 1656 DBG.printf("emmaModeOperation - wifi\r\n");
arsenalist 29:4d94a03d38e0 1657
arsenalist 44:c1d11c491237 1658 //new log and dbg indicator
arsenalist 29:4d94a03d38e0 1659 seconds = time(NULL);
arsenalist 29:4d94a03d38e0 1660 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1661 DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++WiFi++++++++++"));
arsenalist 36:ed696fd1b44e 1662 //DBG.printf("newDbg:%d\r\n",writeDbg(q,"++++++++++++++++++++"));
arsenalist 0:f4e449fa34d7 1663
arsenalist 0:f4e449fa34d7 1664 //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work
arsenalist 0:f4e449fa34d7 1665 //_ESP.printf("MODE=B");
arsenalist 0:f4e449fa34d7 1666 //wait(1);
arsenalist 0:f4e449fa34d7 1667 //while(!esp.ready());
arsenalist 0:f4e449fa34d7 1668
arsenalist 24:5d58515ba510 1669 /*
arsenalist 0:f4e449fa34d7 1670 DBG.printf("emma: setup mqtt client\r\n");
arsenalist 0:f4e449fa34d7 1671 sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
arsenalist 0:f4e449fa34d7 1672
arsenalist 38:e44f35f8129d 1673 if(mqtt.begin(mqttClientId, mqttKEY.c_str(), mqttSECRET.c_str(), 120, 1)) {
arsenalist 0:f4e449fa34d7 1674 mqtt.connectedCb.attach(&mqttConnected);
arsenalist 0:f4e449fa34d7 1675 mqtt.disconnectedCb.attach(&mqttDisconnected);
arsenalist 38:e44f35f8129d 1676 mqtt.connect(mqttSERVER,mqttPORT,false);
arsenalist 13:e8adfe305dbc 1677 DBG.printf("emma: success to setup mqtt\r\n");
arsenalist 13:e8adfe305dbc 1678 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1679 TFT.printf("emma: success to setup mqtt");
arsenalist 0:f4e449fa34d7 1680 }
arsenalist 0:f4e449fa34d7 1681 DBG.printf("emma: system started\r\n");
arsenalist 24:5d58515ba510 1682 */
arsenalist 0:f4e449fa34d7 1683
arsenalist 24:5d58515ba510 1684 //t.start();
arsenalist 24:5d58515ba510 1685 //while(t.read_ms() < 5000) {
arsenalist 24:5d58515ba510 1686 // esp.process();
arsenalist 24:5d58515ba510 1687 //}
arsenalist 24:5d58515ba510 1688 //t.stop();
arsenalist 24:5d58515ba510 1689 //t.reset();
arsenalist 0:f4e449fa34d7 1690
arsenalist 0:f4e449fa34d7 1691 //set ade7758 parameter
arsenalist 44:c1d11c491237 1692 //start set beforehand
arsenalist 44:c1d11c491237 1693 /*
arsenalist 0:f4e449fa34d7 1694 ADE.begin();
arsenalist 0:f4e449fa34d7 1695
arsenalist 0:f4e449fa34d7 1696 ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET);
arsenalist 0:f4e449fa34d7 1697
arsenalist 0:f4e449fa34d7 1698 ADE.write16bits(AWG, 0);
arsenalist 0:f4e449fa34d7 1699 ADE.write16bits(BWG, 0);
arsenalist 0:f4e449fa34d7 1700 ADE.write16bits(CWG, 0);
arsenalist 0:f4e449fa34d7 1701 ADE.write16bits(AVAG, 0);
arsenalist 0:f4e449fa34d7 1702 ADE.write16bits(BVAG, 0);
arsenalist 0:f4e449fa34d7 1703 ADE.write16bits(CVAG, 0);
arsenalist 44:c1d11c491237 1704 */
arsenalist 44:c1d11c491237 1705 //end start beforehand
arsenalist 0:f4e449fa34d7 1706
arsenalist 4:76ab12e2f8a3 1707 //init rest to server
arsenalist 50:50d98fe6cc15 1708 connPort = restPORT;
arsenalist 38:e44f35f8129d 1709 if(rest.begin(restSERVER.c_str(),connPort,false)) {
arsenalist 4:76ab12e2f8a3 1710 DBG.printf("rest to server is created\r\n");
arsenalist 13:e8adfe305dbc 1711 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1712 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1713 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1714 TFT.printf("rest to server is created");
arsenalist 4:76ab12e2f8a3 1715 } else {
arsenalist 4:76ab12e2f8a3 1716 DBG.printf("rest to server is NOT created\r\n");
arsenalist 13:e8adfe305dbc 1717 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1718 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1719 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1720 TFT.printf("rest to server is NOT created");
arsenalist 4:76ab12e2f8a3 1721 }
arsenalist 4:76ab12e2f8a3 1722
arsenalist 7:7e8c6ad3fd64 1723 //check firmware update
arsenalist 7:7e8c6ad3fd64 1724
arsenalist 7:7e8c6ad3fd64 1725 //execute last state of switches on board
arsenalist 7:7e8c6ad3fd64 1726
arsenalist 7:7e8c6ad3fd64 1727 //get list of nodes from server
arsenalist 40:77bd44f57ad3 1728 /*
arsenalist 40:77bd44f57ad3 1729 //working
arsenalist 7:7e8c6ad3fd64 1730 sprintf(s,"/emma/api/controller/remotes?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
arsenalist 7:7e8c6ad3fd64 1731 rest.get(s);
arsenalist 7:7e8c6ad3fd64 1732 for(int i=0; i<sizeof(s); i++) {
arsenalist 7:7e8c6ad3fd64 1733 s[i]=0; }
arsenalist 7:7e8c6ad3fd64 1734 rest.getResponse(s,sizeof(s));
arsenalist 7:7e8c6ad3fd64 1735 str = s;
arsenalist 7:7e8c6ad3fd64 1736 if(str.rfind("[{\"mac\"") != std::string::npos) {
arsenalist 7:7e8c6ad3fd64 1737 DBG.printf("get nodes from server\r\n");
arsenalist 7:7e8c6ad3fd64 1738 str.erase(str.begin(),str.begin()+str.rfind("[{\"mac\""));
arsenalist 7:7e8c6ad3fd64 1739 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 7:7e8c6ad3fd64 1740
arsenalist 7:7e8c6ad3fd64 1741 MbedJSONValue jsonValue;
arsenalist 7:7e8c6ad3fd64 1742 parse(jsonValue,str.c_str());
arsenalist 7:7e8c6ad3fd64 1743 char *parameter[2] = {"mac","ip"};
arsenalist 7:7e8c6ad3fd64 1744
arsenalist 13:e8adfe305dbc 1745 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1746 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1747 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1748 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 13:e8adfe305dbc 1749 wait(0.5);
arsenalist 13:e8adfe305dbc 1750 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1751 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1752
arsenalist 7:7e8c6ad3fd64 1753 //check whether nodes valid
arsenalist 7:7e8c6ad3fd64 1754 bool validNodes = true;
arsenalist 7:7e8c6ad3fd64 1755 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 7:7e8c6ad3fd64 1756 for(int j=0; j<2; j++) {
arsenalist 7:7e8c6ad3fd64 1757 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 7:7e8c6ad3fd64 1758 }
arsenalist 7:7e8c6ad3fd64 1759 }
arsenalist 7:7e8c6ad3fd64 1760 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 7:7e8c6ad3fd64 1761
arsenalist 7:7e8c6ad3fd64 1762 if(validNodes) {
arsenalist 7:7e8c6ad3fd64 1763 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 7:7e8c6ad3fd64 1764 string macValue = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 7:7e8c6ad3fd64 1765 string ipValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 7:7e8c6ad3fd64 1766 nodes[i].macAddr = macValue;
arsenalist 7:7e8c6ad3fd64 1767 nodes[i].ipAddr = ipValue;
arsenalist 13:e8adfe305dbc 1768 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 13:e8adfe305dbc 1769 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 7:7e8c6ad3fd64 1770 }
arsenalist 7:7e8c6ad3fd64 1771 }
arsenalist 7:7e8c6ad3fd64 1772
arsenalist 7:7e8c6ad3fd64 1773 } else {
arsenalist 7:7e8c6ad3fd64 1774 DBG.printf("no nodes from server\r\n");
arsenalist 7:7e8c6ad3fd64 1775 }
arsenalist 40:77bd44f57ad3 1776 */
arsenalist 40:77bd44f57ad3 1777
arsenalist 40:77bd44f57ad3 1778 _ESP.attach(&rxInterrupt,Serial::RxIrq);
arsenalist 40:77bd44f57ad3 1779
arsenalist 40:77bd44f57ad3 1780 //get list of nodes from server (post)
arsenalist 43:612547648ed1 1781 sprintf(p,"/%s/api/controller/wifinodes",appNAME.c_str());
arsenalist 40:77bd44f57ad3 1782 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmacTime.c_str());
arsenalist 40:77bd44f57ad3 1783 rest.post(p,s);
arsenalist 40:77bd44f57ad3 1784 wait(2);
arsenalist 40:77bd44f57ad3 1785 str = rxBuf;
arsenalist 40:77bd44f57ad3 1786 if(str.rfind("[{\"type\"") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1787 DBG.printf("get nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1788 str.erase(str.begin(),str.begin()+str.rfind("[{\"type\""));
arsenalist 40:77bd44f57ad3 1789 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 40:77bd44f57ad3 1790 //DBG.printf("strCrop:%s\r\n",str.c_str());
arsenalist 40:77bd44f57ad3 1791
arsenalist 40:77bd44f57ad3 1792 //start special handler
arsenalist 40:77bd44f57ad3 1793 while(1){
arsenalist 40:77bd44f57ad3 1794 if(str.find("}],") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1795 str.erase(str.begin()+str.find("}],")+1,str.begin()+str.find("}],")+2);
arsenalist 40:77bd44f57ad3 1796 } else {
arsenalist 40:77bd44f57ad3 1797 break;
arsenalist 40:77bd44f57ad3 1798 }
arsenalist 40:77bd44f57ad3 1799 }
arsenalist 40:77bd44f57ad3 1800 //end special handler
arsenalist 40:77bd44f57ad3 1801
arsenalist 40:77bd44f57ad3 1802 DBG.printf("strCrop:%s\r\n",str.c_str());
arsenalist 40:77bd44f57ad3 1803
arsenalist 40:77bd44f57ad3 1804 MbedJSONValue jsonValue;
arsenalist 40:77bd44f57ad3 1805 parse(jsonValue,str.c_str());
arsenalist 40:77bd44f57ad3 1806 char *parameter[3] = {"type","mac","ip"};
arsenalist 40:77bd44f57ad3 1807
arsenalist 40:77bd44f57ad3 1808 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1809 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1810 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1811 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 40:77bd44f57ad3 1812 wait(0.5);
arsenalist 40:77bd44f57ad3 1813 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1814 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1815
arsenalist 40:77bd44f57ad3 1816 //check whether nodes valid
arsenalist 40:77bd44f57ad3 1817 bool validNodes = true;
arsenalist 40:77bd44f57ad3 1818 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1819 for(int j=0; j<3; j++) {
arsenalist 40:77bd44f57ad3 1820 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 40:77bd44f57ad3 1821 }
arsenalist 40:77bd44f57ad3 1822 }
arsenalist 40:77bd44f57ad3 1823 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 40:77bd44f57ad3 1824
arsenalist 40:77bd44f57ad3 1825 if(validNodes) {
arsenalist 40:77bd44f57ad3 1826 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1827 int typeValue = jsonValue[i][parameter[0]].get<int>();
arsenalist 40:77bd44f57ad3 1828 string macValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 40:77bd44f57ad3 1829 string ipValue = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 40:77bd44f57ad3 1830 nodes[i].type = typeValue;
arsenalist 40:77bd44f57ad3 1831 nodes[i].macAddr = macValue;
arsenalist 40:77bd44f57ad3 1832 nodes[i].ipAddr = ipValue;
arsenalist 40:77bd44f57ad3 1833 DBG.printf("nodes[%d]type:%d\r\n",i,nodes[i].type);
arsenalist 40:77bd44f57ad3 1834 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 40:77bd44f57ad3 1835 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 40:77bd44f57ad3 1836 }
arsenalist 40:77bd44f57ad3 1837 }
arsenalist 40:77bd44f57ad3 1838
arsenalist 40:77bd44f57ad3 1839 } else {
arsenalist 40:77bd44f57ad3 1840 DBG.printf("no nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1841 }
arsenalist 7:7e8c6ad3fd64 1842
arsenalist 24:5d58515ba510 1843 /*
arsenalist 36:ed696fd1b44e 1844 //working
arsenalist 24:5d58515ba510 1845 DBG.printf("emma: setup mqtt client\r\n");
arsenalist 24:5d58515ba510 1846 sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
arsenalist 38:e44f35f8129d 1847 if(mqtt.begin(mqttClientId, mqttKEY.c_str(), mqttSECRET.c_str(), 120, 1)) {
arsenalist 24:5d58515ba510 1848 mqtt.connectedCb.attach(&mqttConnected);
arsenalist 24:5d58515ba510 1849 mqtt.disconnectedCb.attach(&mqttDisconnected);
arsenalist 38:e44f35f8129d 1850 mqtt.connect(mqttSERVER,mqttPORT,false);
arsenalist 24:5d58515ba510 1851 DBG.printf("emma: success to setup mqtt\r\n");
arsenalist 24:5d58515ba510 1852 TFT.locate(0,40);
arsenalist 24:5d58515ba510 1853 TFT.printf("emma: success to setup mqtt");
arsenalist 24:5d58515ba510 1854 }
arsenalist 24:5d58515ba510 1855 DBG.printf("emma: system started\r\n");
arsenalist 24:5d58515ba510 1856
arsenalist 24:5d58515ba510 1857 t.start();
arsenalist 24:5d58515ba510 1858 while(t.read_ms() < 5000) {
arsenalist 24:5d58515ba510 1859 esp.process();
arsenalist 24:5d58515ba510 1860 }
arsenalist 24:5d58515ba510 1861 t.stop();
arsenalist 24:5d58515ba510 1862 t.reset();
arsenalist 24:5d58515ba510 1863 */
arsenalist 7:7e8c6ad3fd64 1864
arsenalist 40:77bd44f57ad3 1865 //disable UART2
arsenalist 40:77bd44f57ad3 1866 NVIC_DisableIRQ(USART2_IRQn);
arsenalist 40:77bd44f57ad3 1867
arsenalist 4:76ab12e2f8a3 1868 //init rest to remotes
arsenalist 4:76ab12e2f8a3 1869 for(int i=0; i<NODES_MAX; i++) {
arsenalist 4:76ab12e2f8a3 1870 if(!nodes[i].ipAddr.empty()) {
arsenalist 4:76ab12e2f8a3 1871 DBG.printf("restConn nodes[%d] is created\r\n",i);
arsenalist 45:2406c2b6995a 1872 nodes[i].restConn->begin(nodes[i].ipAddr.c_str(),REMOTE_TCP_PORT,false);
arsenalist 4:76ab12e2f8a3 1873 wait(1);
arsenalist 4:76ab12e2f8a3 1874 } else {
arsenalist 4:76ab12e2f8a3 1875 DBG.printf("restConn nodes[%d] is NOT created\r\n",i);
arsenalist 4:76ab12e2f8a3 1876 wait(1);
arsenalist 4:76ab12e2f8a3 1877 }
arsenalist 4:76ab12e2f8a3 1878 }
arsenalist 4:76ab12e2f8a3 1879
arsenalist 40:77bd44f57ad3 1880 //enable UART2
arsenalist 40:77bd44f57ad3 1881 NVIC_EnableIRQ(USART2_IRQn);
arsenalist 40:77bd44f57ad3 1882
arsenalist 40:77bd44f57ad3 1883 //_ESP.attach(&rxInterrupt,Serial::RxIrq);
arsenalist 4:76ab12e2f8a3 1884
arsenalist 4:76ab12e2f8a3 1885 //define thread
arsenalist 17:b3ced0e67916 1886 osThreadDef(energyThread, osPriorityBelowNormal, (8*DEFAULT_STACK_SIZE));
arsenalist 0:f4e449fa34d7 1887 //create thread
arsenalist 2:fdfdeb5fd6fd 1888 osThreadCreate(osThread(energyThread),NULL);
arsenalist 0:f4e449fa34d7 1889
arsenalist 47:97eadcb478b2 1890 tAlert.start();
arsenalist 24:5d58515ba510 1891 tPanelEnergy.start();
arsenalist 24:5d58515ba510 1892 tPanel.start();
arsenalist 4:76ab12e2f8a3 1893 tNodes.start();
arsenalist 4:76ab12e2f8a3 1894 wait(1);
arsenalist 0:f4e449fa34d7 1895 while(1) {
arsenalist 4:76ab12e2f8a3 1896 checkRxBuffer();
arsenalist 20:ea14f175bbb4 1897 checkVoltagePower();
arsenalist 20:ea14f175bbb4 1898
arsenalist 24:5d58515ba510 1899 //whether espFreeMemory occurs
arsenalist 24:5d58515ba510 1900 if(espFreeMemory) {
arsenalist 30:08d290158fa7 1901 //logging
arsenalist 27:562a95c120cc 1902 seconds = time(NULL);
arsenalist 27:562a95c120cc 1903 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1904 DBG.printf("logFreeMemory:%d\r\n",writeLog(q,rxLog.c_str()));
arsenalist 24:5d58515ba510 1905
arsenalist 27:562a95c120cc 1906 espFreeMemory = false;
arsenalist 27:562a95c120cc 1907 }
arsenalist 27:562a95c120cc 1908
arsenalist 27:562a95c120cc 1909 //whether espDHCPClientStart occurs
arsenalist 27:562a95c120cc 1910 if(espDHCPClientStart) {
arsenalist 30:08d290158fa7 1911 //logging
arsenalist 27:562a95c120cc 1912 seconds = time(NULL);
arsenalist 27:562a95c120cc 1913 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1914 DBG.printf("logDHCPClientStart:%d\r\n",writeLog(q,rxLogA.c_str()));
arsenalist 27:562a95c120cc 1915
arsenalist 27:562a95c120cc 1916 _ESP.printf("MODE=B");
arsenalist 27:562a95c120cc 1917 wait(2);
arsenalist 27:562a95c120cc 1918 espDHCPClientStart = false;
arsenalist 24:5d58515ba510 1919 }
arsenalist 24:5d58515ba510 1920
arsenalist 47:97eadcb478b2 1921 //set allowAlertXxxx to enable controller send alert
arsenalist 47:97eadcb478b2 1922 if(tAlert.read() > 300.0f) { //300.0f is 5 minutes
arsenalist 47:97eadcb478b2 1923 allowAlertAVrms = true;
arsenalist 47:97eadcb478b2 1924 allowAlertAWatt = true;
arsenalist 47:97eadcb478b2 1925 allowAlertBVrms = true;
arsenalist 47:97eadcb478b2 1926 allowAlertBWatt = true;
arsenalist 47:97eadcb478b2 1927 allowAlertCVrms = true;
arsenalist 47:97eadcb478b2 1928 allowAlertCWatt = true;
arsenalist 47:97eadcb478b2 1929 tAlert.reset();
arsenalist 47:97eadcb478b2 1930 }
arsenalist 47:97eadcb478b2 1931
arsenalist 4:76ab12e2f8a3 1932 //panelEnergy, panelVoltage, and panelPower
arsenalist 24:5d58515ba510 1933 if(tPanelEnergy.read() > 30.0f) {
arsenalist 4:76ab12e2f8a3 1934 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
arsenalist 13:e8adfe305dbc 1935 TFT.locate(0,60);
arsenalist 13:e8adfe305dbc 1936 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1937 TFT.locate(0,60);
arsenalist 13:e8adfe305dbc 1938 TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
arsenalist 13:e8adfe305dbc 1939
arsenalist 4:76ab12e2f8a3 1940 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
arsenalist 13:e8adfe305dbc 1941 TFT.locate(0,80);
arsenalist 13:e8adfe305dbc 1942 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1943 TFT.locate(0,80);
arsenalist 13:e8adfe305dbc 1944 TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
arsenalist 13:e8adfe305dbc 1945
arsenalist 4:76ab12e2f8a3 1946 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
arsenalist 13:e8adfe305dbc 1947 TFT.locate(0,100);
arsenalist 13:e8adfe305dbc 1948 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1949 TFT.locate(0,100);
arsenalist 13:e8adfe305dbc 1950 TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
arsenalist 4:76ab12e2f8a3 1951
arsenalist 22:e18d361bf688 1952 if(newEnergyData) {
arsenalist 22:e18d361bf688 1953 //for(int i=1; i<4; i++) {
arsenalist 49:d11a1914ee8b 1954 for(int i=1; i<(powerPHASE+1); i++) {
arsenalist 22:e18d361bf688 1955 DBG.printf("sending channel: %d\r\n",i);
arsenalist 22:e18d361bf688 1956 if(i==1){
arsenalist 22:e18d361bf688 1957 XWattHr = AWattHr;
arsenalist 22:e18d361bf688 1958 XVrms = AVrms;
arsenalist 22:e18d361bf688 1959 XWatt = AWatt;
arsenalist 22:e18d361bf688 1960 } else if(i==2) {
arsenalist 22:e18d361bf688 1961 XWattHr = BWattHr;
arsenalist 22:e18d361bf688 1962 XVrms = BVrms;
arsenalist 22:e18d361bf688 1963 XWatt = BWatt;
arsenalist 22:e18d361bf688 1964 } else {
arsenalist 22:e18d361bf688 1965 XWattHr = CWattHr;
arsenalist 22:e18d361bf688 1966 XVrms = CVrms;
arsenalist 22:e18d361bf688 1967 XWatt = CWatt;
arsenalist 22:e18d361bf688 1968 }
arsenalist 24:5d58515ba510 1969
arsenalist 24:5d58515ba510 1970 if(XWattHr != 0.0f) {
arsenalist 22:e18d361bf688 1971
arsenalist 43:612547648ed1 1972 sprintf(r,"/%s/api/controller/energy/%d",appNAME.c_str(),i);
arsenalist 24:5d58515ba510 1973 seconds = time(NULL);
arsenalist 24:5d58515ba510 1974 //for(int j=0; j<sizeof(q); j++) {
arsenalist 24:5d58515ba510 1975 // q[j]=0; }
arsenalist 24:5d58515ba510 1976 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 24:5d58515ba510 1977
arsenalist 24:5d58515ba510 1978 //calculate hmacTime
arsenalist 24:5d58515ba510 1979 for(int j=0; j<sizeof(p); j++) {
arsenalist 24:5d58515ba510 1980 p[j]=0; }
arsenalist 24:5d58515ba510 1981 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 24:5d58515ba510 1982 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 1983
arsenalist 24:5d58515ba510 1984 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 24:5d58515ba510 1985 emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
arsenalist 24:5d58515ba510 1986 //DBG.printf("dataEnergy:\r\n%s\r\n",s);
arsenalist 24:5d58515ba510 1987 rest.post(r,s);
arsenalist 24:5d58515ba510 1988 wait(2);
arsenalist 24:5d58515ba510 1989 if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 27:562a95c120cc 1990 //logging purpose
arsenalist 27:562a95c120cc 1991 seconds = time(NULL);
arsenalist 27:562a95c120cc 1992 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1993 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData success"));
arsenalist 27:562a95c120cc 1994
arsenalist 24:5d58515ba510 1995 DBG.printf("send channel: %d success\r\n",i);
arsenalist 24:5d58515ba510 1996 TFT.foreground(Green);
arsenalist 24:5d58515ba510 1997 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1998 TFT.printf(" ");
arsenalist 24:5d58515ba510 1999 TFT.locate(0,120);
arsenalist 24:5d58515ba510 2000 TFT.printf("send ch%d success",i);
arsenalist 24:5d58515ba510 2001 wait(1);
arsenalist 24:5d58515ba510 2002 TFT.locate(0,120);
arsenalist 24:5d58515ba510 2003 TFT.printf(" ");
arsenalist 24:5d58515ba510 2004 TFT.foreground(White);
arsenalist 24:5d58515ba510 2005 } else {
arsenalist 27:562a95c120cc 2006 //logging purpose
arsenalist 27:562a95c120cc 2007 seconds = time(NULL);
arsenalist 27:562a95c120cc 2008 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 2009 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData failed"));
arsenalist 27:562a95c120cc 2010
arsenalist 24:5d58515ba510 2011 DBG.printf("send channel: %d failed\r\n",i);
arsenalist 24:5d58515ba510 2012 TFT.foreground(Red);
arsenalist 24:5d58515ba510 2013 TFT.locate(0,120);
arsenalist 24:5d58515ba510 2014 TFT.printf(" ");
arsenalist 24:5d58515ba510 2015 TFT.locate(0,120);
arsenalist 24:5d58515ba510 2016 TFT.printf("send ch%d failed",i);
arsenalist 24:5d58515ba510 2017 wait(1);
arsenalist 24:5d58515ba510 2018 TFT.locate(0,120);
arsenalist 24:5d58515ba510 2019 TFT.printf(" ");
arsenalist 24:5d58515ba510 2020 TFT.foreground(White);
arsenalist 24:5d58515ba510 2021 }
arsenalist 22:e18d361bf688 2022 }
arsenalist 4:76ab12e2f8a3 2023 }
arsenalist 22:e18d361bf688 2024
arsenalist 22:e18d361bf688 2025 newEnergyData = false;
arsenalist 0:f4e449fa34d7 2026 }
arsenalist 22:e18d361bf688 2027
arsenalist 24:5d58515ba510 2028 tPanelEnergy.reset();
arsenalist 4:76ab12e2f8a3 2029 loop++;
arsenalist 0:f4e449fa34d7 2030 }
arsenalist 0:f4e449fa34d7 2031
arsenalist 21:33bd8b82560f 2032 //panel environment
arsenalist 5:d00233dd36f5 2033 checkRxBuffer();
arsenalist 47:97eadcb478b2 2034 if(tPanel.read() > 900.0f) { //900.0f is 15 minutes
arsenalist 21:33bd8b82560f 2035 int dTemp=0;
arsenalist 21:33bd8b82560f 2036 int dHum=0;
arsenalist 21:33bd8b82560f 2037 int dGas=0;
arsenalist 21:33bd8b82560f 2038
arsenalist 21:33bd8b82560f 2039 DBG.printf("getPanelEnvironment\r\n");
arsenalist 21:33bd8b82560f 2040
arsenalist 21:33bd8b82560f 2041 //get environment sensor
arsenalist 21:33bd8b82560f 2042 trial=0;
arsenalist 21:33bd8b82560f 2043 while(1) {
arsenalist 21:33bd8b82560f 2044 if(trial>=2) { //two times trial
arsenalist 21:33bd8b82560f 2045 break;
arsenalist 21:33bd8b82560f 2046 }
arsenalist 21:33bd8b82560f 2047 if(d.readData() == DHT11::OK) {
arsenalist 21:33bd8b82560f 2048 dTemp = d.readTemperature();
arsenalist 21:33bd8b82560f 2049 dHum = d.readHumidity();
arsenalist 21:33bd8b82560f 2050 break;
arsenalist 21:33bd8b82560f 2051 }
arsenalist 21:33bd8b82560f 2052 trial++;
arsenalist 21:33bd8b82560f 2053 wait(3);
arsenalist 21:33bd8b82560f 2054 }
arsenalist 21:33bd8b82560f 2055
arsenalist 21:33bd8b82560f 2056 //send environment sensor
arsenalist 24:5d58515ba510 2057 if(dTemp!=0 && dHum!=0) {
arsenalist 24:5d58515ba510 2058 seconds = time(NULL);
arsenalist 24:5d58515ba510 2059 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 22:e18d361bf688 2060
arsenalist 24:5d58515ba510 2061 //calculate hmacTime
arsenalist 24:5d58515ba510 2062 for(int j=0; j<sizeof(p); j++) {
arsenalist 24:5d58515ba510 2063 p[j]=0; }
arsenalist 24:5d58515ba510 2064 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 24:5d58515ba510 2065 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 2066
arsenalist 24:5d58515ba510 2067 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
arsenalist 24:5d58515ba510 2068 emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas);
arsenalist 24:5d58515ba510 2069 //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
arsenalist 43:612547648ed1 2070 sprintf(p,"/%s/api/controller/environment",appNAME.c_str());
arsenalist 43:612547648ed1 2071 rest.post(p,s);
arsenalist 43:612547648ed1 2072 //rest.post("/emma/api/controller/environment",s); //working
arsenalist 24:5d58515ba510 2073 wait(2);
arsenalist 24:5d58515ba510 2074 str = rxBuf;
arsenalist 24:5d58515ba510 2075 if(str.rfind("/environment") != std::string::npos) {
arsenalist 24:5d58515ba510 2076 str.erase(str.begin(),str.begin()+str.rfind("/environment"));
arsenalist 24:5d58515ba510 2077 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 27:562a95c120cc 2078 //logging purpose
arsenalist 27:562a95c120cc 2079 seconds = time(NULL);
arsenalist 27:562a95c120cc 2080 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 2081 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv success"));
arsenalist 24:5d58515ba510 2082 DBG.printf("send panel environment success\r\n");
arsenalist 24:5d58515ba510 2083 } else {
arsenalist 27:562a95c120cc 2084 //logging purpose
arsenalist 27:562a95c120cc 2085 seconds = time(NULL);
arsenalist 27:562a95c120cc 2086 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 2087 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv failed"));
arsenalist 24:5d58515ba510 2088 DBG.printf("send panel environment failed\r\n");
arsenalist 24:5d58515ba510 2089 }
arsenalist 21:33bd8b82560f 2090 }
arsenalist 24:5d58515ba510 2091 checkRxBuffer();
arsenalist 21:33bd8b82560f 2092 }
arsenalist 21:33bd8b82560f 2093 tPanel.reset();
arsenalist 21:33bd8b82560f 2094 }
arsenalist 21:33bd8b82560f 2095
arsenalist 0:f4e449fa34d7 2096 //nodeTemp
arsenalist 21:33bd8b82560f 2097 checkRxBuffer();
arsenalist 25:36c6a5db50ed 2098 if(tNodes.read() > 900.0f) { //900 is 15 minutes
arsenalist 5:d00233dd36f5 2099 DBG.printf("getNodesTemperature\r\n");
arsenalist 5:d00233dd36f5 2100
arsenalist 5:d00233dd36f5 2101 for(int i=0; i<NODES_MAX; i++) {
arsenalist 49:d11a1914ee8b 2102 if(!nodes[i].ipAddr.empty() && nodes[i].type == 1) {
arsenalist 5:d00233dd36f5 2103 //get node's temp
arsenalist 5:d00233dd36f5 2104 string temp;
arsenalist 5:d00233dd36f5 2105 nodes[i].restConn->get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
arsenalist 5:d00233dd36f5 2106 wait(2);
arsenalist 5:d00233dd36f5 2107 temp = rxBuf;
arsenalist 5:d00233dd36f5 2108 if(temp.rfind(nodes[i].ipAddr) != std::string::npos) {
arsenalist 5:d00233dd36f5 2109 temp.erase(temp.begin(),temp.begin()+temp.rfind(nodes[i].ipAddr));
arsenalist 5:d00233dd36f5 2110 if(temp.rfind("temp=") != std::string::npos) {
arsenalist 5:d00233dd36f5 2111 temp.erase(temp.begin(),temp.begin()+temp.rfind("temp=")+6);
arsenalist 5:d00233dd36f5 2112 temp.erase(temp.begin()+temp.find("\""),temp.end());
arsenalist 5:d00233dd36f5 2113 } else {
arsenalist 5:d00233dd36f5 2114 temp = "0"; //connect to node, but receive none
arsenalist 5:d00233dd36f5 2115 }
arsenalist 5:d00233dd36f5 2116 } else {
arsenalist 5:d00233dd36f5 2117 temp = "0"; //not connected to node
arsenalist 5:d00233dd36f5 2118 }
arsenalist 0:f4e449fa34d7 2119
arsenalist 5:d00233dd36f5 2120 DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
arsenalist 5:d00233dd36f5 2121
arsenalist 5:d00233dd36f5 2122 //send node's temp
arsenalist 25:36c6a5db50ed 2123 if(temp != "0") {
arsenalist 25:36c6a5db50ed 2124 seconds = time(NULL);
arsenalist 25:36c6a5db50ed 2125 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 22:e18d361bf688 2126
arsenalist 25:36c6a5db50ed 2127 //calculate hmacTime
arsenalist 25:36c6a5db50ed 2128 for(int j=0; j<sizeof(p); j++) {
arsenalist 25:36c6a5db50ed 2129 p[j]=0; }
arsenalist 25:36c6a5db50ed 2130 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 25:36c6a5db50ed 2131 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 2132
arsenalist 25:36c6a5db50ed 2133 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":\"%s\",\"value\":%s}",
arsenalist 25:36c6a5db50ed 2134 emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
arsenalist 25:36c6a5db50ed 2135 //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
arsenalist 43:612547648ed1 2136 sprintf(p,"/%s/api/controller/nodetemp",appNAME.c_str());
arsenalist 43:612547648ed1 2137 rest.post(p,s);
arsenalist 43:612547648ed1 2138 //rest.post("/emma/api/controller/nodetemp",s); //working
arsenalist 25:36c6a5db50ed 2139 wait(2);
arsenalist 25:36c6a5db50ed 2140 str = rxBuf;
arsenalist 25:36c6a5db50ed 2141 if(str.rfind("/nodetemp") != std::string::npos) {
arsenalist 25:36c6a5db50ed 2142 str.erase(str.begin(),str.begin()+str.rfind("/nodetemp"));
arsenalist 25:36c6a5db50ed 2143 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 25:36c6a5db50ed 2144 DBG.printf("send nodeTemp success\r\n");
arsenalist 25:36c6a5db50ed 2145 } else {
arsenalist 25:36c6a5db50ed 2146 DBG.printf("send nodeTemp failed\r\n");
arsenalist 25:36c6a5db50ed 2147 }
arsenalist 5:d00233dd36f5 2148 }
arsenalist 25:36c6a5db50ed 2149 checkRxBuffer();
arsenalist 5:d00233dd36f5 2150 }
arsenalist 5:d00233dd36f5 2151 }
arsenalist 0:f4e449fa34d7 2152 }
arsenalist 19:7e3e9332f719 2153 tNodes.reset();
arsenalist 19:7e3e9332f719 2154 }
arsenalist 21:33bd8b82560f 2155
arsenalist 21:33bd8b82560f 2156 //command
arsenalist 43:612547648ed1 2157 sprintf(r,"/%s/api/controller/command",appNAME.c_str());
arsenalist 40:77bd44f57ad3 2158 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 44:c1d11c491237 2159 emmaUID.c_str(),hmacTime.c_str()); //INI HARUSNYA HMAC BUKAN?
arsenalist 40:77bd44f57ad3 2160 //rest.get("/emma/api/controller/command");
arsenalist 40:77bd44f57ad3 2161 rest.post(r,s);
arsenalist 40:77bd44f57ad3 2162
arsenalist 5:d00233dd36f5 2163 checkRxBuffer();
arsenalist 0:f4e449fa34d7 2164 if(newCommand) {
arsenalist 42:b32e51a374c7 2165 //DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
arsenalist 13:e8adfe305dbc 2166 TFT.locate(0,160);
arsenalist 13:e8adfe305dbc 2167 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 2168 TFT.locate(0,160);
arsenalist 13:e8adfe305dbc 2169 TFT.printf("newCommand");
arsenalist 13:e8adfe305dbc 2170
arsenalist 0:f4e449fa34d7 2171 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 2172 parse(jsonValue,globalCommand.c_str());
arsenalist 40:77bd44f57ad3 2173 char *parameter[5] = {"from","nType","nAddr","dType","cmd"};
arsenalist 0:f4e449fa34d7 2174
arsenalist 41:2d023bd80a9c 2175 DBG.printf("get %d command\r\n",jsonValue.size());
arsenalist 41:2d023bd80a9c 2176
arsenalist 41:2d023bd80a9c 2177 //processing each command
arsenalist 41:2d023bd80a9c 2178 bool validCommand;
arsenalist 41:2d023bd80a9c 2179 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 41:2d023bd80a9c 2180 //check whether command is valid
arsenalist 41:2d023bd80a9c 2181 DBG.printf("\r\nprocessing cmd[%d]\r\n",i);
arsenalist 41:2d023bd80a9c 2182 validCommand = true;
arsenalist 41:2d023bd80a9c 2183 for(int j=0; j<5; j++) {
arsenalist 41:2d023bd80a9c 2184 validCommand = validCommand && jsonValue[i].hasMember(parameter[j]);
arsenalist 41:2d023bd80a9c 2185 }
arsenalist 41:2d023bd80a9c 2186 DBG.printf("command validity:%d\r\n",validCommand);
arsenalist 41:2d023bd80a9c 2187
arsenalist 41:2d023bd80a9c 2188 if(validCommand) {
arsenalist 41:2d023bd80a9c 2189 string commandFrom = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 41:2d023bd80a9c 2190 string commandNType = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 41:2d023bd80a9c 2191 string commandNAddr = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 41:2d023bd80a9c 2192 string commandDType = jsonValue[i][parameter[3]].get<std::string>();
arsenalist 41:2d023bd80a9c 2193 string commandCmd = jsonValue[i][parameter[4]].get<std::string>();
arsenalist 41:2d023bd80a9c 2194
arsenalist 41:2d023bd80a9c 2195 if(commandNType == "0") { //switch on panel controller
arsenalist 41:2d023bd80a9c 2196 DBG.printf("command for switch\r\n");
arsenalist 41:2d023bd80a9c 2197 }
arsenalist 50:50d98fe6cc15 2198 else if(commandNType == "1" || commandNType == "2") { //ir&rf remote control or wifi smart plug
arsenalist 42:b32e51a374c7 2199 DBG.printf("command for remote control\r\n");
arsenalist 41:2d023bd80a9c 2200 //get node ip address based on node mac address
arsenalist 41:2d023bd80a9c 2201 //string nodeIP;
arsenalist 41:2d023bd80a9c 2202 //nodeIp = readNodeIP(commandNAddr);
arsenalist 41:2d023bd80a9c 2203 //nodeIP = "192.168.2.15";
arsenalist 41:2d023bd80a9c 2204 //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
arsenalist 41:2d023bd80a9c 2205
arsenalist 41:2d023bd80a9c 2206 //get index of node list based on mac address
arsenalist 41:2d023bd80a9c 2207 int idx = NODES_INVALID;
arsenalist 41:2d023bd80a9c 2208 for(int i=0; i<NODES_MAX; i++) {
arsenalist 41:2d023bd80a9c 2209 if(!nodes[i].macAddr.compare(commandNAddr)) {
arsenalist 41:2d023bd80a9c 2210 idx = i;
arsenalist 41:2d023bd80a9c 2211 }
arsenalist 41:2d023bd80a9c 2212 }
arsenalist 41:2d023bd80a9c 2213
arsenalist 41:2d023bd80a9c 2214 //execution process
arsenalist 41:2d023bd80a9c 2215 string execResult = "failed";
arsenalist 41:2d023bd80a9c 2216 if(idx != NODES_INVALID) {
arsenalist 41:2d023bd80a9c 2217 DBG.printf("index found at %d\r\n",idx);
arsenalist 41:2d023bd80a9c 2218
arsenalist 50:50d98fe6cc15 2219 if(commandNType == "1") {
arsenalist 50:50d98fe6cc15 2220 //get cmd string based on device type and command number
arsenalist 50:50d98fe6cc15 2221 string nodeCmd;
arsenalist 50:50d98fe6cc15 2222 nodeCmd = readNodeCmd(commandDType,commandCmd);
arsenalist 50:50d98fe6cc15 2223 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
arsenalist 50:50d98fe6cc15 2224 } else if(commandNType == "2") {
arsenalist 50:50d98fe6cc15 2225 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str());
arsenalist 50:50d98fe6cc15 2226 }
arsenalist 50:50d98fe6cc15 2227
arsenalist 41:2d023bd80a9c 2228 //execute command
arsenalist 41:2d023bd80a9c 2229 DBG.printf("executing command\r\n");
arsenalist 41:2d023bd80a9c 2230
arsenalist 41:2d023bd80a9c 2231 trial=0;
arsenalist 41:2d023bd80a9c 2232 while(1) {
arsenalist 41:2d023bd80a9c 2233 rxBuf.clear();
arsenalist 41:2d023bd80a9c 2234 if(trial>=2) { //two times trial
arsenalist 41:2d023bd80a9c 2235 DBG.printf("cmd is not executed\r\n");
arsenalist 41:2d023bd80a9c 2236 TFT.foreground(Red);
arsenalist 41:2d023bd80a9c 2237 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2238 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2239 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2240 TFT.printf("cmd is not executed");
arsenalist 41:2d023bd80a9c 2241 wait(1);
arsenalist 41:2d023bd80a9c 2242 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2243 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2244 TFT.foreground(White);
arsenalist 41:2d023bd80a9c 2245 break;
arsenalist 41:2d023bd80a9c 2246 }
arsenalist 41:2d023bd80a9c 2247 nodes[idx].restConn->get("/",s);
arsenalist 41:2d023bd80a9c 2248 wait(2);
arsenalist 41:2d023bd80a9c 2249 if(rxBuf.find("REST: status = 200") != std::string::npos) {
arsenalist 41:2d023bd80a9c 2250 DBG.printf("cmd is executed\r\n");
arsenalist 41:2d023bd80a9c 2251 TFT.foreground(Green);
arsenalist 41:2d023bd80a9c 2252 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2253 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2254 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2255 TFT.printf("cmd is executed");
arsenalist 41:2d023bd80a9c 2256 wait(1);
arsenalist 41:2d023bd80a9c 2257 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2258 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2259 TFT.foreground(White);
arsenalist 41:2d023bd80a9c 2260 execResult = "success";
arsenalist 41:2d023bd80a9c 2261 break;
arsenalist 41:2d023bd80a9c 2262 }
arsenalist 41:2d023bd80a9c 2263 trial++;
arsenalist 41:2d023bd80a9c 2264 }
arsenalist 41:2d023bd80a9c 2265 } else {
arsenalist 41:2d023bd80a9c 2266 TFT.foreground(Red);
arsenalist 41:2d023bd80a9c 2267 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2268 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2269 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2270 TFT.printf("node is invalid");
arsenalist 41:2d023bd80a9c 2271 wait(1);
arsenalist 41:2d023bd80a9c 2272 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 2273 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2274 TFT.foreground(White);
arsenalist 41:2d023bd80a9c 2275 }
arsenalist 41:2d023bd80a9c 2276
arsenalist 41:2d023bd80a9c 2277 wait(2);
arsenalist 41:2d023bd80a9c 2278 //send execution result
arsenalist 41:2d023bd80a9c 2279 sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());
arsenalist 41:2d023bd80a9c 2280 hmacCmd = calculateMD5(p);
arsenalist 41:2d023bd80a9c 2281
arsenalist 41:2d023bd80a9c 2282 sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 41:2d023bd80a9c 2283 emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str());
arsenalist 41:2d023bd80a9c 2284
arsenalist 41:2d023bd80a9c 2285 trial=0;
arsenalist 41:2d023bd80a9c 2286 while(1) {
arsenalist 41:2d023bd80a9c 2287 if(trial>=2) { //two times trial
arsenalist 41:2d023bd80a9c 2288 DBG.printf("failed to send execution result\r\n");
arsenalist 41:2d023bd80a9c 2289 break;
arsenalist 41:2d023bd80a9c 2290 }
arsenalist 43:612547648ed1 2291 sprintf(p,"/%s/api/controller/result",appNAME.c_str());
arsenalist 43:612547648ed1 2292 rest.post(p,s);
arsenalist 43:612547648ed1 2293 //rest.post("/emma/api/controller/result",s); //working
arsenalist 41:2d023bd80a9c 2294 wait(2);
arsenalist 41:2d023bd80a9c 2295 str = rxBuf;
arsenalist 41:2d023bd80a9c 2296 if(str.rfind("/result") != std::string::npos) {
arsenalist 41:2d023bd80a9c 2297 str.erase(str.begin(),str.begin()+str.rfind("/result"));
arsenalist 41:2d023bd80a9c 2298 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 41:2d023bd80a9c 2299 DBG.printf("success to send execution result\r\n");
arsenalist 41:2d023bd80a9c 2300 break;
arsenalist 41:2d023bd80a9c 2301 }
arsenalist 41:2d023bd80a9c 2302 }
arsenalist 42:b32e51a374c7 2303 //checkRxBuffer();
arsenalist 50:50d98fe6cc15 2304 rxBuf.clear();
arsenalist 42:b32e51a374c7 2305 trial++;
arsenalist 42:b32e51a374c7 2306 }
arsenalist 50:50d98fe6cc15 2307 }
arsenalist 41:2d023bd80a9c 2308 }
arsenalist 41:2d023bd80a9c 2309 }
arsenalist 41:2d023bd80a9c 2310 //clear text on lcd
arsenalist 41:2d023bd80a9c 2311 TFT.locate(0,160);
arsenalist 41:2d023bd80a9c 2312 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 2313
arsenalist 41:2d023bd80a9c 2314 newCommand = false;
arsenalist 2:fdfdeb5fd6fd 2315 }
arsenalist 30:08d290158fa7 2316
arsenalist 4:76ab12e2f8a3 2317 osDelay(5000);
arsenalist 0:f4e449fa34d7 2318 }
arsenalist 48:053e92b6dc1e 2319 } else if(gprsConnected) {
arsenalist 48:053e92b6dc1e 2320 DBG.printf("emmaModeOperation - gprs\r\n");
arsenalist 49:d11a1914ee8b 2321
arsenalist 49:d11a1914ee8b 2322 //new log indicator
arsenalist 49:d11a1914ee8b 2323 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2324 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2325 DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++GPRS++++++++++"));
arsenalist 49:d11a1914ee8b 2326
arsenalist 49:d11a1914ee8b 2327 //check firmware update
arsenalist 49:d11a1914ee8b 2328
arsenalist 49:d11a1914ee8b 2329 //execute last state of switches on board
arsenalist 49:d11a1914ee8b 2330
arsenalist 49:d11a1914ee8b 2331 //get list of nodes from server
arsenalist 49:d11a1914ee8b 2332 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 49:d11a1914ee8b 2333 sprintf(s,"POST /%s/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 50:50d98fe6cc15 2334 connPort = restPORT;
arsenalist 49:d11a1914ee8b 2335 str.clear();
arsenalist 49:d11a1914ee8b 2336 str = gprsREST(restSERVER,connPort,s);
arsenalist 49:d11a1914ee8b 2337 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 49:d11a1914ee8b 2338
arsenalist 49:d11a1914ee8b 2339 if(str.rfind("[{\"type\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2340 DBG.printf("get nodes from server\r\n");
arsenalist 49:d11a1914ee8b 2341 str.erase(str.begin(),str.begin()+str.rfind("[{\"type\""));
arsenalist 49:d11a1914ee8b 2342 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 49:d11a1914ee8b 2343
arsenalist 49:d11a1914ee8b 2344 MbedJSONValue jsonValue;
arsenalist 49:d11a1914ee8b 2345 parse(jsonValue,str.c_str());
arsenalist 49:d11a1914ee8b 2346 char *parameter[3] = {"type","mac","ip"};
arsenalist 49:d11a1914ee8b 2347
arsenalist 49:d11a1914ee8b 2348 TFT.locate(0,40);
arsenalist 49:d11a1914ee8b 2349 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2350 TFT.locate(0,40);
arsenalist 49:d11a1914ee8b 2351 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 49:d11a1914ee8b 2352 wait(0.5);
arsenalist 49:d11a1914ee8b 2353 TFT.locate(0,40);
arsenalist 49:d11a1914ee8b 2354 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2355
arsenalist 49:d11a1914ee8b 2356 //check whether nodes valid
arsenalist 49:d11a1914ee8b 2357 bool validNodes = true;
arsenalist 49:d11a1914ee8b 2358 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 49:d11a1914ee8b 2359 for(int j=0; j<3; j++) {
arsenalist 49:d11a1914ee8b 2360 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 49:d11a1914ee8b 2361 }
arsenalist 49:d11a1914ee8b 2362 }
arsenalist 49:d11a1914ee8b 2363 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 49:d11a1914ee8b 2364
arsenalist 49:d11a1914ee8b 2365 if(validNodes) {
arsenalist 49:d11a1914ee8b 2366 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 49:d11a1914ee8b 2367 int typeValue = jsonValue[i][parameter[0]].get<int>();
arsenalist 49:d11a1914ee8b 2368 string macValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 49:d11a1914ee8b 2369 string ipValue = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 49:d11a1914ee8b 2370 nodes[i].type = typeValue;
arsenalist 49:d11a1914ee8b 2371 nodes[i].macAddr = macValue;
arsenalist 49:d11a1914ee8b 2372 nodes[i].ipAddr = ipValue;
arsenalist 49:d11a1914ee8b 2373 DBG.printf("nodes[%d]type:%d\r\n",i,nodes[i].type);
arsenalist 49:d11a1914ee8b 2374 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 49:d11a1914ee8b 2375 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 49:d11a1914ee8b 2376 }
arsenalist 49:d11a1914ee8b 2377 }
arsenalist 49:d11a1914ee8b 2378
arsenalist 49:d11a1914ee8b 2379 } else {
arsenalist 49:d11a1914ee8b 2380 DBG.printf("no nodes from server\r\n");
arsenalist 49:d11a1914ee8b 2381 }
arsenalist 49:d11a1914ee8b 2382
arsenalist 49:d11a1914ee8b 2383 //create nodes connection via wifi (we assume that when using gprs, user should provide wifi for node control)
arsenalist 49:d11a1914ee8b 2384 //init rest to remotes
arsenalist 49:d11a1914ee8b 2385 for(int i=0; i<NODES_MAX; i++) {
arsenalist 49:d11a1914ee8b 2386 if(!nodes[i].ipAddr.empty()) {
arsenalist 49:d11a1914ee8b 2387 DBG.printf("restConn nodes[%d] is created\r\n",i);
arsenalist 49:d11a1914ee8b 2388 nodes[i].restConn->begin(nodes[i].ipAddr.c_str(),REMOTE_TCP_PORT,false);
arsenalist 49:d11a1914ee8b 2389 wait(1);
arsenalist 49:d11a1914ee8b 2390 } else {
arsenalist 49:d11a1914ee8b 2391 DBG.printf("restConn nodes[%d] is NOT created\r\n",i);
arsenalist 49:d11a1914ee8b 2392 wait(1);
arsenalist 49:d11a1914ee8b 2393 }
arsenalist 49:d11a1914ee8b 2394 }
arsenalist 49:d11a1914ee8b 2395
arsenalist 49:d11a1914ee8b 2396 _ESP.attach(&rxInterrupt,Serial::RxIrq);
arsenalist 49:d11a1914ee8b 2397
arsenalist 49:d11a1914ee8b 2398 //define thread
arsenalist 49:d11a1914ee8b 2399 osThreadDef(energyThread, osPriorityBelowNormal, (8*DEFAULT_STACK_SIZE));
arsenalist 49:d11a1914ee8b 2400 //create thread
arsenalist 49:d11a1914ee8b 2401 osThreadCreate(osThread(energyThread),NULL);
arsenalist 49:d11a1914ee8b 2402
arsenalist 49:d11a1914ee8b 2403 tAlert.start();
arsenalist 49:d11a1914ee8b 2404 tPanelEnergy.start();
arsenalist 49:d11a1914ee8b 2405 tPanel.start();
arsenalist 49:d11a1914ee8b 2406 tNodes.start();
arsenalist 49:d11a1914ee8b 2407 wait(1);
arsenalist 49:d11a1914ee8b 2408 while(1) {
arsenalist 50:50d98fe6cc15 2409 checkVoltagePower(); //need revision to support gprs
arsenalist 49:d11a1914ee8b 2410
arsenalist 51:ad6410dcaf52 2411 //set allowAlertXxxx to enable controller send alert
arsenalist 51:ad6410dcaf52 2412 if(tAlert.read() > 300.0f) { //300.0f is 5 minutes
arsenalist 51:ad6410dcaf52 2413 allowAlertAVrms = true;
arsenalist 51:ad6410dcaf52 2414 allowAlertAWatt = true;
arsenalist 51:ad6410dcaf52 2415 allowAlertBVrms = true;
arsenalist 51:ad6410dcaf52 2416 allowAlertBWatt = true;
arsenalist 51:ad6410dcaf52 2417 allowAlertCVrms = true;
arsenalist 51:ad6410dcaf52 2418 allowAlertCWatt = true;
arsenalist 51:ad6410dcaf52 2419 tAlert.reset();
arsenalist 51:ad6410dcaf52 2420 }
arsenalist 51:ad6410dcaf52 2421
arsenalist 49:d11a1914ee8b 2422 //panelEnergy, panelVoltage, and panelPower
arsenalist 49:d11a1914ee8b 2423 if(tPanelEnergy.read() > 30.0f) {
arsenalist 49:d11a1914ee8b 2424 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
arsenalist 49:d11a1914ee8b 2425 TFT.locate(0,60);
arsenalist 49:d11a1914ee8b 2426 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2427 TFT.locate(0,60);
arsenalist 49:d11a1914ee8b 2428 TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
arsenalist 49:d11a1914ee8b 2429
arsenalist 49:d11a1914ee8b 2430 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
arsenalist 49:d11a1914ee8b 2431 TFT.locate(0,80);
arsenalist 49:d11a1914ee8b 2432 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2433 TFT.locate(0,80);
arsenalist 49:d11a1914ee8b 2434 TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
arsenalist 49:d11a1914ee8b 2435
arsenalist 49:d11a1914ee8b 2436 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
arsenalist 49:d11a1914ee8b 2437 TFT.locate(0,100);
arsenalist 49:d11a1914ee8b 2438 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2439 TFT.locate(0,100);
arsenalist 49:d11a1914ee8b 2440 TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
arsenalist 49:d11a1914ee8b 2441
arsenalist 49:d11a1914ee8b 2442 if(newEnergyData) {
arsenalist 49:d11a1914ee8b 2443 for(int i=1; i<(powerPHASE+1); i++) {
arsenalist 49:d11a1914ee8b 2444 DBG.printf("sending channel: %d\r\n",i);
arsenalist 49:d11a1914ee8b 2445 if(i==1){
arsenalist 49:d11a1914ee8b 2446 XWattHr = AWattHr;
arsenalist 49:d11a1914ee8b 2447 XVrms = AVrms;
arsenalist 49:d11a1914ee8b 2448 XWatt = AWatt;
arsenalist 49:d11a1914ee8b 2449 } else if(i==2) {
arsenalist 49:d11a1914ee8b 2450 XWattHr = BWattHr;
arsenalist 49:d11a1914ee8b 2451 XVrms = BVrms;
arsenalist 49:d11a1914ee8b 2452 XWatt = BWatt;
arsenalist 49:d11a1914ee8b 2453 } else {
arsenalist 49:d11a1914ee8b 2454 XWattHr = CWattHr;
arsenalist 49:d11a1914ee8b 2455 XVrms = CVrms;
arsenalist 49:d11a1914ee8b 2456 XWatt = CWatt;
arsenalist 49:d11a1914ee8b 2457 }
arsenalist 49:d11a1914ee8b 2458
arsenalist 49:d11a1914ee8b 2459 if(XWattHr != 0.0f) {
arsenalist 49:d11a1914ee8b 2460 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2461 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2462
arsenalist 49:d11a1914ee8b 2463 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 49:d11a1914ee8b 2464 hmacTime = calculateMD5(p);
arsenalist 49:d11a1914ee8b 2465
arsenalist 49:d11a1914ee8b 2466 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 49:d11a1914ee8b 2467 emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
arsenalist 49:d11a1914ee8b 2468 sprintf(s,"POST /%s/api/controller/energy/%d HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),i,restSERVER.c_str(),connBodyLen,connBody);
arsenalist 49:d11a1914ee8b 2469
arsenalist 49:d11a1914ee8b 2470 //DBG.printf("dataEnergy:\r\n%s\r\n",connBody);
arsenalist 49:d11a1914ee8b 2471
arsenalist 50:50d98fe6cc15 2472 connPort = restPORT;
arsenalist 49:d11a1914ee8b 2473 str.clear();
arsenalist 49:d11a1914ee8b 2474 str = gprsREST(restSERVER,connPort,s);
arsenalist 49:d11a1914ee8b 2475 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 49:d11a1914ee8b 2476
arsenalist 49:d11a1914ee8b 2477 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2478 //logging purpose
arsenalist 49:d11a1914ee8b 2479 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2480 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2481 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData success"));
arsenalist 49:d11a1914ee8b 2482
arsenalist 49:d11a1914ee8b 2483 DBG.printf("send channel: %d success\r\n",i);
arsenalist 49:d11a1914ee8b 2484 TFT.foreground(Green);
arsenalist 49:d11a1914ee8b 2485 TFT.locate(0,120);
arsenalist 49:d11a1914ee8b 2486 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2487 TFT.locate(0,120);
arsenalist 49:d11a1914ee8b 2488 TFT.printf("send ch%d success",i);
arsenalist 49:d11a1914ee8b 2489 wait(1);
arsenalist 49:d11a1914ee8b 2490 TFT.locate(0,120);
arsenalist 49:d11a1914ee8b 2491 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2492 TFT.foreground(White);
arsenalist 49:d11a1914ee8b 2493 } else {
arsenalist 49:d11a1914ee8b 2494 //logging purpose
arsenalist 49:d11a1914ee8b 2495 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2496 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2497 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData failed"));
arsenalist 49:d11a1914ee8b 2498
arsenalist 49:d11a1914ee8b 2499 DBG.printf("send channel: %d failed\r\n",i);
arsenalist 49:d11a1914ee8b 2500 TFT.foreground(Red);
arsenalist 49:d11a1914ee8b 2501 TFT.locate(0,120);
arsenalist 49:d11a1914ee8b 2502 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2503 TFT.locate(0,120);
arsenalist 49:d11a1914ee8b 2504 TFT.printf("send ch%d failed",i);
arsenalist 49:d11a1914ee8b 2505 wait(1);
arsenalist 49:d11a1914ee8b 2506 TFT.locate(0,120);
arsenalist 49:d11a1914ee8b 2507 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2508 TFT.foreground(White);
arsenalist 49:d11a1914ee8b 2509 }
arsenalist 49:d11a1914ee8b 2510 }
arsenalist 49:d11a1914ee8b 2511 }
arsenalist 49:d11a1914ee8b 2512 newEnergyData = false;
arsenalist 49:d11a1914ee8b 2513 }
arsenalist 49:d11a1914ee8b 2514
arsenalist 49:d11a1914ee8b 2515 tPanelEnergy.reset();
arsenalist 49:d11a1914ee8b 2516 loop++;
arsenalist 49:d11a1914ee8b 2517 }
arsenalist 49:d11a1914ee8b 2518
arsenalist 49:d11a1914ee8b 2519 //panel environment
arsenalist 49:d11a1914ee8b 2520 if(tPanel.read() > 900.0f) { //900.0f is 15 minutes
arsenalist 49:d11a1914ee8b 2521 int dTemp=0;
arsenalist 49:d11a1914ee8b 2522 int dHum=0;
arsenalist 49:d11a1914ee8b 2523 int dGas=0;
arsenalist 49:d11a1914ee8b 2524
arsenalist 49:d11a1914ee8b 2525 DBG.printf("getPanelEnvironment\r\n");
arsenalist 49:d11a1914ee8b 2526
arsenalist 49:d11a1914ee8b 2527 //get environment sensor
arsenalist 49:d11a1914ee8b 2528 trial=0;
arsenalist 49:d11a1914ee8b 2529 while(1) {
arsenalist 49:d11a1914ee8b 2530 if(trial>=2) { //two times trial
arsenalist 49:d11a1914ee8b 2531 break;
arsenalist 49:d11a1914ee8b 2532 }
arsenalist 49:d11a1914ee8b 2533 if(d.readData() == DHT11::OK) {
arsenalist 49:d11a1914ee8b 2534 dTemp = d.readTemperature();
arsenalist 49:d11a1914ee8b 2535 dHum = d.readHumidity();
arsenalist 49:d11a1914ee8b 2536 break;
arsenalist 49:d11a1914ee8b 2537 }
arsenalist 49:d11a1914ee8b 2538 trial++;
arsenalist 49:d11a1914ee8b 2539 wait(3);
arsenalist 49:d11a1914ee8b 2540 }
arsenalist 49:d11a1914ee8b 2541
arsenalist 49:d11a1914ee8b 2542 //send environment sensor
arsenalist 49:d11a1914ee8b 2543 if(dTemp!=0 && dHum!=0) {
arsenalist 49:d11a1914ee8b 2544 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2545 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2546
arsenalist 49:d11a1914ee8b 2547 //calculate hmacTime
arsenalist 49:d11a1914ee8b 2548 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 49:d11a1914ee8b 2549 hmacTime = calculateMD5(p);
arsenalist 49:d11a1914ee8b 2550
arsenalist 49:d11a1914ee8b 2551 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
arsenalist 49:d11a1914ee8b 2552 emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas);
arsenalist 49:d11a1914ee8b 2553 sprintf(s,"POST /%s/api/controller/environment HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 49:d11a1914ee8b 2554
arsenalist 49:d11a1914ee8b 2555 //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
arsenalist 49:d11a1914ee8b 2556
arsenalist 50:50d98fe6cc15 2557 connPort = restPORT;
arsenalist 49:d11a1914ee8b 2558 str.clear();
arsenalist 49:d11a1914ee8b 2559 str = gprsREST(restSERVER,connPort,s);
arsenalist 49:d11a1914ee8b 2560 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 49:d11a1914ee8b 2561
arsenalist 49:d11a1914ee8b 2562 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2563 //logging purpose
arsenalist 49:d11a1914ee8b 2564 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2565 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2566 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv success"));
arsenalist 49:d11a1914ee8b 2567 DBG.printf("send panel environment success\r\n");
arsenalist 49:d11a1914ee8b 2568 } else {
arsenalist 49:d11a1914ee8b 2569 //logging purpose
arsenalist 49:d11a1914ee8b 2570 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2571 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2572 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv failed"));
arsenalist 49:d11a1914ee8b 2573 DBG.printf("send panel environment failed\r\n");
arsenalist 49:d11a1914ee8b 2574 }
arsenalist 49:d11a1914ee8b 2575 }
arsenalist 49:d11a1914ee8b 2576 tPanel.reset();
arsenalist 49:d11a1914ee8b 2577 }
arsenalist 49:d11a1914ee8b 2578
arsenalist 49:d11a1914ee8b 2579 //nodeTemp
arsenalist 49:d11a1914ee8b 2580 checkRxBuffer();
arsenalist 49:d11a1914ee8b 2581 if(tNodes.read() > 900.0f) { //900 is 15 minutes
arsenalist 49:d11a1914ee8b 2582 DBG.printf("getNodesTemperature\r\n");
arsenalist 49:d11a1914ee8b 2583
arsenalist 49:d11a1914ee8b 2584 for(int i=0; i<NODES_MAX; i++) {
arsenalist 49:d11a1914ee8b 2585 if(!nodes[i].ipAddr.empty() && nodes[i].type == 1) {
arsenalist 49:d11a1914ee8b 2586 //get node's temp
arsenalist 49:d11a1914ee8b 2587 string temp;
arsenalist 49:d11a1914ee8b 2588 nodes[i].restConn->get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
arsenalist 49:d11a1914ee8b 2589 wait(2);
arsenalist 49:d11a1914ee8b 2590 temp = rxBuf;
arsenalist 49:d11a1914ee8b 2591 if(temp.rfind(nodes[i].ipAddr) != std::string::npos) {
arsenalist 49:d11a1914ee8b 2592 temp.erase(temp.begin(),temp.begin()+temp.rfind(nodes[i].ipAddr));
arsenalist 49:d11a1914ee8b 2593 if(temp.rfind("temp=") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2594 temp.erase(temp.begin(),temp.begin()+temp.rfind("temp=")+6);
arsenalist 49:d11a1914ee8b 2595 temp.erase(temp.begin()+temp.find("\""),temp.end());
arsenalist 49:d11a1914ee8b 2596 } else {
arsenalist 49:d11a1914ee8b 2597 temp = "0"; //connect to node, but receive none
arsenalist 49:d11a1914ee8b 2598 }
arsenalist 49:d11a1914ee8b 2599 } else {
arsenalist 49:d11a1914ee8b 2600 temp = "0"; //not connected to node
arsenalist 49:d11a1914ee8b 2601 }
arsenalist 49:d11a1914ee8b 2602
arsenalist 49:d11a1914ee8b 2603 DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
arsenalist 49:d11a1914ee8b 2604
arsenalist 49:d11a1914ee8b 2605 //send node's temp
arsenalist 49:d11a1914ee8b 2606 if(temp != "0") {
arsenalist 49:d11a1914ee8b 2607 seconds = time(NULL);
arsenalist 49:d11a1914ee8b 2608 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 49:d11a1914ee8b 2609
arsenalist 49:d11a1914ee8b 2610 //calculate hmacTime
arsenalist 49:d11a1914ee8b 2611 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 49:d11a1914ee8b 2612 hmacTime = calculateMD5(p);
arsenalist 49:d11a1914ee8b 2613
arsenalist 49:d11a1914ee8b 2614 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":%s,\"value\":%s}",
arsenalist 49:d11a1914ee8b 2615 emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
arsenalist 49:d11a1914ee8b 2616 sprintf(s,"POST /%s/api/controller/nodetemp HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 49:d11a1914ee8b 2617
arsenalist 49:d11a1914ee8b 2618 //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
arsenalist 49:d11a1914ee8b 2619
arsenalist 50:50d98fe6cc15 2620 connPort = restPORT;
arsenalist 49:d11a1914ee8b 2621 str.clear();
arsenalist 49:d11a1914ee8b 2622 str = gprsREST(restSERVER,connPort,s);
arsenalist 49:d11a1914ee8b 2623 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 49:d11a1914ee8b 2624
arsenalist 49:d11a1914ee8b 2625 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2626 DBG.printf("send nodeTemp success\r\n");
arsenalist 49:d11a1914ee8b 2627 } else {
arsenalist 49:d11a1914ee8b 2628 DBG.printf("send nodeTemp failed\r\n");
arsenalist 49:d11a1914ee8b 2629 }
arsenalist 49:d11a1914ee8b 2630 }
arsenalist 49:d11a1914ee8b 2631 }
arsenalist 49:d11a1914ee8b 2632 }
arsenalist 49:d11a1914ee8b 2633 tNodes.reset();
arsenalist 49:d11a1914ee8b 2634 }
arsenalist 49:d11a1914ee8b 2635
arsenalist 49:d11a1914ee8b 2636 //command
arsenalist 49:d11a1914ee8b 2637 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 49:d11a1914ee8b 2638 sprintf(s,"POST /%s/api/controller/command HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 50:50d98fe6cc15 2639 connPort = restPORT;
arsenalist 49:d11a1914ee8b 2640 str = gprsREST(restSERVER,connPort,s);
arsenalist 49:d11a1914ee8b 2641 //DBG.printf("str:%s\r\n",str.c_str());
arsenalist 49:d11a1914ee8b 2642
arsenalist 49:d11a1914ee8b 2643 if(str.rfind("[{\"from\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2644 str.erase(str.begin(),str.begin()+str.rfind("[{\"from\""));
arsenalist 49:d11a1914ee8b 2645 if(str.find("[{") != std::string::npos && str.rfind("}]") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2646 str.erase(str.begin(),str.begin()+str.find("[{"));
arsenalist 49:d11a1914ee8b 2647 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 49:d11a1914ee8b 2648 }
arsenalist 49:d11a1914ee8b 2649 //DBG.printf("newCommand:\r\n%s\r\n",str.c_str());
arsenalist 49:d11a1914ee8b 2650 TFT.locate(0,160);
arsenalist 49:d11a1914ee8b 2651 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2652 TFT.locate(0,160);
arsenalist 49:d11a1914ee8b 2653 TFT.printf("newCommand");
arsenalist 49:d11a1914ee8b 2654
arsenalist 49:d11a1914ee8b 2655 MbedJSONValue jsonValue;
arsenalist 49:d11a1914ee8b 2656 parse(jsonValue,str.c_str());
arsenalist 49:d11a1914ee8b 2657 char *parameter[5] = {"from","nType","nAddr","dType","cmd"};
arsenalist 49:d11a1914ee8b 2658
arsenalist 49:d11a1914ee8b 2659 DBG.printf("get %d command\r\n",jsonValue.size());
arsenalist 49:d11a1914ee8b 2660
arsenalist 49:d11a1914ee8b 2661 //processing each command
arsenalist 49:d11a1914ee8b 2662 bool validCommand;
arsenalist 49:d11a1914ee8b 2663 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 49:d11a1914ee8b 2664 //check whether command is valid
arsenalist 49:d11a1914ee8b 2665 DBG.printf("\r\nprocessing cmd[%d]\r\n",i);
arsenalist 49:d11a1914ee8b 2666 validCommand = true;
arsenalist 49:d11a1914ee8b 2667 for(int j=0; j<5; j++) {
arsenalist 49:d11a1914ee8b 2668 validCommand = validCommand && jsonValue[i].hasMember(parameter[j]);
arsenalist 49:d11a1914ee8b 2669 }
arsenalist 49:d11a1914ee8b 2670 DBG.printf("command validity:%d\r\n",validCommand);
arsenalist 49:d11a1914ee8b 2671
arsenalist 49:d11a1914ee8b 2672 if(validCommand) {
arsenalist 49:d11a1914ee8b 2673 string commandFrom = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 49:d11a1914ee8b 2674 string commandNType = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 49:d11a1914ee8b 2675 string commandNAddr = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 49:d11a1914ee8b 2676 string commandDType = jsonValue[i][parameter[3]].get<std::string>();
arsenalist 49:d11a1914ee8b 2677 string commandCmd = jsonValue[i][parameter[4]].get<std::string>();
arsenalist 49:d11a1914ee8b 2678
arsenalist 49:d11a1914ee8b 2679 if(commandNType == "0") { //switch on panel controller
arsenalist 49:d11a1914ee8b 2680 DBG.printf("command for switch\r\n");
arsenalist 49:d11a1914ee8b 2681 }
arsenalist 49:d11a1914ee8b 2682 else if(commandNType == "1" || commandNType == "2") { //ir&rf remote control or wifi smart plug
arsenalist 49:d11a1914ee8b 2683 DBG.printf("command for remote control\r\n");
arsenalist 49:d11a1914ee8b 2684
arsenalist 49:d11a1914ee8b 2685 //get index of node list based on mac address
arsenalist 49:d11a1914ee8b 2686 int idx = NODES_INVALID;
arsenalist 49:d11a1914ee8b 2687 for(int i=0; i<NODES_MAX; i++) {
arsenalist 49:d11a1914ee8b 2688 if(!nodes[i].macAddr.compare(commandNAddr)) {
arsenalist 49:d11a1914ee8b 2689 idx = i;
arsenalist 49:d11a1914ee8b 2690 }
arsenalist 49:d11a1914ee8b 2691 }
arsenalist 49:d11a1914ee8b 2692
arsenalist 49:d11a1914ee8b 2693 //execution process
arsenalist 49:d11a1914ee8b 2694 string execResult = "failed";
arsenalist 49:d11a1914ee8b 2695 if(idx != NODES_INVALID) {
arsenalist 49:d11a1914ee8b 2696 DBG.printf("index found at %d\r\n",idx);
arsenalist 49:d11a1914ee8b 2697
arsenalist 49:d11a1914ee8b 2698 if(commandNType == "1"){
arsenalist 49:d11a1914ee8b 2699 //get cmd string based on device type and command number
arsenalist 49:d11a1914ee8b 2700 string nodeCmd;
arsenalist 49:d11a1914ee8b 2701 nodeCmd = readNodeCmd(commandDType,commandCmd);
arsenalist 49:d11a1914ee8b 2702 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
arsenalist 49:d11a1914ee8b 2703 } else if(commandNType == "2") {
arsenalist 49:d11a1914ee8b 2704 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str());
arsenalist 49:d11a1914ee8b 2705 }
arsenalist 49:d11a1914ee8b 2706
arsenalist 49:d11a1914ee8b 2707 //execute command
arsenalist 49:d11a1914ee8b 2708 DBG.printf("executing command\r\n");
arsenalist 49:d11a1914ee8b 2709
arsenalist 49:d11a1914ee8b 2710 trial=0;
arsenalist 49:d11a1914ee8b 2711 while(1) {
arsenalist 49:d11a1914ee8b 2712 rxBuf.clear();
arsenalist 49:d11a1914ee8b 2713 if(trial>=2) { //two times trial
arsenalist 49:d11a1914ee8b 2714 DBG.printf("cmd is not executed\r\n");
arsenalist 49:d11a1914ee8b 2715 TFT.foreground(Red);
arsenalist 49:d11a1914ee8b 2716 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2717 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2718 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2719 TFT.printf("cmd is not executed");
arsenalist 49:d11a1914ee8b 2720 wait(1);
arsenalist 49:d11a1914ee8b 2721 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2722 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2723 TFT.foreground(White);
arsenalist 49:d11a1914ee8b 2724 break;
arsenalist 49:d11a1914ee8b 2725 }
arsenalist 49:d11a1914ee8b 2726 nodes[idx].restConn->get("/",s);
arsenalist 49:d11a1914ee8b 2727 wait(2);
arsenalist 49:d11a1914ee8b 2728 if(rxBuf.find("REST: status = 200") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2729 DBG.printf("cmd is executed\r\n");
arsenalist 49:d11a1914ee8b 2730 TFT.foreground(Green);
arsenalist 49:d11a1914ee8b 2731 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2732 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2733 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2734 TFT.printf("cmd is executed");
arsenalist 49:d11a1914ee8b 2735 wait(1);
arsenalist 49:d11a1914ee8b 2736 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2737 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2738 TFT.foreground(White);
arsenalist 49:d11a1914ee8b 2739 execResult = "success";
arsenalist 49:d11a1914ee8b 2740 break;
arsenalist 49:d11a1914ee8b 2741 }
arsenalist 49:d11a1914ee8b 2742 trial++;
arsenalist 49:d11a1914ee8b 2743 }
arsenalist 49:d11a1914ee8b 2744 } else {
arsenalist 49:d11a1914ee8b 2745 TFT.foreground(Red);
arsenalist 49:d11a1914ee8b 2746 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2747 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2748 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2749 TFT.printf("node is invalid");
arsenalist 49:d11a1914ee8b 2750 wait(1);
arsenalist 49:d11a1914ee8b 2751 TFT.locate(0,180);
arsenalist 49:d11a1914ee8b 2752 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2753 TFT.foreground(White);
arsenalist 49:d11a1914ee8b 2754 }
arsenalist 49:d11a1914ee8b 2755 wait(2);
arsenalist 49:d11a1914ee8b 2756
arsenalist 49:d11a1914ee8b 2757 //send execution result
arsenalist 49:d11a1914ee8b 2758 sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());
arsenalist 49:d11a1914ee8b 2759 hmacCmd = calculateMD5(p);
arsenalist 49:d11a1914ee8b 2760
arsenalist 49:d11a1914ee8b 2761 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 49:d11a1914ee8b 2762 emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str());
arsenalist 49:d11a1914ee8b 2763
arsenalist 49:d11a1914ee8b 2764 sprintf(s,"POST /%s/api/controller/result HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 50:50d98fe6cc15 2765 connPort = restPORT;
arsenalist 49:d11a1914ee8b 2766
arsenalist 49:d11a1914ee8b 2767 trial=0;
arsenalist 49:d11a1914ee8b 2768 while(1) {
arsenalist 49:d11a1914ee8b 2769 if(trial>=2) { //two times trial
arsenalist 49:d11a1914ee8b 2770 DBG.printf("failed to send execution result\r\n");
arsenalist 49:d11a1914ee8b 2771 break;
arsenalist 49:d11a1914ee8b 2772 }
arsenalist 49:d11a1914ee8b 2773 str = gprsREST(restSERVER,connPort,s);
arsenalist 49:d11a1914ee8b 2774 wait(2);
arsenalist 49:d11a1914ee8b 2775 if(str.rfind("[{\"status\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2776 str.erase(str.begin(),str.begin()+str.rfind("[{\"status\""));
arsenalist 49:d11a1914ee8b 2777 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 49:d11a1914ee8b 2778 DBG.printf("success to send execution result\r\n");
arsenalist 49:d11a1914ee8b 2779 break;
arsenalist 49:d11a1914ee8b 2780 }
arsenalist 49:d11a1914ee8b 2781 }
arsenalist 49:d11a1914ee8b 2782 str.clear();
arsenalist 49:d11a1914ee8b 2783 trial++;
arsenalist 49:d11a1914ee8b 2784 }
arsenalist 49:d11a1914ee8b 2785 }
arsenalist 49:d11a1914ee8b 2786 }
arsenalist 49:d11a1914ee8b 2787 }
arsenalist 49:d11a1914ee8b 2788 //clear text on lcd
arsenalist 49:d11a1914ee8b 2789 TFT.locate(0,160);
arsenalist 49:d11a1914ee8b 2790 TFT.printf(" ");
arsenalist 49:d11a1914ee8b 2791 }
arsenalist 49:d11a1914ee8b 2792 osDelay(5000);
arsenalist 49:d11a1914ee8b 2793 }
arsenalist 0:f4e449fa34d7 2794 }
arsenalist 0:f4e449fa34d7 2795 }
arsenalist 0:f4e449fa34d7 2796 void emmaModeFirmwareDownload(void) {
arsenalist 0:f4e449fa34d7 2797 bool emmaGetFirmwareParam = false;
arsenalist 0:f4e449fa34d7 2798
arsenalist 0:f4e449fa34d7 2799 DBG.printf("emmaModeFirmwareDownload\r\n");
arsenalist 0:f4e449fa34d7 2800
arsenalist 0:f4e449fa34d7 2801 char s[384];
arsenalist 0:f4e449fa34d7 2802 string str;
arsenalist 0:f4e449fa34d7 2803 string connData;
arsenalist 0:f4e449fa34d7 2804 string chunk;
arsenalist 0:f4e449fa34d7 2805
arsenalist 0:f4e449fa34d7 2806 //firmware parameter
arsenalist 0:f4e449fa34d7 2807 string firmwareVer;
arsenalist 0:f4e449fa34d7 2808 string firmwareName;
arsenalist 0:f4e449fa34d7 2809 int numPart;
arsenalist 0:f4e449fa34d7 2810
arsenalist 0:f4e449fa34d7 2811 //downloading
arsenalist 0:f4e449fa34d7 2812 string firmwarePart;
arsenalist 0:f4e449fa34d7 2813 string calcMD5;
arsenalist 0:f4e449fa34d7 2814 string srvrMD5;
arsenalist 0:f4e449fa34d7 2815 bool nextPart;
arsenalist 0:f4e449fa34d7 2816
arsenalist 0:f4e449fa34d7 2817 //set wifi to mode bridge
arsenalist 0:f4e449fa34d7 2818 _ESP.printf("MODE=B");
arsenalist 0:f4e449fa34d7 2819 DBG.printf("set mode bridge\r\n");
arsenalist 0:f4e449fa34d7 2820 while(1) {
arsenalist 0:f4e449fa34d7 2821 char rcv[128] = {};
arsenalist 48:053e92b6dc1e 2822 wifiRcvReply(rcv,3000);
arsenalist 0:f4e449fa34d7 2823 str = rcv;
arsenalist 0:f4e449fa34d7 2824 if(str.find("MODE=B_OK") != std::string::npos)
arsenalist 0:f4e449fa34d7 2825 break;
arsenalist 0:f4e449fa34d7 2826 }
arsenalist 0:f4e449fa34d7 2827 DBG.printf("MODE=B\r\n");
arsenalist 0:f4e449fa34d7 2828
arsenalist 0:f4e449fa34d7 2829 esp.enable();
arsenalist 0:f4e449fa34d7 2830 wait(1);
arsenalist 0:f4e449fa34d7 2831 while(!esp.ready());
arsenalist 0:f4e449fa34d7 2832
arsenalist 0:f4e449fa34d7 2833 if(!rest.begin("candra.tritronik.com",3128,false)) {
arsenalist 0:f4e449fa34d7 2834 DBG.printf("EMMA: fail to setup rest");
arsenalist 0:f4e449fa34d7 2835 while(1);
arsenalist 0:f4e449fa34d7 2836 }
arsenalist 0:f4e449fa34d7 2837
arsenalist 0:f4e449fa34d7 2838 //wifiConnected = true; //with custom firmware, panel should connect wifi automatically
arsenalist 0:f4e449fa34d7 2839 useProxy = true;
arsenalist 0:f4e449fa34d7 2840
arsenalist 0:f4e449fa34d7 2841 esp.process();
arsenalist 0:f4e449fa34d7 2842 //set connData
arsenalist 0:f4e449fa34d7 2843 if(useProxy) {
arsenalist 0:f4e449fa34d7 2844 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2845 s[i]=0; }
arsenalist 38:e44f35f8129d 2846 //sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",restSERVER,restPORT,emmaUID.c_str(),hmac.c_str());
arsenalist 0:f4e449fa34d7 2847 sprintf(s,"http://192.168.128.69/emmaController/firmware/firmwareParameter");
arsenalist 0:f4e449fa34d7 2848 connData = s;
arsenalist 0:f4e449fa34d7 2849 } else {
arsenalist 0:f4e449fa34d7 2850 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2851 s[i]=0; }
arsenalist 0:f4e449fa34d7 2852 //sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
arsenalist 0:f4e449fa34d7 2853 sprintf(s,"/emmaController/firmware/firmwareParameter");
arsenalist 0:f4e449fa34d7 2854 connData = s;
arsenalist 0:f4e449fa34d7 2855 }
arsenalist 0:f4e449fa34d7 2856
arsenalist 0:f4e449fa34d7 2857 //get parameter of firmware to be downloaded
arsenalist 0:f4e449fa34d7 2858 while(!emmaGetFirmwareParam) {
arsenalist 0:f4e449fa34d7 2859 rest.get(connData.c_str());
arsenalist 0:f4e449fa34d7 2860 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2861 s[i]=0; }
arsenalist 0:f4e449fa34d7 2862 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 2863 //DBG.printf("rsp param:%s\r\n",s);
arsenalist 0:f4e449fa34d7 2864
arsenalist 0:f4e449fa34d7 2865 str = s;
arsenalist 0:f4e449fa34d7 2866 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2867 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 2868 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 2869
arsenalist 0:f4e449fa34d7 2870 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 2871 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 2872
arsenalist 0:f4e449fa34d7 2873 char *parameter[2] = {"firmwareVer","numPart"};
arsenalist 0:f4e449fa34d7 2874
arsenalist 0:f4e449fa34d7 2875 for(int i=0; i<2; i++) {
arsenalist 0:f4e449fa34d7 2876 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 2877 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 0:f4e449fa34d7 2878 if(i==0) {
arsenalist 0:f4e449fa34d7 2879 firmwareVer = val;
arsenalist 0:f4e449fa34d7 2880 } else if(i==1) {
arsenalist 0:f4e449fa34d7 2881 sscanf(val.c_str(),"%d",&numPart);
arsenalist 0:f4e449fa34d7 2882 }
arsenalist 0:f4e449fa34d7 2883 }
arsenalist 0:f4e449fa34d7 2884 }
arsenalist 0:f4e449fa34d7 2885
arsenalist 0:f4e449fa34d7 2886 if(!firmwareVer.empty() && numPart!=0) {
arsenalist 0:f4e449fa34d7 2887 emmaGetFirmwareParam = true;
arsenalist 0:f4e449fa34d7 2888 }
arsenalist 0:f4e449fa34d7 2889 }
arsenalist 0:f4e449fa34d7 2890 }
arsenalist 0:f4e449fa34d7 2891 DBG.printf("firmwareVer:%s\r\n",firmwareVer.c_str());
arsenalist 0:f4e449fa34d7 2892 DBG.printf("numPart:%d\r\n",numPart);
arsenalist 0:f4e449fa34d7 2893
arsenalist 0:f4e449fa34d7 2894 //clear firmware file
arsenalist 0:f4e449fa34d7 2895 while(1) {
arsenalist 0:f4e449fa34d7 2896 if(clearFirmware()){
arsenalist 0:f4e449fa34d7 2897 DBG.printf("clear firmware on sd card\r\n\r\n");
arsenalist 0:f4e449fa34d7 2898 break;
arsenalist 0:f4e449fa34d7 2899 }
arsenalist 0:f4e449fa34d7 2900 wait(1);
arsenalist 0:f4e449fa34d7 2901 }
arsenalist 0:f4e449fa34d7 2902
arsenalist 0:f4e449fa34d7 2903 //set connData
arsenalist 0:f4e449fa34d7 2904 if(useProxy) {
arsenalist 0:f4e449fa34d7 2905 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2906 s[i]=0; }
arsenalist 0:f4e449fa34d7 2907 sprintf(s,"http://192.168.128.69/emmaController/firmware/%s",firmwareVer.c_str());
arsenalist 0:f4e449fa34d7 2908 connData = s;
arsenalist 0:f4e449fa34d7 2909 } else {
arsenalist 0:f4e449fa34d7 2910 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2911 s[i]=0; }
arsenalist 0:f4e449fa34d7 2912 sprintf(s,"/emmaController/firmware/%s",firmwareVer.c_str());
arsenalist 0:f4e449fa34d7 2913 connData = s;
arsenalist 0:f4e449fa34d7 2914 }
arsenalist 0:f4e449fa34d7 2915
arsenalist 0:f4e449fa34d7 2916 //download firmware
arsenalist 0:f4e449fa34d7 2917 for(int n=0; n<numPart; n++) {
arsenalist 0:f4e449fa34d7 2918 nextPart = false;
arsenalist 0:f4e449fa34d7 2919 while(!nextPart) {
arsenalist 0:f4e449fa34d7 2920 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2921 s[i]=0; }
arsenalist 0:f4e449fa34d7 2922 sprintf(s,"%s/firmware_Hex_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
arsenalist 0:f4e449fa34d7 2923 rest.get(s);
arsenalist 0:f4e449fa34d7 2924 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2925 s[i]=0; }
arsenalist 0:f4e449fa34d7 2926 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 2927 //DBG.printf("rsp[%d]:%s\r\n",n,s);
arsenalist 0:f4e449fa34d7 2928
arsenalist 0:f4e449fa34d7 2929 str = s;
arsenalist 0:f4e449fa34d7 2930 if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2931 str.erase(str.begin(),str.begin()+str.find("{")+1);
arsenalist 0:f4e449fa34d7 2932 str.erase(str.begin()+str.find("}"),str.end());
arsenalist 0:f4e449fa34d7 2933 //DBG.printf("firmwarePart[%d]:%s\r\n",n,str.c_str());
arsenalist 0:f4e449fa34d7 2934 firmwarePart = str;
arsenalist 0:f4e449fa34d7 2935
arsenalist 0:f4e449fa34d7 2936 //calculated MD5
arsenalist 0:f4e449fa34d7 2937 calcMD5 = calculateMD5(firmwarePart);
arsenalist 0:f4e449fa34d7 2938 //DBG.printf("calcMD5[%d]:%s\r\n",n,calcMD5.c_str());
arsenalist 0:f4e449fa34d7 2939
arsenalist 0:f4e449fa34d7 2940 //MD5 from server
arsenalist 0:f4e449fa34d7 2941 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2942 s[i]=0; }
arsenalist 0:f4e449fa34d7 2943 sprintf(s,"%s/MD5/firmware_MD5_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
arsenalist 0:f4e449fa34d7 2944 rest.get(s);
arsenalist 0:f4e449fa34d7 2945 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2946 s[i]=0; }
arsenalist 0:f4e449fa34d7 2947 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 2948
arsenalist 0:f4e449fa34d7 2949 str = s;
arsenalist 0:f4e449fa34d7 2950 if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2951 str.erase(str.begin(),str.begin()+str.find("{")+1);
arsenalist 0:f4e449fa34d7 2952 str.erase(str.begin()+str.find("}"),str.end());
arsenalist 0:f4e449fa34d7 2953 srvrMD5 = str;
arsenalist 0:f4e449fa34d7 2954 //DBG.printf("srvrMD5[%d]:%s\r\n",n,srvrMD5.c_str());
arsenalist 0:f4e449fa34d7 2955
arsenalist 0:f4e449fa34d7 2956 //compare original MD5 vs MD5 from server
arsenalist 0:f4e449fa34d7 2957 if(strcmp(calcMD5.c_str(),srvrMD5.c_str()) == 0) {
arsenalist 0:f4e449fa34d7 2958 //DBG.printf("MD5 correct\r\n");
arsenalist 0:f4e449fa34d7 2959
arsenalist 0:f4e449fa34d7 2960 //save to sd card
arsenalist 0:f4e449fa34d7 2961 int st = writeFirmwareHexToChar(firmwarePart);
arsenalist 0:f4e449fa34d7 2962 if(st) {
arsenalist 0:f4e449fa34d7 2963 DBG.printf("firmwarePart[%d/%d] written\r\n",n,numPart-1);
arsenalist 0:f4e449fa34d7 2964 nextPart = true;
arsenalist 0:f4e449fa34d7 2965 }
arsenalist 0:f4e449fa34d7 2966
arsenalist 0:f4e449fa34d7 2967 } else {
arsenalist 0:f4e449fa34d7 2968 DBG.printf("MD5 incorrect\r\n");
arsenalist 0:f4e449fa34d7 2969 }
arsenalist 0:f4e449fa34d7 2970 }
arsenalist 0:f4e449fa34d7 2971 } else {
arsenalist 0:f4e449fa34d7 2972 DBG.printf("retry to fetch firmwarePart[%d]\r\n",n);
arsenalist 0:f4e449fa34d7 2973 }
arsenalist 0:f4e449fa34d7 2974 wait(0.5);
arsenalist 0:f4e449fa34d7 2975 }
arsenalist 0:f4e449fa34d7 2976 }
arsenalist 0:f4e449fa34d7 2977 DBG.printf("download finished\r\n");
arsenalist 0:f4e449fa34d7 2978 }
arsenalist 44:c1d11c491237 2979
arsenalist 44:c1d11c491237 2980 void emmaModeReserved(void) {
arsenalist 44:c1d11c491237 2981 char connBody[128];
arsenalist 44:c1d11c491237 2982 char s[256];
arsenalist 46:86a0c2133130 2983 //char buf[1024];
arsenalist 44:c1d11c491237 2984 int connBodyLen;
arsenalist 44:c1d11c491237 2985 int i=0;
arsenalist 46:86a0c2133130 2986 //int ret=99;
arsenalist 44:c1d11c491237 2987 string str;
arsenalist 44:c1d11c491237 2988 Timer t;
arsenalist 45:2406c2b6995a 2989 //TCPSocketConnection sock;
arsenalist 44:c1d11c491237 2990
arsenalist 44:c1d11c491237 2991 eth.init();
arsenalist 45:2406c2b6995a 2992 eth.connect();
arsenalist 45:2406c2b6995a 2993 connBodyLen = sprintf(connBody,"{\"uid\":\"005e00553533510334313732\",\"hmac\":\"9424c60e708d8e3d3dff6d23fb104339\"}");
arsenalist 45:2406c2b6995a 2994 sprintf(s,"POST /emma/api/controller/command HTTP/1.0\nHost: 36.80.35.8\nContent-Length:%d\n\n%s\r\n\r\n",connBodyLen,connBody);
arsenalist 44:c1d11c491237 2995 while(1) {
arsenalist 44:c1d11c491237 2996 //command
arsenalist 44:c1d11c491237 2997 str.clear();
arsenalist 44:c1d11c491237 2998 str = ethREST("36.80.35.8",8080,s);
arsenalist 44:c1d11c491237 2999 DBG.printf("[%d]str:%s\r\n",i,str.c_str());
arsenalist 44:c1d11c491237 3000 i++;
arsenalist 44:c1d11c491237 3001 wait(5);
arsenalist 44:c1d11c491237 3002 }
arsenalist 44:c1d11c491237 3003
arsenalist 45:2406c2b6995a 3004 /*
arsenalist 44:c1d11c491237 3005 //start working as expected
arsenalist 44:c1d11c491237 3006 eth.init();
arsenalist 44:c1d11c491237 3007 eth.connect();
arsenalist 44:c1d11c491237 3008 connBodyLen = sprintf(connBody,"{\"uid\":\"005e00553533510334313732\",\"hmac\":\"9424c60e708d8e3d3dff6d23fb104339\"}");
arsenalist 44:c1d11c491237 3009 sprintf(s,"POST /emma/api/controller/command HTTP/1.0\nHost: 36.80.35.8\nContent-Length:%d\n\n%s\r\n\r\n",connBodyLen,connBody);
arsenalist 44:c1d11c491237 3010
arsenalist 44:c1d11c491237 3011 while(1) {
arsenalist 44:c1d11c491237 3012 //eth.connect();
arsenalist 44:c1d11c491237 3013 t.start();
arsenalist 44:c1d11c491237 3014 while(eth.linkstatus() && t.read() < 5 && ret !=0) {
arsenalist 44:c1d11c491237 3015 ret = sock.connect("36.80.35.8",8080);
arsenalist 44:c1d11c491237 3016 }
arsenalist 44:c1d11c491237 3017 t.stop();
arsenalist 44:c1d11c491237 3018 t.reset();
arsenalist 44:c1d11c491237 3019
arsenalist 44:c1d11c491237 3020 memset(buf,0,sizeof(buf));
arsenalist 44:c1d11c491237 3021 if(sock.is_connected()) {
arsenalist 44:c1d11c491237 3022 DBG.printf("connect\r\n");
arsenalist 44:c1d11c491237 3023 sock.send_all(s,sizeof(s)-1);
arsenalist 44:c1d11c491237 3024 wait(2);
arsenalist 44:c1d11c491237 3025 //receive return
arsenalist 44:c1d11c491237 3026 t.start();
arsenalist 44:c1d11c491237 3027 while(1) {
arsenalist 44:c1d11c491237 3028 ret = sock.receive(buf, sizeof(buf));
arsenalist 44:c1d11c491237 3029 if(ret<=0 || t.read() > 5) {
arsenalist 44:c1d11c491237 3030 t.stop();
arsenalist 44:c1d11c491237 3031 t.reset();
arsenalist 44:c1d11c491237 3032 break;
arsenalist 44:c1d11c491237 3033 }
arsenalist 44:c1d11c491237 3034 }
arsenalist 44:c1d11c491237 3035 sock.close();
arsenalist 44:c1d11c491237 3036 } else {
arsenalist 44:c1d11c491237 3037 DBG.printf("not connected\r\n");
arsenalist 44:c1d11c491237 3038 }
arsenalist 44:c1d11c491237 3039 //eth.disconnect();
arsenalist 44:c1d11c491237 3040 DBG.printf("[%d]BUF:%s\r\n\r\n",i,buf);
arsenalist 44:c1d11c491237 3041 i++;
arsenalist 44:c1d11c491237 3042 wait(5);
arsenalist 44:c1d11c491237 3043 }
arsenalist 44:c1d11c491237 3044 //end working as expected
arsenalist 45:2406c2b6995a 3045 */
arsenalist 44:c1d11c491237 3046 }
arsenalist 12:96f637ed37f9 3047 /*end emma mode*/
arsenalist 0:f4e449fa34d7 3048
arsenalist 4:76ab12e2f8a3 3049 /*start energy related*/
arsenalist 0:f4e449fa34d7 3050 void energyThread(void const*) {
arsenalist 19:7e3e9332f719 3051 Timer tEnergy;
arsenalist 19:7e3e9332f719 3052
arsenalist 19:7e3e9332f719 3053 while(1) {
arsenalist 19:7e3e9332f719 3054 tEnergy.start();
arsenalist 19:7e3e9332f719 3055 DBG.printf("energyThread-start\r\n");
arsenalist 19:7e3e9332f719 3056
arsenalist 19:7e3e9332f719 3057 AWattHrSum = 0;
arsenalist 19:7e3e9332f719 3058 BWattHrSum = 0;
arsenalist 19:7e3e9332f719 3059 CWattHrSum = 0;
arsenalist 19:7e3e9332f719 3060
arsenalist 49:d11a1914ee8b 3061 while(tEnergy.read() < 5*60.0) {
arsenalist 19:7e3e9332f719 3062 AWattHrValue = ADE.getWattHR(PHASE_A);
arsenalist 19:7e3e9332f719 3063 BWattHrValue = ADE.getWattHR(PHASE_B);
arsenalist 19:7e3e9332f719 3064 CWattHrValue = ADE.getWattHR(PHASE_C);
arsenalist 19:7e3e9332f719 3065
arsenalist 19:7e3e9332f719 3066 AWattHrSum += AWattHrValue;
arsenalist 19:7e3e9332f719 3067 BWattHrSum += BWattHrValue;
arsenalist 20:ea14f175bbb4 3068 CWattHrSum += CWattHrValue;
arsenalist 20:ea14f175bbb4 3069
arsenalist 20:ea14f175bbb4 3070
arsenalist 20:ea14f175bbb4 3071 //start check voltage and power
arsenalist 21:33bd8b82560f 3072 AVrms = ADE.VRMS(PHASE_A) * 0.000124f; //0.000158; //constants are from calculateVRMS function
arsenalist 21:33bd8b82560f 3073 BVrms = ADE.VRMS(PHASE_B) * 0.000123f;
arsenalist 21:33bd8b82560f 3074 CVrms = ADE.VRMS(PHASE_C) * 0.000122f;
arsenalist 20:ea14f175bbb4 3075
arsenalist 21:33bd8b82560f 3076 AIrms = ADE.IRMS(PHASE_A) * 0.00001006f; //0.0000125f; //constants are from calculateIRMS function
arsenalist 21:33bd8b82560f 3077 BIrms = ADE.IRMS(PHASE_B) * 0.00001005f;
arsenalist 21:33bd8b82560f 3078 CIrms = ADE.IRMS(PHASE_C) * 0.00001004f;
arsenalist 20:ea14f175bbb4 3079
arsenalist 20:ea14f175bbb4 3080 AWatt = AVrms * AIrms;
arsenalist 20:ea14f175bbb4 3081 BWatt = BVrms * BIrms;
arsenalist 20:ea14f175bbb4 3082 CWatt = CVrms * CIrms;
arsenalist 20:ea14f175bbb4 3083 //end check voltage and power
arsenalist 20:ea14f175bbb4 3084
arsenalist 19:7e3e9332f719 3085 }
arsenalist 19:7e3e9332f719 3086
arsenalist 25:36c6a5db50ed 3087 AWattHr = AWattHrSum * 0.000044169f; //0.0000198f;
arsenalist 25:36c6a5db50ed 3088 BWattHr = BWattHrSum * 0.000044168f; //0.0000197f;
arsenalist 25:36c6a5db50ed 3089 CWattHr = CWattHrSum * 0.000044167f; //0.0000196f;
arsenalist 19:7e3e9332f719 3090
arsenalist 22:e18d361bf688 3091 newEnergyData = true;
arsenalist 22:e18d361bf688 3092
arsenalist 19:7e3e9332f719 3093 tEnergy.stop();
arsenalist 19:7e3e9332f719 3094 tEnergy.reset();
arsenalist 19:7e3e9332f719 3095 DBG.printf("energyThread-finish\r\n");
arsenalist 19:7e3e9332f719 3096 }
arsenalist 0:f4e449fa34d7 3097 }
arsenalist 47:97eadcb478b2 3098
arsenalist 21:33bd8b82560f 3099 void checkVoltagePower(void) {
arsenalist 4:76ab12e2f8a3 3100 //check if voltage or power violates threshold
arsenalist 47:97eadcb478b2 3101 char connBody[256];
arsenalist 22:e18d361bf688 3102 char p[64];
arsenalist 20:ea14f175bbb4 3103 char q[32];
arsenalist 4:76ab12e2f8a3 3104 char s[256];
arsenalist 47:97eadcb478b2 3105 int connBodyLen=0;
arsenalist 47:97eadcb478b2 3106 int connPort=0;
arsenalist 47:97eadcb478b2 3107 int alertType=0;
arsenalist 22:e18d361bf688 3108 string hmacTime;
arsenalist 4:76ab12e2f8a3 3109 string str;
arsenalist 20:ea14f175bbb4 3110 time_t seconds;
arsenalist 4:76ab12e2f8a3 3111
arsenalist 4:76ab12e2f8a3 3112 //DBG.printf("checkVoltagePower-start\r\n");
arsenalist 4:76ab12e2f8a3 3113
arsenalist 47:97eadcb478b2 3114 //vrms and irms should be placed inside energy calculation routine
arsenalist 19:7e3e9332f719 3115
arsenalist 20:ea14f175bbb4 3116 //get time
arsenalist 20:ea14f175bbb4 3117 seconds = time(NULL);
arsenalist 20:ea14f175bbb4 3118 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 20:ea14f175bbb4 3119
arsenalist 22:e18d361bf688 3120 //calculate hmacTime
arsenalist 22:e18d361bf688 3121 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 22:e18d361bf688 3122 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 3123
arsenalist 47:97eadcb478b2 3124 //read connPort
arsenalist 50:50d98fe6cc15 3125 connPort = restPORT;
arsenalist 47:97eadcb478b2 3126
arsenalist 47:97eadcb478b2 3127 if(vrmsTHL > AVrms) {
arsenalist 47:97eadcb478b2 3128 alertType = 1;
arsenalist 47:97eadcb478b2 3129 } else if (AVrms > vrmsTHH) {
arsenalist 47:97eadcb478b2 3130 alertType = 2;
arsenalist 47:97eadcb478b2 3131 }
arsenalist 47:97eadcb478b2 3132
arsenalist 47:97eadcb478b2 3133 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
arsenalist 47:97eadcb478b2 3134 emmaUID.c_str(),hmacTime.c_str(),q,AVrms,alertType);
arsenalist 47:97eadcb478b2 3135
arsenalist 47:97eadcb478b2 3136 if(alertType != 0 && ethConnected && allowAlertAVrms) {
arsenalist 47:97eadcb478b2 3137 sprintf(s,"POST /%s/api/controller/alert/1 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 47:97eadcb478b2 3138 str.clear();
arsenalist 47:97eadcb478b2 3139 str = ethREST(restSERVER,connPort,s);
arsenalist 47:97eadcb478b2 3140 allowAlertAVrms = false;
arsenalist 47:97eadcb478b2 3141 DBG.printf("[ph1]vrms:%s\r\n",str.c_str());
arsenalist 47:97eadcb478b2 3142 } else if(alertType != 0 && wifiConnected && allowAlertAVrms) {
arsenalist 47:97eadcb478b2 3143 sprintf(p,"/%s/api/controller/alert/1",appNAME.c_str());
arsenalist 47:97eadcb478b2 3144 //DBG.printf("[ph1]:%s\r\n",connBody);
arsenalist 43:612547648ed1 3145
arsenalist 47:97eadcb478b2 3146 rxBuf.clear();
arsenalist 47:97eadcb478b2 3147 rest.post(p,connBody);
arsenalist 47:97eadcb478b2 3148 wait(2);
arsenalist 47:97eadcb478b2 3149 str = rxBuf;
arsenalist 47:97eadcb478b2 3150 if(str.rfind("/alert/1") != std::string::npos) {
arsenalist 47:97eadcb478b2 3151 str.erase(str.begin(),str.begin()+str.rfind("/alert/1"));
arsenalist 47:97eadcb478b2 3152 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 47:97eadcb478b2 3153 DBG.printf("[ph1]vrms success\r\n");
arsenalist 47:97eadcb478b2 3154 } else {
arsenalist 47:97eadcb478b2 3155 DBG.printf("[ph1]vrms failed\r\n");
arsenalist 47:97eadcb478b2 3156 }
arsenalist 47:97eadcb478b2 3157 }
arsenalist 47:97eadcb478b2 3158 allowAlertAVrms = false;
arsenalist 51:ad6410dcaf52 3159 } else if(alertType != 0 && gprsConnected && allowAlertAVrms) {
arsenalist 51:ad6410dcaf52 3160 sprintf(s,"POST /%s/api/controller/alert/1 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 51:ad6410dcaf52 3161 str.clear();
arsenalist 51:ad6410dcaf52 3162 str = gprsREST(restSERVER,connPort,s);
arsenalist 51:ad6410dcaf52 3163 if(str.rfind("/alert/1") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3164 str.erase(str.begin(),str.begin()+str.rfind("/alert/1"));
arsenalist 51:ad6410dcaf52 3165 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3166 DBG.printf("[ph1]vrms success\r\n");
arsenalist 51:ad6410dcaf52 3167 } else {
arsenalist 51:ad6410dcaf52 3168 DBG.printf("[ph1]vrms failed\r\n");
arsenalist 51:ad6410dcaf52 3169 }
arsenalist 51:ad6410dcaf52 3170 }
arsenalist 51:ad6410dcaf52 3171 allowAlertAVrms = false;
arsenalist 47:97eadcb478b2 3172 }
arsenalist 47:97eadcb478b2 3173
arsenalist 47:97eadcb478b2 3174 alertType = 0;
arsenalist 47:97eadcb478b2 3175 if(wattTHL > AWatt) {
arsenalist 47:97eadcb478b2 3176 alertType = 3;
arsenalist 47:97eadcb478b2 3177 } else if (AWatt > wattTHH) {
arsenalist 47:97eadcb478b2 3178 alertType = 4;
arsenalist 47:97eadcb478b2 3179 }
arsenalist 50:50d98fe6cc15 3180
arsenalist 50:50d98fe6cc15 3181 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
arsenalist 50:50d98fe6cc15 3182 emmaUID.c_str(),hmacTime.c_str(),q,AWatt,alertType);
arsenalist 50:50d98fe6cc15 3183
arsenalist 47:97eadcb478b2 3184 if(alertType != 0 && ethConnected && allowAlertAWatt) {
arsenalist 47:97eadcb478b2 3185 sprintf(s,"POST /%s/api/controller/alert/1 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 47:97eadcb478b2 3186 str.clear();
arsenalist 47:97eadcb478b2 3187 str = ethREST(restSERVER,connPort,s);
arsenalist 47:97eadcb478b2 3188 allowAlertAWatt = false;
arsenalist 47:97eadcb478b2 3189 DBG.printf("[ph1]watt:%s\r\n",str.c_str());
arsenalist 47:97eadcb478b2 3190 } else if(alertType != 0 && wifiConnected && allowAlertAWatt) {
arsenalist 43:612547648ed1 3191 sprintf(p,"/%s/api/controller/alert/1",appNAME.c_str());
arsenalist 47:97eadcb478b2 3192 //DBG.printf("[ph1]:%s\r\n",connBody);
arsenalist 47:97eadcb478b2 3193
arsenalist 47:97eadcb478b2 3194 rxBuf.clear();
arsenalist 47:97eadcb478b2 3195 rest.post(p,connBody);
arsenalist 4:76ab12e2f8a3 3196 wait(2);
arsenalist 4:76ab12e2f8a3 3197 str = rxBuf;
arsenalist 4:76ab12e2f8a3 3198 if(str.rfind("/alert/1") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 3199 str.erase(str.begin(),str.begin()+str.rfind("/alert/1"));
arsenalist 4:76ab12e2f8a3 3200 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 47:97eadcb478b2 3201 DBG.printf("[ph1]watt success\r\n");
arsenalist 4:76ab12e2f8a3 3202 } else {
arsenalist 47:97eadcb478b2 3203 DBG.printf("[ph1]watt failed\r\n");
arsenalist 4:76ab12e2f8a3 3204 }
arsenalist 4:76ab12e2f8a3 3205 }
arsenalist 47:97eadcb478b2 3206 allowAlertAWatt = false;
arsenalist 51:ad6410dcaf52 3207 } else if(alertType != 0 && gprsConnected && allowAlertAWatt) {
arsenalist 51:ad6410dcaf52 3208 sprintf(s,"POST /%s/api/controller/alert/1 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 51:ad6410dcaf52 3209 str.clear();
arsenalist 51:ad6410dcaf52 3210 str = gprsREST(restSERVER,connPort,s);
arsenalist 51:ad6410dcaf52 3211 if(str.rfind("/alert/1") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3212 str.erase(str.begin(),str.begin()+str.rfind("/alert/1"));
arsenalist 51:ad6410dcaf52 3213 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3214 DBG.printf("[ph1]watt success\r\n");
arsenalist 51:ad6410dcaf52 3215 } else {
arsenalist 51:ad6410dcaf52 3216 DBG.printf("[ph1]watt failed\r\n");
arsenalist 51:ad6410dcaf52 3217 }
arsenalist 51:ad6410dcaf52 3218 }
arsenalist 51:ad6410dcaf52 3219 allowAlertAWatt = false;
arsenalist 4:76ab12e2f8a3 3220 }
arsenalist 4:76ab12e2f8a3 3221
arsenalist 47:97eadcb478b2 3222 alertType = 0;
arsenalist 47:97eadcb478b2 3223 if(vrmsTHL > BVrms) {
arsenalist 47:97eadcb478b2 3224 alertType = 1;
arsenalist 47:97eadcb478b2 3225 } else if (BVrms > vrmsTHH) {
arsenalist 47:97eadcb478b2 3226 alertType = 2;
arsenalist 47:97eadcb478b2 3227 }
arsenalist 50:50d98fe6cc15 3228
arsenalist 50:50d98fe6cc15 3229 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
arsenalist 50:50d98fe6cc15 3230 emmaUID.c_str(),hmacTime.c_str(),q,BVrms,alertType);
arsenalist 50:50d98fe6cc15 3231
arsenalist 47:97eadcb478b2 3232 if(alertType != 0 && ethConnected && allowAlertBVrms) {
arsenalist 47:97eadcb478b2 3233 sprintf(s,"POST /%s/api/controller/alert/2 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 47:97eadcb478b2 3234 str.clear();
arsenalist 47:97eadcb478b2 3235 str = ethREST(restSERVER,connPort,s);
arsenalist 47:97eadcb478b2 3236 allowAlertBVrms = false;
arsenalist 47:97eadcb478b2 3237 DBG.printf("[ph2]vrms:%s\r\n",str.c_str());
arsenalist 47:97eadcb478b2 3238 } else if(alertType != 0 && wifiConnected && allowAlertBVrms) {
arsenalist 43:612547648ed1 3239 sprintf(p,"/%s/api/controller/alert/2",appNAME.c_str());
arsenalist 47:97eadcb478b2 3240 //DBG.printf("[ph2]:%s\r\n",connBody);
arsenalist 47:97eadcb478b2 3241
arsenalist 47:97eadcb478b2 3242 rxBuf.clear();
arsenalist 47:97eadcb478b2 3243 rest.post(p,connBody);
arsenalist 47:97eadcb478b2 3244 wait(2);
arsenalist 47:97eadcb478b2 3245 str = rxBuf;
arsenalist 47:97eadcb478b2 3246 if(str.rfind("/alert/2") != std::string::npos) {
arsenalist 47:97eadcb478b2 3247 str.erase(str.begin(),str.begin()+str.rfind("/alert/2"));
arsenalist 47:97eadcb478b2 3248 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 47:97eadcb478b2 3249 DBG.printf("[ph2]vrms success\r\n");
arsenalist 47:97eadcb478b2 3250 } else {
arsenalist 47:97eadcb478b2 3251 DBG.printf("[ph2]vrms failed\r\n");
arsenalist 47:97eadcb478b2 3252 }
arsenalist 47:97eadcb478b2 3253 }
arsenalist 47:97eadcb478b2 3254 allowAlertBVrms = false;
arsenalist 51:ad6410dcaf52 3255 } else if(alertType != 0 && gprsConnected && allowAlertBVrms) {
arsenalist 51:ad6410dcaf52 3256 sprintf(s,"POST /%s/api/controller/alert/2 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 51:ad6410dcaf52 3257 str.clear();
arsenalist 51:ad6410dcaf52 3258 str = gprsREST(restSERVER,connPort,s);
arsenalist 51:ad6410dcaf52 3259 if(str.rfind("/alert/2") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3260 str.erase(str.begin(),str.begin()+str.rfind("/alert/2"));
arsenalist 51:ad6410dcaf52 3261 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3262 DBG.printf("[ph2]vrms success\r\n");
arsenalist 51:ad6410dcaf52 3263 } else {
arsenalist 51:ad6410dcaf52 3264 DBG.printf("[ph2]vrms failed\r\n");
arsenalist 51:ad6410dcaf52 3265 }
arsenalist 51:ad6410dcaf52 3266 }
arsenalist 51:ad6410dcaf52 3267 allowAlertBVrms = false;
arsenalist 47:97eadcb478b2 3268 }
arsenalist 47:97eadcb478b2 3269
arsenalist 47:97eadcb478b2 3270 alertType = 0;
arsenalist 47:97eadcb478b2 3271 if(wattTHL > BWatt) {
arsenalist 47:97eadcb478b2 3272 alertType = 3;
arsenalist 47:97eadcb478b2 3273 } else if (BWatt > wattTHH) {
arsenalist 47:97eadcb478b2 3274 alertType = 4;
arsenalist 47:97eadcb478b2 3275 }
arsenalist 50:50d98fe6cc15 3276
arsenalist 50:50d98fe6cc15 3277 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
arsenalist 50:50d98fe6cc15 3278 emmaUID.c_str(),hmacTime.c_str(),q,BWatt,alertType);
arsenalist 50:50d98fe6cc15 3279
arsenalist 47:97eadcb478b2 3280 if(alertType != 0 && ethConnected && allowAlertBWatt) {
arsenalist 47:97eadcb478b2 3281 sprintf(s,"POST /%s/api/controller/alert/2 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 47:97eadcb478b2 3282 str.clear();
arsenalist 47:97eadcb478b2 3283 str = ethREST(restSERVER,connPort,s);
arsenalist 47:97eadcb478b2 3284 allowAlertBWatt = false;
arsenalist 47:97eadcb478b2 3285 DBG.printf("[ph2]watt:%s\r\n",str.c_str());
arsenalist 47:97eadcb478b2 3286 } else if(alertType != 0 && wifiConnected && allowAlertBWatt) {
arsenalist 47:97eadcb478b2 3287 sprintf(p,"/%s/api/controller/alert/2",appNAME.c_str());
arsenalist 47:97eadcb478b2 3288 //DBG.printf("[ph2]:%s\r\n",connBody);
arsenalist 47:97eadcb478b2 3289
arsenalist 47:97eadcb478b2 3290 rxBuf.clear();
arsenalist 47:97eadcb478b2 3291 rest.post(p,connBody);
arsenalist 4:76ab12e2f8a3 3292 wait(2);
arsenalist 4:76ab12e2f8a3 3293 str = rxBuf;
arsenalist 4:76ab12e2f8a3 3294 if(str.rfind("/alert/2") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 3295 str.erase(str.begin(),str.begin()+str.rfind("/alert/2"));
arsenalist 4:76ab12e2f8a3 3296 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 47:97eadcb478b2 3297 DBG.printf("[ph2]watt success\r\n");
arsenalist 4:76ab12e2f8a3 3298 } else {
arsenalist 47:97eadcb478b2 3299 DBG.printf("[ph2]watt failed\r\n");
arsenalist 4:76ab12e2f8a3 3300 }
arsenalist 4:76ab12e2f8a3 3301 }
arsenalist 47:97eadcb478b2 3302 allowAlertBWatt = false;
arsenalist 51:ad6410dcaf52 3303 } else if(alertType != 0 && gprsConnected && allowAlertBWatt) {
arsenalist 51:ad6410dcaf52 3304 sprintf(s,"POST /%s/api/controller/alert/2 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 51:ad6410dcaf52 3305 str.clear();
arsenalist 51:ad6410dcaf52 3306 str = gprsREST(restSERVER,connPort,s);
arsenalist 51:ad6410dcaf52 3307 if(str.rfind("/alert/2") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3308 str.erase(str.begin(),str.begin()+str.rfind("/alert/2"));
arsenalist 51:ad6410dcaf52 3309 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3310 DBG.printf("[ph2]watt success\r\n");
arsenalist 51:ad6410dcaf52 3311 } else {
arsenalist 51:ad6410dcaf52 3312 DBG.printf("[ph2]watt failed\r\n");
arsenalist 51:ad6410dcaf52 3313 }
arsenalist 51:ad6410dcaf52 3314 }
arsenalist 51:ad6410dcaf52 3315 allowAlertBWatt = false;
arsenalist 4:76ab12e2f8a3 3316 }
arsenalist 4:76ab12e2f8a3 3317
arsenalist 47:97eadcb478b2 3318 alertType = 0;
arsenalist 47:97eadcb478b2 3319 if(vrmsTHL > CVrms) {
arsenalist 47:97eadcb478b2 3320 alertType = 1;
arsenalist 47:97eadcb478b2 3321 } else if (CVrms > vrmsTHH) {
arsenalist 47:97eadcb478b2 3322 alertType = 2;
arsenalist 47:97eadcb478b2 3323 }
arsenalist 50:50d98fe6cc15 3324
arsenalist 50:50d98fe6cc15 3325 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
arsenalist 50:50d98fe6cc15 3326 emmaUID.c_str(),hmacTime.c_str(),q,CVrms,alertType);
arsenalist 50:50d98fe6cc15 3327
arsenalist 47:97eadcb478b2 3328 if(alertType != 0 && ethConnected && allowAlertCVrms) {
arsenalist 47:97eadcb478b2 3329 sprintf(s,"POST /%s/api/controller/alert/3 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 47:97eadcb478b2 3330 str.clear();
arsenalist 47:97eadcb478b2 3331 str = ethREST(restSERVER,connPort,s);
arsenalist 47:97eadcb478b2 3332 allowAlertCVrms = false;
arsenalist 47:97eadcb478b2 3333 DBG.printf("[ph3]vrms:%s\r\n",str.c_str());
arsenalist 47:97eadcb478b2 3334 } else if(alertType != 0 && wifiConnected && allowAlertCVrms) {
arsenalist 43:612547648ed1 3335 sprintf(p,"/%s/api/controller/alert/3",appNAME.c_str());
arsenalist 47:97eadcb478b2 3336 //DBG.printf("[ph3]:%s\r\n",connBody);
arsenalist 47:97eadcb478b2 3337
arsenalist 47:97eadcb478b2 3338 rxBuf.clear();
arsenalist 47:97eadcb478b2 3339 rest.post(p,connBody);
arsenalist 47:97eadcb478b2 3340 wait(2);
arsenalist 47:97eadcb478b2 3341 str = rxBuf;
arsenalist 47:97eadcb478b2 3342 if(str.rfind("/alert/3") != std::string::npos) {
arsenalist 47:97eadcb478b2 3343 str.erase(str.begin(),str.begin()+str.rfind("/alert/3"));
arsenalist 47:97eadcb478b2 3344 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 47:97eadcb478b2 3345 DBG.printf("[ph3]vrms success\r\n");
arsenalist 47:97eadcb478b2 3346 } else {
arsenalist 47:97eadcb478b2 3347 DBG.printf("[ph3]vrms failed\r\n");
arsenalist 47:97eadcb478b2 3348 }
arsenalist 47:97eadcb478b2 3349 }
arsenalist 47:97eadcb478b2 3350 allowAlertCVrms = false;
arsenalist 51:ad6410dcaf52 3351 } else if(alertType != 0 && gprsConnected && allowAlertCVrms) {
arsenalist 51:ad6410dcaf52 3352 sprintf(s,"POST /%s/api/controller/alert/3 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 51:ad6410dcaf52 3353 str.clear();
arsenalist 51:ad6410dcaf52 3354 str = gprsREST(restSERVER,connPort,s);
arsenalist 51:ad6410dcaf52 3355 if(str.rfind("/alert/3") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3356 str.erase(str.begin(),str.begin()+str.rfind("/alert/3"));
arsenalist 51:ad6410dcaf52 3357 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3358 DBG.printf("[ph3]vrms success\r\n");
arsenalist 51:ad6410dcaf52 3359 } else {
arsenalist 51:ad6410dcaf52 3360 DBG.printf("[ph3]vrms failed\r\n");
arsenalist 51:ad6410dcaf52 3361 }
arsenalist 51:ad6410dcaf52 3362 }
arsenalist 51:ad6410dcaf52 3363 allowAlertCVrms = false;
arsenalist 47:97eadcb478b2 3364 }
arsenalist 47:97eadcb478b2 3365
arsenalist 47:97eadcb478b2 3366 alertType = 0;
arsenalist 47:97eadcb478b2 3367 if(wattTHL > CWatt) {
arsenalist 47:97eadcb478b2 3368 alertType = 3;
arsenalist 47:97eadcb478b2 3369 } else if (CWatt > wattTHH) {
arsenalist 47:97eadcb478b2 3370 alertType = 4;
arsenalist 47:97eadcb478b2 3371 }
arsenalist 50:50d98fe6cc15 3372
arsenalist 50:50d98fe6cc15 3373 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"value\":%.2f,\"type\":%d}",
arsenalist 50:50d98fe6cc15 3374 emmaUID.c_str(),hmacTime.c_str(),q,CWatt,alertType);
arsenalist 50:50d98fe6cc15 3375
arsenalist 47:97eadcb478b2 3376 if(alertType != 0 && ethConnected && allowAlertCWatt) {
arsenalist 47:97eadcb478b2 3377 sprintf(s,"POST /%s/api/controller/alert/3 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 47:97eadcb478b2 3378 str.clear();
arsenalist 47:97eadcb478b2 3379 str = ethREST(restSERVER,connPort,s);
arsenalist 47:97eadcb478b2 3380 allowAlertCWatt = false;
arsenalist 47:97eadcb478b2 3381 DBG.printf("[ph3]watt:%s\r\n",str.c_str());
arsenalist 47:97eadcb478b2 3382 } else if(alertType != 0 && wifiConnected && allowAlertCWatt) {
arsenalist 47:97eadcb478b2 3383 sprintf(p,"/%s/api/controller/alert/3",appNAME.c_str());
arsenalist 47:97eadcb478b2 3384 //DBG.printf("[ph3]:%s\r\n",connBody);
arsenalist 47:97eadcb478b2 3385
arsenalist 47:97eadcb478b2 3386 rxBuf.clear();
arsenalist 47:97eadcb478b2 3387 rest.post(p,connBody);
arsenalist 4:76ab12e2f8a3 3388 wait(2);
arsenalist 4:76ab12e2f8a3 3389 str = rxBuf;
arsenalist 4:76ab12e2f8a3 3390 if(str.rfind("/alert/3") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 3391 str.erase(str.begin(),str.begin()+str.rfind("/alert/3"));
arsenalist 4:76ab12e2f8a3 3392 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 47:97eadcb478b2 3393 DBG.printf("[ph3]watt success\r\n");
arsenalist 4:76ab12e2f8a3 3394 } else {
arsenalist 47:97eadcb478b2 3395 DBG.printf("[ph3]watt failed\r\n");
arsenalist 4:76ab12e2f8a3 3396 }
arsenalist 4:76ab12e2f8a3 3397 }
arsenalist 47:97eadcb478b2 3398 allowAlertCWatt = false;
arsenalist 51:ad6410dcaf52 3399 } else if(alertType != 0 && gprsConnected && allowAlertCWatt) {
arsenalist 51:ad6410dcaf52 3400 sprintf(s,"POST /%s/api/controller/alert/3 HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 51:ad6410dcaf52 3401 str.clear();
arsenalist 51:ad6410dcaf52 3402 str = gprsREST(restSERVER,connPort,s);
arsenalist 51:ad6410dcaf52 3403 if(str.rfind("/alert/3") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3404 str.erase(str.begin(),str.begin()+str.rfind("/alert/3"));
arsenalist 51:ad6410dcaf52 3405 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 51:ad6410dcaf52 3406 DBG.printf("[ph3]watt success\r\n");
arsenalist 51:ad6410dcaf52 3407 } else {
arsenalist 51:ad6410dcaf52 3408 DBG.printf("[ph3]watt failed\r\n");
arsenalist 51:ad6410dcaf52 3409 }
arsenalist 51:ad6410dcaf52 3410 }
arsenalist 51:ad6410dcaf52 3411 allowAlertCWatt = false;
arsenalist 4:76ab12e2f8a3 3412 }
arsenalist 4:76ab12e2f8a3 3413 //DBG.printf("checkVoltagePower-finish\r\n");
arsenalist 4:76ab12e2f8a3 3414 }
arsenalist 48:053e92b6dc1e 3415 /*end energy related*/
arsenalist 47:97eadcb478b2 3416
arsenalist 48:053e92b6dc1e 3417 /*start eth rest*/
arsenalist 48:053e92b6dc1e 3418 string ethREST(string host, int port, string data) {
arsenalist 48:053e92b6dc1e 3419 char buf[1024];
arsenalist 47:97eadcb478b2 3420 char s[256];
arsenalist 48:053e92b6dc1e 3421 int ret=99;
arsenalist 48:053e92b6dc1e 3422 Timer t;
arsenalist 47:97eadcb478b2 3423
arsenalist 48:053e92b6dc1e 3424 t.start();
arsenalist 48:053e92b6dc1e 3425 while(eth.linkstatus() && t.read() < 5 && ret !=0) {
arsenalist 48:053e92b6dc1e 3426 ret = sock.connect(host.c_str(),port);
arsenalist 48:053e92b6dc1e 3427 }
arsenalist 48:053e92b6dc1e 3428 t.stop();
arsenalist 48:053e92b6dc1e 3429 t.reset();
arsenalist 47:97eadcb478b2 3430
arsenalist 48:053e92b6dc1e 3431 memset(buf,0,sizeof(buf));
arsenalist 48:053e92b6dc1e 3432 if(sock.is_connected()) {
arsenalist 48:053e92b6dc1e 3433 DBG.printf("sockConnect\r\n");
arsenalist 48:053e92b6dc1e 3434 sprintf(s,"%s",data.c_str());
arsenalist 48:053e92b6dc1e 3435 sock.send_all(s,sizeof(s)-1);
arsenalist 47:97eadcb478b2 3436 //wait(2);
arsenalist 48:053e92b6dc1e 3437 wait(0.5);
arsenalist 47:97eadcb478b2 3438
arsenalist 48:053e92b6dc1e 3439 //receive return
arsenalist 48:053e92b6dc1e 3440 t.start();
arsenalist 48:053e92b6dc1e 3441 while(1) {
arsenalist 48:053e92b6dc1e 3442 ret = sock.receive(buf, sizeof(buf));
arsenalist 48:053e92b6dc1e 3443 if(ret<=0 || t.read() > 5) {
arsenalist 48:053e92b6dc1e 3444 t.stop();
arsenalist 48:053e92b6dc1e 3445 t.reset();
arsenalist 48:053e92b6dc1e 3446 break;
arsenalist 48:053e92b6dc1e 3447 }
arsenalist 48:053e92b6dc1e 3448 }
arsenalist 48:053e92b6dc1e 3449 sock.close();
arsenalist 48:053e92b6dc1e 3450 } else {
arsenalist 48:053e92b6dc1e 3451 DBG.printf("sockNotConnect\r\n");
arsenalist 47:97eadcb478b2 3452 }
arsenalist 48:053e92b6dc1e 3453 return buf;
arsenalist 48:053e92b6dc1e 3454
arsenalist 48:053e92b6dc1e 3455 /*
arsenalist 48:053e92b6dc1e 3456 //eth.init(); //use DHCP
arsenalist 48:053e92b6dc1e 3457 eth.connect();
arsenalist 47:97eadcb478b2 3458
arsenalist 48:053e92b6dc1e 3459 //TCPSocketConnection sock;
arsenalist 48:053e92b6dc1e 3460 Timer t;
arsenalist 48:053e92b6dc1e 3461
arsenalist 48:053e92b6dc1e 3462 sprintf(s,"%s",data.c_str());
arsenalist 48:053e92b6dc1e 3463 sock.connect(host.c_str(),port);
arsenalist 48:053e92b6dc1e 3464 sock.send_all(s,sizeof(s)-1);
arsenalist 48:053e92b6dc1e 3465 wait(2);
arsenalist 48:053e92b6dc1e 3466
arsenalist 48:053e92b6dc1e 3467 //receive return
arsenalist 48:053e92b6dc1e 3468 t.start();
arsenalist 48:053e92b6dc1e 3469 while(1) {
arsenalist 48:053e92b6dc1e 3470 ret = sock.receive(buf, sizeof(buf));
arsenalist 48:053e92b6dc1e 3471 if(ret<=0 || t.read_ms() > 10000) {
arsenalist 48:053e92b6dc1e 3472 t.stop();
arsenalist 48:053e92b6dc1e 3473 break;
arsenalist 48:053e92b6dc1e 3474 }
arsenalist 47:97eadcb478b2 3475 }
arsenalist 48:053e92b6dc1e 3476 sock.close();
arsenalist 48:053e92b6dc1e 3477 eth.disconnect();
arsenalist 48:053e92b6dc1e 3478 return buf;
arsenalist 48:053e92b6dc1e 3479 */
arsenalist 47:97eadcb478b2 3480 }
arsenalist 48:053e92b6dc1e 3481 /*end eth rest*/
arsenalist 0:f4e449fa34d7 3482
arsenalist 0:f4e449fa34d7 3483 /*start wifi mqtt*/
arsenalist 0:f4e449fa34d7 3484 void mqttConnected(void* response) {
arsenalist 1:d314a43ae6f7 3485 DBG.printf("MQTT Connected\r\n");
arsenalist 0:f4e449fa34d7 3486 char mqttTopic[64];
arsenalist 38:e44f35f8129d 3487 sprintf(mqttTopic,"%s/%s/command",mqttDOMAIN.c_str(),emmaUID.c_str());
arsenalist 27:562a95c120cc 3488 //mqtt.subscribe(mqttTopic);
arsenalist 0:f4e449fa34d7 3489 }
arsenalist 0:f4e449fa34d7 3490 void mqttDisconnected(void* response) {
arsenalist 1:d314a43ae6f7 3491 DBG.printf("MQTT Disconnected\r\n");
arsenalist 0:f4e449fa34d7 3492 }
arsenalist 0:f4e449fa34d7 3493 /*end wifi mqtt*/
arsenalist 0:f4e449fa34d7 3494
arsenalist 0:f4e449fa34d7 3495 /*start wifi rest*/
arsenalist 27:562a95c120cc 3496 void wifiCb(void* response) {
arsenalist 27:562a95c120cc 3497 uint32_t status;
arsenalist 27:562a95c120cc 3498 RESPONSE res(response);
arsenalist 27:562a95c120cc 3499
arsenalist 27:562a95c120cc 3500 if(res.getArgc() == 1) {
arsenalist 27:562a95c120cc 3501 res.popArgs((uint8_t*)&status,4);
arsenalist 27:562a95c120cc 3502 if(status == STATION_GOT_IP) {
arsenalist 27:562a95c120cc 3503 DBG.printf("WIFI Connected\r\n");
arsenalist 27:562a95c120cc 3504 //wifiConnected = true;
arsenalist 27:562a95c120cc 3505 }
arsenalist 27:562a95c120cc 3506 else {
arsenalist 27:562a95c120cc 3507 //wifiConnected = false;
arsenalist 27:562a95c120cc 3508 }
arsenalist 27:562a95c120cc 3509 }
arsenalist 27:562a95c120cc 3510 }
arsenalist 27:562a95c120cc 3511
arsenalist 4:76ab12e2f8a3 3512 void rxInterrupt(void) {
arsenalist 4:76ab12e2f8a3 3513 char c;
arsenalist 4:76ab12e2f8a3 3514
arsenalist 4:76ab12e2f8a3 3515 while(_ESP.readable()) {
arsenalist 4:76ab12e2f8a3 3516 c = _ESP.getc();
arsenalist 4:76ab12e2f8a3 3517 if(c != 0) { //char is not null
arsenalist 4:76ab12e2f8a3 3518 rxBuf += c;
arsenalist 4:76ab12e2f8a3 3519 }
arsenalist 4:76ab12e2f8a3 3520 }
arsenalist 4:76ab12e2f8a3 3521 }
arsenalist 4:76ab12e2f8a3 3522 void checkRxBuffer(void) {
arsenalist 4:76ab12e2f8a3 3523 //check new command
arsenalist 4:76ab12e2f8a3 3524 if(rxBuf.rfind("/command") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 3525 rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.rfind("/command"));
arsenalist 41:2d023bd80a9c 3526 if(rxBuf.find("[{") != std::string::npos && rxBuf.rfind("}]") != std::string::npos) {
arsenalist 41:2d023bd80a9c 3527 rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.find("[{"));
arsenalist 41:2d023bd80a9c 3528 rxBuf.erase(rxBuf.begin()+rxBuf.rfind("}]")+2,rxBuf.end());
arsenalist 41:2d023bd80a9c 3529
arsenalist 41:2d023bd80a9c 3530 //start special handler
arsenalist 41:2d023bd80a9c 3531 while(1){
arsenalist 41:2d023bd80a9c 3532 if(rxBuf.find("}],") != std::string::npos) {
arsenalist 41:2d023bd80a9c 3533 rxBuf.erase(rxBuf.begin()+rxBuf.find("}],")+1,rxBuf.begin()+rxBuf.find("}],")+2);
arsenalist 41:2d023bd80a9c 3534 } else {
arsenalist 41:2d023bd80a9c 3535 break;
arsenalist 41:2d023bd80a9c 3536 }
arsenalist 41:2d023bd80a9c 3537 }
arsenalist 41:2d023bd80a9c 3538 //end special handler
arsenalist 41:2d023bd80a9c 3539
arsenalist 4:76ab12e2f8a3 3540 globalCommand = rxBuf;
arsenalist 4:76ab12e2f8a3 3541 newCommand = true;
arsenalist 4:76ab12e2f8a3 3542 }
arsenalist 4:76ab12e2f8a3 3543 }
arsenalist 4:76ab12e2f8a3 3544
arsenalist 24:5d58515ba510 3545 //check free memory -> reinitialize mqtt connection
arsenalist 27:562a95c120cc 3546 if(rxBuf.rfind("Free memory") != std::string::npos) {
arsenalist 27:562a95c120cc 3547 rxLog = "Free memory-" + rxBuf;
arsenalist 24:5d58515ba510 3548 espFreeMemory = true;
arsenalist 24:5d58515ba510 3549 }
arsenalist 24:5d58515ba510 3550
arsenalist 27:562a95c120cc 3551 //check dhcp client start -> initialize all connection
arsenalist 27:562a95c120cc 3552 if(rxBuf.rfind("dhcp client start") != std::string::npos) {
arsenalist 27:562a95c120cc 3553 rxLogA = "dhcp client start-" + rxBuf;
arsenalist 27:562a95c120cc 3554 espDHCPClientStart = true;
arsenalist 27:562a95c120cc 3555 }
arsenalist 27:562a95c120cc 3556
arsenalist 4:76ab12e2f8a3 3557 //clear rxBuf
arsenalist 4:76ab12e2f8a3 3558 rxBuf.clear();
arsenalist 4:76ab12e2f8a3 3559 }
arsenalist 48:053e92b6dc1e 3560 void wifiRcvReply(char *r, int to) {
arsenalist 48:053e92b6dc1e 3561 Timer t;
arsenalist 48:053e92b6dc1e 3562 bool ended = false;
arsenalist 48:053e92b6dc1e 3563 char c;
arsenalist 48:053e92b6dc1e 3564
arsenalist 48:053e92b6dc1e 3565 strcpy(r,"");
arsenalist 48:053e92b6dc1e 3566 t.start();
arsenalist 48:053e92b6dc1e 3567 while(!ended) {
arsenalist 48:053e92b6dc1e 3568 if(_ESP.readable()) {
arsenalist 48:053e92b6dc1e 3569 c = _ESP.getc();
arsenalist 48:053e92b6dc1e 3570 addChar(r,c);
arsenalist 48:053e92b6dc1e 3571 t.start();
arsenalist 48:053e92b6dc1e 3572 }
arsenalist 48:053e92b6dc1e 3573 if(t.read_ms() > to) {
arsenalist 48:053e92b6dc1e 3574 ended = true;
arsenalist 48:053e92b6dc1e 3575 }
arsenalist 48:053e92b6dc1e 3576 }
arsenalist 48:053e92b6dc1e 3577 addChar(r, 0x00);
arsenalist 48:053e92b6dc1e 3578 }
arsenalist 0:f4e449fa34d7 3579 /*end wifi rest*/
arsenalist 0:f4e449fa34d7 3580
arsenalist 48:053e92b6dc1e 3581 /*start gprs rest*/
arsenalist 48:053e92b6dc1e 3582 void gprsReset(void) {
arsenalist 48:053e92b6dc1e 3583 pwrKey = 0;
arsenalist 48:053e92b6dc1e 3584 wait(1);
arsenalist 48:053e92b6dc1e 3585 pwrKey = 1;
arsenalist 48:053e92b6dc1e 3586 wait(2);
arsenalist 48:053e92b6dc1e 3587 pwrKey = 0;
arsenalist 48:053e92b6dc1e 3588 wait(2);
arsenalist 48:053e92b6dc1e 3589 }
arsenalist 48:053e92b6dc1e 3590 string gprsInit(string APN) {
arsenalist 48:053e92b6dc1e 3591 char r[128];
arsenalist 48:053e92b6dc1e 3592 char s[128];
arsenalist 48:053e92b6dc1e 3593 int n=0;
arsenalist 48:053e92b6dc1e 3594 string str;
arsenalist 48:053e92b6dc1e 3595
arsenalist 48:053e92b6dc1e 3596 while(1) {
arsenalist 48:053e92b6dc1e 3597 sim900.printf("AT+CIPMUX=1\r\n");
arsenalist 48:053e92b6dc1e 3598 gprsRcvReply(r,3000);
arsenalist 48:053e92b6dc1e 3599 //DBG.printf("rspn[CIPMUX]:%s\r\n",r);
arsenalist 48:053e92b6dc1e 3600 DBG.printf("AT+CIPMUX\r\n");
arsenalist 48:053e92b6dc1e 3601 str = r;
arsenalist 48:053e92b6dc1e 3602 if(str.find("OK") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3603 break;
arsenalist 48:053e92b6dc1e 3604 }
arsenalist 48:053e92b6dc1e 3605 if(n>=3) {
arsenalist 48:053e92b6dc1e 3606 return "ERROR";
arsenalist 48:053e92b6dc1e 3607 }
arsenalist 48:053e92b6dc1e 3608 n++;
arsenalist 48:053e92b6dc1e 3609 }
arsenalist 48:053e92b6dc1e 3610
arsenalist 48:053e92b6dc1e 3611 if(!APN.compare("Telkomsel")) {
arsenalist 48:053e92b6dc1e 3612 strcpy(s,"AT+CSTT=\"telkomsel\",\"wap\",\"wap123\"\r\n");
arsenalist 48:053e92b6dc1e 3613 } else if(!APN.compare("Indosat")) {
arsenalist 48:053e92b6dc1e 3614 strcpy(s,"AT+CSTT=\"indosatgprs\",\"indosat\",\"indosat\"\r\n");
arsenalist 48:053e92b6dc1e 3615 } else if(!APN.compare("XL")) {
arsenalist 48:053e92b6dc1e 3616 strcpy(s,"AT+CSTT=\"www.xlgprs.net\",\"xlgprs\",\"proxl\"\r\n");
arsenalist 48:053e92b6dc1e 3617 } else if(!APN.compare("3")) {
arsenalist 48:053e92b6dc1e 3618 strcpy(s,"AT+CSTT=\"3gprs\",\"3gprs\",\"3gprs\"\r\n");
arsenalist 48:053e92b6dc1e 3619 } else {
arsenalist 48:053e92b6dc1e 3620 return "ERROR";
arsenalist 48:053e92b6dc1e 3621 }
arsenalist 48:053e92b6dc1e 3622
arsenalist 48:053e92b6dc1e 3623 n=0;
arsenalist 48:053e92b6dc1e 3624 while(1) {
arsenalist 48:053e92b6dc1e 3625 sim900.printf("%s",s);
arsenalist 48:053e92b6dc1e 3626 gprsRcvReply(r,5000);
arsenalist 48:053e92b6dc1e 3627 //DBG.printf("rspn[CSTT]:%s\r\n",r);
arsenalist 48:053e92b6dc1e 3628 DBG.printf("AT+CSTT\r\n");
arsenalist 48:053e92b6dc1e 3629 str = r;
arsenalist 48:053e92b6dc1e 3630 if(str.find("OK") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3631 break;
arsenalist 48:053e92b6dc1e 3632 }
arsenalist 48:053e92b6dc1e 3633 if(n>=3) {
arsenalist 48:053e92b6dc1e 3634 return "ERROR";
arsenalist 48:053e92b6dc1e 3635 }
arsenalist 48:053e92b6dc1e 3636 n++;
arsenalist 48:053e92b6dc1e 3637 }
arsenalist 48:053e92b6dc1e 3638
arsenalist 48:053e92b6dc1e 3639 n=0;
arsenalist 48:053e92b6dc1e 3640 while(1) {
arsenalist 48:053e92b6dc1e 3641 sim900.printf("AT+CIICR\r\n");
arsenalist 48:053e92b6dc1e 3642 gprsRcvReply(r,3000);
arsenalist 48:053e92b6dc1e 3643 //DBG.printf("rspn[CIICR]:%s\r\n",r);
arsenalist 48:053e92b6dc1e 3644 DBG.printf("AT+CIICR\r\n");
arsenalist 48:053e92b6dc1e 3645 str = r;
arsenalist 48:053e92b6dc1e 3646 if(str.find("OK") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3647 break;
arsenalist 48:053e92b6dc1e 3648 }
arsenalist 48:053e92b6dc1e 3649 if(n>=3) {
arsenalist 48:053e92b6dc1e 3650 return "ERROR";
arsenalist 48:053e92b6dc1e 3651 }
arsenalist 48:053e92b6dc1e 3652 n++;
arsenalist 48:053e92b6dc1e 3653 }
arsenalist 48:053e92b6dc1e 3654
arsenalist 48:053e92b6dc1e 3655 while(1) {
arsenalist 48:053e92b6dc1e 3656 sim900.printf("AT+CIFSR\r\n");
arsenalist 48:053e92b6dc1e 3657 gprsRcvReply(r,3000);
arsenalist 48:053e92b6dc1e 3658 //DBG.printf("rspn[CIFSR]:%s\r\n",r);
arsenalist 48:053e92b6dc1e 3659 DBG.printf("AT+CIFSR\r\n");
arsenalist 48:053e92b6dc1e 3660 str = r;
arsenalist 48:053e92b6dc1e 3661 if(str.find("ERROR") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3662 return "ERROR";
arsenalist 48:053e92b6dc1e 3663 } else {
arsenalist 48:053e92b6dc1e 3664 return str;
arsenalist 48:053e92b6dc1e 3665 }
arsenalist 48:053e92b6dc1e 3666 }
arsenalist 48:053e92b6dc1e 3667 }
arsenalist 48:053e92b6dc1e 3668 string gprsREST(string host, int port, string data) {
arsenalist 48:053e92b6dc1e 3669 char r[128];
arsenalist 48:053e92b6dc1e 3670 char rLong[2048];
arsenalist 48:053e92b6dc1e 3671 int n=0;
arsenalist 48:053e92b6dc1e 3672 string str;
arsenalist 48:053e92b6dc1e 3673
arsenalist 48:053e92b6dc1e 3674 //AT+CIPSTART
arsenalist 48:053e92b6dc1e 3675 while(1) {
arsenalist 48:053e92b6dc1e 3676 sim900.printf("AT+CIPSTART=1,\"TCP\",\"%s\",%d\r\n",host.c_str(),port);
arsenalist 48:053e92b6dc1e 3677 gprsRcvReply(r,3000);
arsenalist 48:053e92b6dc1e 3678 //DBG.printf("rspn[CIPSTART]:%s",r);
arsenalist 48:053e92b6dc1e 3679 DBG.printf("[CIPSTART]\r\n");
arsenalist 48:053e92b6dc1e 3680 str = r;
arsenalist 48:053e92b6dc1e 3681 if(str.find("CONNECT OK") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3682 break;
arsenalist 48:053e92b6dc1e 3683 }
arsenalist 48:053e92b6dc1e 3684 if(n>5) {
arsenalist 48:053e92b6dc1e 3685 return "ERROR:CIPSTART"; //unable to start connection
arsenalist 48:053e92b6dc1e 3686 }
arsenalist 48:053e92b6dc1e 3687 n++;
arsenalist 48:053e92b6dc1e 3688 }
arsenalist 48:053e92b6dc1e 3689
arsenalist 48:053e92b6dc1e 3690 //AT+CIPSEND
arsenalist 48:053e92b6dc1e 3691 sim900.printf("AT+CIPSEND=1,%d\r\n",data.length());
arsenalist 48:053e92b6dc1e 3692 gprsRcvReply(r,1000);
arsenalist 48:053e92b6dc1e 3693 //DBG.printf("rspn[CIPSEND]:%s",r);
arsenalist 48:053e92b6dc1e 3694 DBG.printf("[CIPSEND]\r\n");
arsenalist 48:053e92b6dc1e 3695
arsenalist 48:053e92b6dc1e 3696 str = r;
arsenalist 48:053e92b6dc1e 3697 if(str.find(">") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3698 sim900.printf("%s",data.c_str());
arsenalist 48:053e92b6dc1e 3699 gprsRcvReply(rLong,10000);
arsenalist 48:053e92b6dc1e 3700 //DBG.printf("rspn[>]:%s\r\n",rLong);
arsenalist 48:053e92b6dc1e 3701 DBG.printf("[>]\r\n");
arsenalist 48:053e92b6dc1e 3702 }
arsenalist 48:053e92b6dc1e 3703 else {
arsenalist 48:053e92b6dc1e 3704 return "ERROR:>"; //unable to send data
arsenalist 48:053e92b6dc1e 3705 }
arsenalist 48:053e92b6dc1e 3706
arsenalist 48:053e92b6dc1e 3707 //close connection (case for get request)
arsenalist 48:053e92b6dc1e 3708 str = rLong;
arsenalist 48:053e92b6dc1e 3709 if(str.find("CLOSED") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3710 return rLong;
arsenalist 48:053e92b6dc1e 3711 }
arsenalist 48:053e92b6dc1e 3712
arsenalist 48:053e92b6dc1e 3713 //close connection
arsenalist 48:053e92b6dc1e 3714 n=0;
arsenalist 48:053e92b6dc1e 3715 while(1) {
arsenalist 48:053e92b6dc1e 3716 sim900.printf("AT+CIPCLOSE=1,1\r\n");
arsenalist 48:053e92b6dc1e 3717 gprsRcvReply(r,3000);
arsenalist 48:053e92b6dc1e 3718 //DBG.printf("rspn[CIPCLOSE]:%s\r\n",r);
arsenalist 48:053e92b6dc1e 3719 DBG.printf("[CIPCLOSE]\r\n");
arsenalist 48:053e92b6dc1e 3720 str = r;
arsenalist 48:053e92b6dc1e 3721 if(str.find("CLOSE OK") != std::string::npos) {
arsenalist 48:053e92b6dc1e 3722 break;
arsenalist 48:053e92b6dc1e 3723 }
arsenalist 49:d11a1914ee8b 3724 if(n>3) {
arsenalist 49:d11a1914ee8b 3725 //return "ERROR:CIPCLOSE"; //unable to close connection
arsenalist 49:d11a1914ee8b 3726 return rLong; //assumption if unable to close connection because it already been closed
arsenalist 48:053e92b6dc1e 3727 }
arsenalist 48:053e92b6dc1e 3728 n++;
arsenalist 48:053e92b6dc1e 3729 }
arsenalist 48:053e92b6dc1e 3730 return rLong;
arsenalist 48:053e92b6dc1e 3731 }
arsenalist 48:053e92b6dc1e 3732 void gprsRcvReply(char *r, int to) {
arsenalist 48:053e92b6dc1e 3733 Timer t;
arsenalist 48:053e92b6dc1e 3734 bool ended = false;
arsenalist 48:053e92b6dc1e 3735 char c;
arsenalist 48:053e92b6dc1e 3736
arsenalist 48:053e92b6dc1e 3737 strcpy(r,"");
arsenalist 48:053e92b6dc1e 3738 t.start();
arsenalist 48:053e92b6dc1e 3739 while(!ended) {
arsenalist 48:053e92b6dc1e 3740 if(sim900.readable()) {
arsenalist 48:053e92b6dc1e 3741 c = sim900.getc();
arsenalist 48:053e92b6dc1e 3742 addChar(r,c);
arsenalist 48:053e92b6dc1e 3743 t.start();
arsenalist 48:053e92b6dc1e 3744 }
arsenalist 48:053e92b6dc1e 3745 if(t.read_ms() > to) {
arsenalist 48:053e92b6dc1e 3746 ended = true;
arsenalist 48:053e92b6dc1e 3747 }
arsenalist 48:053e92b6dc1e 3748 }
arsenalist 48:053e92b6dc1e 3749 addChar(r, 0x00);
arsenalist 48:053e92b6dc1e 3750 }
arsenalist 48:053e92b6dc1e 3751
arsenalist 48:053e92b6dc1e 3752 /*end gprs rest*/
arsenalist 48:053e92b6dc1e 3753
arsenalist 0:f4e449fa34d7 3754 /*start emma settings*/
arsenalist 0:f4e449fa34d7 3755 string getUID(void) {
arsenalist 0:f4e449fa34d7 3756 char s[32];
arsenalist 0:f4e449fa34d7 3757 unsigned long *unique = (unsigned long *)BASE_ADDR;
arsenalist 0:f4e449fa34d7 3758 sprintf(s,"%08x%08x%08x",unique[0], unique[1], unique[2]);
arsenalist 0:f4e449fa34d7 3759 return s;
arsenalist 0:f4e449fa34d7 3760 }
arsenalist 0:f4e449fa34d7 3761
arsenalist 0:f4e449fa34d7 3762 string readSetting(string parameter) {
arsenalist 0:f4e449fa34d7 3763 FILE *fp;
arsenalist 0:f4e449fa34d7 3764 signed char c;
arsenalist 0:f4e449fa34d7 3765 int i=0;
arsenalist 0:f4e449fa34d7 3766 char s[64];
arsenalist 0:f4e449fa34d7 3767 string strS;
arsenalist 0:f4e449fa34d7 3768
arsenalist 0:f4e449fa34d7 3769 sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
arsenalist 0:f4e449fa34d7 3770
arsenalist 0:f4e449fa34d7 3771 fp = fopen(s,"r");
arsenalist 0:f4e449fa34d7 3772 memset(s,0,sizeof(s));
arsenalist 0:f4e449fa34d7 3773 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 3774 while(1) {
arsenalist 0:f4e449fa34d7 3775 c = fgetc(fp);
arsenalist 0:f4e449fa34d7 3776 if(c == EOF){
arsenalist 0:f4e449fa34d7 3777 break;
arsenalist 0:f4e449fa34d7 3778 }
arsenalist 0:f4e449fa34d7 3779 s[i] = c;
arsenalist 0:f4e449fa34d7 3780 i++;
arsenalist 0:f4e449fa34d7 3781 }
arsenalist 0:f4e449fa34d7 3782 strS = s;
arsenalist 0:f4e449fa34d7 3783 if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 3784 strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
arsenalist 0:f4e449fa34d7 3785 strS.erase(strS.begin()+strS.find(")"),strS.end());
arsenalist 0:f4e449fa34d7 3786 } else {
arsenalist 0:f4e449fa34d7 3787 strS = "";
arsenalist 0:f4e449fa34d7 3788 }
arsenalist 0:f4e449fa34d7 3789 }
arsenalist 0:f4e449fa34d7 3790 fclose(fp);
arsenalist 30:08d290158fa7 3791 free(fp);
arsenalist 0:f4e449fa34d7 3792 return strS;
arsenalist 0:f4e449fa34d7 3793 }
arsenalist 0:f4e449fa34d7 3794
arsenalist 0:f4e449fa34d7 3795 bool writeSetting(string parameter, string value) {
arsenalist 0:f4e449fa34d7 3796 FILE *fp;
arsenalist 0:f4e449fa34d7 3797 char s[255];
arsenalist 0:f4e449fa34d7 3798
arsenalist 0:f4e449fa34d7 3799 sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
arsenalist 0:f4e449fa34d7 3800 fp = fopen(s,"w");
arsenalist 0:f4e449fa34d7 3801 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 3802 fprintf(fp,value.c_str());
arsenalist 0:f4e449fa34d7 3803 fclose(fp);
arsenalist 30:08d290158fa7 3804 free(fp);
arsenalist 0:f4e449fa34d7 3805 return true;
arsenalist 0:f4e449fa34d7 3806 }
arsenalist 30:08d290158fa7 3807 fclose(fp);
arsenalist 30:08d290158fa7 3808 free(fp);
arsenalist 0:f4e449fa34d7 3809 return false;
arsenalist 0:f4e449fa34d7 3810 }
arsenalist 0:f4e449fa34d7 3811 /*end emma settings*/
arsenalist 0:f4e449fa34d7 3812
arsenalist 0:f4e449fa34d7 3813 /*start emma node*/
arsenalist 0:f4e449fa34d7 3814 string readNodeIP(string macAddr) {
arsenalist 0:f4e449fa34d7 3815 FILE *fp;
arsenalist 0:f4e449fa34d7 3816 signed char c;
arsenalist 0:f4e449fa34d7 3817 int i=0;
arsenalist 0:f4e449fa34d7 3818 char s[64];
arsenalist 0:f4e449fa34d7 3819 string strS;
arsenalist 0:f4e449fa34d7 3820
arsenalist 0:f4e449fa34d7 3821 sprintf(s,"/sd/nodeList/%s/nodeIP.txt",macAddr.c_str());
arsenalist 0:f4e449fa34d7 3822
arsenalist 0:f4e449fa34d7 3823 fp = fopen(s,"r");
arsenalist 0:f4e449fa34d7 3824 memset(s,0,sizeof(s));
arsenalist 0:f4e449fa34d7 3825 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 3826 while(1) {
arsenalist 0:f4e449fa34d7 3827 c = fgetc(fp);
arsenalist 0:f4e449fa34d7 3828 if(c == EOF){
arsenalist 0:f4e449fa34d7 3829 break;
arsenalist 0:f4e449fa34d7 3830 }
arsenalist 0:f4e449fa34d7 3831 s[i] = c;
arsenalist 0:f4e449fa34d7 3832 i++;
arsenalist 0:f4e449fa34d7 3833 }
arsenalist 0:f4e449fa34d7 3834 strS = s;
arsenalist 0:f4e449fa34d7 3835 if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 3836 strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
arsenalist 0:f4e449fa34d7 3837 strS.erase(strS.begin()+strS.find(")"),strS.end());
arsenalist 0:f4e449fa34d7 3838 } else {
arsenalist 0:f4e449fa34d7 3839 strS = "";
arsenalist 0:f4e449fa34d7 3840 }
arsenalist 0:f4e449fa34d7 3841 }
arsenalist 0:f4e449fa34d7 3842 fclose(fp);
arsenalist 30:08d290158fa7 3843 free(fp);
arsenalist 0:f4e449fa34d7 3844 return strS;
arsenalist 0:f4e449fa34d7 3845 }
arsenalist 0:f4e449fa34d7 3846
arsenalist 0:f4e449fa34d7 3847 string readNodeCmd(string dType, string cmd) {
arsenalist 0:f4e449fa34d7 3848 FILE *fp;
arsenalist 0:f4e449fa34d7 3849 signed char c;
arsenalist 0:f4e449fa34d7 3850 int i=0;
arsenalist 34:8555d471b880 3851 char s[2048];
arsenalist 0:f4e449fa34d7 3852 string strS;
arsenalist 0:f4e449fa34d7 3853
arsenalist 0:f4e449fa34d7 3854 sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmd.c_str());
arsenalist 0:f4e449fa34d7 3855
arsenalist 0:f4e449fa34d7 3856 fp = fopen(s,"r");
arsenalist 0:f4e449fa34d7 3857 memset(s,0,sizeof(s));
arsenalist 0:f4e449fa34d7 3858 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 3859 while(1) {
arsenalist 0:f4e449fa34d7 3860 c = fgetc(fp);
arsenalist 0:f4e449fa34d7 3861 if(c == EOF){
arsenalist 0:f4e449fa34d7 3862 break;
arsenalist 0:f4e449fa34d7 3863 }
arsenalist 0:f4e449fa34d7 3864 s[i] = c;
arsenalist 0:f4e449fa34d7 3865 i++;
arsenalist 0:f4e449fa34d7 3866 }
arsenalist 0:f4e449fa34d7 3867 strS = s;
arsenalist 0:f4e449fa34d7 3868 if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 3869 strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
arsenalist 0:f4e449fa34d7 3870 strS.erase(strS.begin()+strS.find(")"),strS.end());
arsenalist 0:f4e449fa34d7 3871 } else {
arsenalist 0:f4e449fa34d7 3872 strS = "";
arsenalist 0:f4e449fa34d7 3873 }
arsenalist 0:f4e449fa34d7 3874 }
arsenalist 0:f4e449fa34d7 3875 fclose(fp);
arsenalist 30:08d290158fa7 3876 free(fp);
arsenalist 0:f4e449fa34d7 3877 return strS;
arsenalist 0:f4e449fa34d7 3878 }
arsenalist 0:f4e449fa34d7 3879
arsenalist 0:f4e449fa34d7 3880 string *readNodeList(void) {
arsenalist 0:f4e449fa34d7 3881 static string nd[10]; //max node
arsenalist 0:f4e449fa34d7 3882 DIR *d;
arsenalist 0:f4e449fa34d7 3883 struct dirent *p;
arsenalist 0:f4e449fa34d7 3884 string q;
arsenalist 0:f4e449fa34d7 3885 int i=0;
arsenalist 0:f4e449fa34d7 3886
arsenalist 0:f4e449fa34d7 3887 d = opendir("/sd/nodeList");
arsenalist 0:f4e449fa34d7 3888 if(d != NULL) {
arsenalist 0:f4e449fa34d7 3889 while((p = readdir(d)) != NULL) {
arsenalist 0:f4e449fa34d7 3890 q = p->d_name;
arsenalist 0:f4e449fa34d7 3891 nd[i] = q;
arsenalist 0:f4e449fa34d7 3892 i++;
arsenalist 0:f4e449fa34d7 3893 }
arsenalist 0:f4e449fa34d7 3894 }
arsenalist 0:f4e449fa34d7 3895 closedir(d);
arsenalist 30:08d290158fa7 3896 free(d);
arsenalist 0:f4e449fa34d7 3897 return nd;
arsenalist 0:f4e449fa34d7 3898 }
arsenalist 0:f4e449fa34d7 3899
arsenalist 0:f4e449fa34d7 3900 string wifiGetNodeTemp(string macAddr) {
arsenalist 0:f4e449fa34d7 3901 int trial=0;
arsenalist 0:f4e449fa34d7 3902 string nodeIP = readNodeIP(macAddr);
arsenalist 0:f4e449fa34d7 3903 string str;
arsenalist 0:f4e449fa34d7 3904 string temp = "0";
arsenalist 0:f4e449fa34d7 3905
arsenalist 0:f4e449fa34d7 3906 if(rest.begin(nodeIP.c_str(),REMOTE_TCP_PORT,false)) {
arsenalist 0:f4e449fa34d7 3907 while(1) {
arsenalist 0:f4e449fa34d7 3908 char rcv[256] = {};
arsenalist 0:f4e449fa34d7 3909 rest.get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
arsenalist 0:f4e449fa34d7 3910 rest.getResponse(rcv,sizeof(rcv));
arsenalist 0:f4e449fa34d7 3911 str = rcv;
arsenalist 0:f4e449fa34d7 3912 if(str.find("temp=") != std::string::npos) {
arsenalist 0:f4e449fa34d7 3913 str.erase(str.begin(),str.begin()+str.find("temp=")+6);
arsenalist 0:f4e449fa34d7 3914 str.erase(str.begin()+str.find("\""),str.end());
arsenalist 0:f4e449fa34d7 3915 temp = str;
arsenalist 0:f4e449fa34d7 3916 break;
arsenalist 0:f4e449fa34d7 3917 }
arsenalist 0:f4e449fa34d7 3918 if(trial>1) { //three times trial
arsenalist 0:f4e449fa34d7 3919 break;
arsenalist 0:f4e449fa34d7 3920 }
arsenalist 0:f4e449fa34d7 3921 trial++;
arsenalist 0:f4e449fa34d7 3922 wait(2);
arsenalist 0:f4e449fa34d7 3923 }
arsenalist 0:f4e449fa34d7 3924 }
arsenalist 0:f4e449fa34d7 3925 return temp;
arsenalist 0:f4e449fa34d7 3926 }
arsenalist 0:f4e449fa34d7 3927 /*end emma node*/
arsenalist 0:f4e449fa34d7 3928
arsenalist 0:f4e449fa34d7 3929 /*start emma private function*/
arsenalist 8:51a0ca9079ca 3930 void isEthAvailable(void) {
arsenalist 44:c1d11c491237 3931 //if(ipstack.getEth().linkstatus()) {
arsenalist 44:c1d11c491237 3932 if(eth.linkstatus()) {
arsenalist 8:51a0ca9079ca 3933 ethAvailable = true;
arsenalist 8:51a0ca9079ca 3934 } else {
arsenalist 8:51a0ca9079ca 3935 ethAvailable = false;
arsenalist 8:51a0ca9079ca 3936 }
arsenalist 8:51a0ca9079ca 3937 }
arsenalist 8:51a0ca9079ca 3938
arsenalist 8:51a0ca9079ca 3939 void isEthConnected(void) {
arsenalist 8:51a0ca9079ca 3940 char s[512];
arsenalist 8:51a0ca9079ca 3941 int connPort;
arsenalist 8:51a0ca9079ca 3942 string connHost;
arsenalist 8:51a0ca9079ca 3943 string str;
arsenalist 8:51a0ca9079ca 3944 Timer t;
arsenalist 8:51a0ca9079ca 3945
arsenalist 45:2406c2b6995a 3946 eth.init(); //init ethernet
arsenalist 45:2406c2b6995a 3947 eth.connect();
arsenalist 45:2406c2b6995a 3948
arsenalist 8:51a0ca9079ca 3949 if(ethAvailable) {
arsenalist 8:51a0ca9079ca 3950 if(useProxy) {
arsenalist 8:51a0ca9079ca 3951 connHost = proxySERVER;
arsenalist 50:50d98fe6cc15 3952 connPort = proxyPORT;
arsenalist 44:c1d11c491237 3953 //for(int i=0; i<sizeof(s); i++) {
arsenalist 44:c1d11c491237 3954 // s[i]=0; }
arsenalist 50:50d98fe6cc15 3955 sprintf(s,"GET http://%s:%d/%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str());
arsenalist 8:51a0ca9079ca 3956 } else {
arsenalist 38:e44f35f8129d 3957 connHost = restSERVER;
arsenalist 50:50d98fe6cc15 3958 connPort = restPORT;
arsenalist 44:c1d11c491237 3959 //for(int i=0; i<sizeof(s); i++) {
arsenalist 44:c1d11c491237 3960 // s[i]=0; }
arsenalist 43:612547648ed1 3961 sprintf(s,"GET /%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",appNAME.c_str(),restSERVER.c_str());
arsenalist 8:51a0ca9079ca 3962 }
arsenalist 8:51a0ca9079ca 3963
arsenalist 8:51a0ca9079ca 3964 t.start();
arsenalist 8:51a0ca9079ca 3965 while(1) {
arsenalist 40:77bd44f57ad3 3966 str = ethREST(connHost,connPort,s);
arsenalist 44:c1d11c491237 3967 if(str.find("\"status\":\"OK\"") != std::string::npos) {
arsenalist 8:51a0ca9079ca 3968 t.stop();
arsenalist 8:51a0ca9079ca 3969 ethConnected = true;
arsenalist 8:51a0ca9079ca 3970 break;
arsenalist 8:51a0ca9079ca 3971 }
arsenalist 8:51a0ca9079ca 3972 if(t.read_ms() > 5000) {
arsenalist 8:51a0ca9079ca 3973 t.stop();
arsenalist 8:51a0ca9079ca 3974 ethConnected = false;
arsenalist 8:51a0ca9079ca 3975 break;
arsenalist 8:51a0ca9079ca 3976 }
arsenalist 8:51a0ca9079ca 3977 }
arsenalist 8:51a0ca9079ca 3978 } else {
arsenalist 8:51a0ca9079ca 3979 ethConnected = false;
arsenalist 8:51a0ca9079ca 3980 }
arsenalist 8:51a0ca9079ca 3981 }
arsenalist 8:51a0ca9079ca 3982
arsenalist 8:51a0ca9079ca 3983 void isWiFiConnected(void) { //WARNING: should be run in emmaModeRegister and emmaModeOperation only - limitation with esp, after MODE=B, cannot go to MODE=S
arsenalist 8:51a0ca9079ca 3984 char s[512];
arsenalist 8:51a0ca9079ca 3985 int connPort;
arsenalist 8:51a0ca9079ca 3986 string connHost;
arsenalist 8:51a0ca9079ca 3987 string str;
arsenalist 8:51a0ca9079ca 3988 Timer t;
arsenalist 8:51a0ca9079ca 3989
arsenalist 29:4d94a03d38e0 3990 /*
arsenalist 27:562a95c120cc 3991 esp.enable();
arsenalist 27:562a95c120cc 3992 wait(1);
arsenalist 27:562a95c120cc 3993 esp.reset();
arsenalist 27:562a95c120cc 3994 wait(1);
arsenalist 27:562a95c120cc 3995 while(!esp.ready());
arsenalist 27:562a95c120cc 3996
arsenalist 27:562a95c120cc 3997 //rest begin
arsenalist 38:e44f35f8129d 3998 if(!rest.begin(restSERVER,restPORT,false)) {
arsenalist 27:562a95c120cc 3999 DBG.printf("EMMA: fail to setup rest\r\n");
arsenalist 27:562a95c120cc 4000 TFT.locate(0,20);
arsenalist 27:562a95c120cc 4001 TFT.printf("EMMA: fail to setup rest");
arsenalist 27:562a95c120cc 4002 while(1);
arsenalist 27:562a95c120cc 4003 }
arsenalist 27:562a95c120cc 4004
arsenalist 27:562a95c120cc 4005 //setup wifi
arsenalist 27:562a95c120cc 4006 DBG.printf("EMMA: setup wifi\r\n");
arsenalist 27:562a95c120cc 4007 TFT.locate(0,20);
arsenalist 27:562a95c120cc 4008 TFT.printf("EMMA: setup wifi");
arsenalist 27:562a95c120cc 4009 wait(1);
arsenalist 27:562a95c120cc 4010 esp.wifiCb.attach(&wifiCb);
arsenalist 28:7561035e3df5 4011 //esp.wifiConnect("Tritronik Mobile","Tri12@11");
arsenalist 28:7561035e3df5 4012 esp.wifiConnect(wifiSSID.c_str(),wifiPASS.c_str());
arsenalist 27:562a95c120cc 4013 DBG.printf("EMMA: system started\r\n");
arsenalist 27:562a95c120cc 4014 TFT.locate(0,20);
arsenalist 27:562a95c120cc 4015 TFT.printf("EMMA: system started");
arsenalist 27:562a95c120cc 4016 t.start();
arsenalist 28:7561035e3df5 4017 while(t.read() < 20);
arsenalist 27:562a95c120cc 4018 t.stop();
arsenalist 27:562a95c120cc 4019 t.reset();
arsenalist 28:7561035e3df5 4020 TFT.locate(0,20);
arsenalist 28:7561035e3df5 4021 TFT.printf(" ");
arsenalist 29:4d94a03d38e0 4022 */
arsenalist 27:562a95c120cc 4023
arsenalist 8:51a0ca9079ca 4024 if(wifiAvailable) {
arsenalist 8:51a0ca9079ca 4025 _ESP.printf("MODE=B");
arsenalist 8:51a0ca9079ca 4026 if(useProxy) {
arsenalist 8:51a0ca9079ca 4027 connHost = proxySERVER;
arsenalist 50:50d98fe6cc15 4028 connPort = proxyPORT;
arsenalist 44:c1d11c491237 4029 //for(int i=0; i<sizeof(s); i++) {
arsenalist 44:c1d11c491237 4030 // s[i]=0; }
arsenalist 50:50d98fe6cc15 4031 sprintf(s,"http://%s:%d/%s/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT,appNAME.c_str(),restSERVER.c_str());
arsenalist 8:51a0ca9079ca 4032 } else {
arsenalist 38:e44f35f8129d 4033 connHost = restSERVER;
arsenalist 50:50d98fe6cc15 4034 connPort = restPORT;
arsenalist 44:c1d11c491237 4035 //for(int i=0; i<sizeof(s); i++) {
arsenalist 44:c1d11c491237 4036 // s[i]=0; }
arsenalist 43:612547648ed1 4037 sprintf(s,"/%s/api/controller/test",appNAME.c_str());
arsenalist 8:51a0ca9079ca 4038 }
arsenalist 27:562a95c120cc 4039 wait(2);
arsenalist 8:51a0ca9079ca 4040 t.start();
arsenalist 28:7561035e3df5 4041 while(!esp.ready() && t.read() < 20);
arsenalist 8:51a0ca9079ca 4042 t.stop();
arsenalist 27:562a95c120cc 4043 t.reset();
arsenalist 8:51a0ca9079ca 4044 if(rest.begin(connHost.c_str(),connPort,false)) {
arsenalist 44:c1d11c491237 4045 //DBG.printf("rest begin\r\n");
arsenalist 8:51a0ca9079ca 4046 esp.process();
arsenalist 8:51a0ca9079ca 4047 rest.get(s);
arsenalist 44:c1d11c491237 4048 //for(int i=0; i<sizeof(s); i++) {
arsenalist 44:c1d11c491237 4049 // s[i]=0; }
arsenalist 8:51a0ca9079ca 4050 rest.getResponse(s,sizeof(s));
arsenalist 8:51a0ca9079ca 4051 str = s;
arsenalist 27:562a95c120cc 4052 DBG.printf("response:%s\r\n",s);
arsenalist 44:c1d11c491237 4053 if(str.find("\"status\":\"OK\"") != std::string::npos) {
arsenalist 8:51a0ca9079ca 4054 wifiConnected = true;
arsenalist 8:51a0ca9079ca 4055 }
arsenalist 8:51a0ca9079ca 4056 } else {
arsenalist 8:51a0ca9079ca 4057 wifiConnected = false;
arsenalist 8:51a0ca9079ca 4058 }
arsenalist 8:51a0ca9079ca 4059 } else {
arsenalist 8:51a0ca9079ca 4060 wifiConnected = false;
arsenalist 8:51a0ca9079ca 4061 }
arsenalist 8:51a0ca9079ca 4062 }
arsenalist 8:51a0ca9079ca 4063
arsenalist 8:51a0ca9079ca 4064 void isGprsConnected(void) {
arsenalist 48:053e92b6dc1e 4065 char cmd[256];
arsenalist 48:053e92b6dc1e 4066 int connPort;
arsenalist 48:053e92b6dc1e 4067 string str;
arsenalist 48:053e92b6dc1e 4068
arsenalist 48:053e92b6dc1e 4069 gprsReset();
arsenalist 48:053e92b6dc1e 4070 str = gprsInit(gprsAPN);
arsenalist 48:053e92b6dc1e 4071 if(str.find("ERROR") != std::string::npos) {
arsenalist 48:053e92b6dc1e 4072 gprsConnected = false;
arsenalist 48:053e92b6dc1e 4073 } else {
arsenalist 48:053e92b6dc1e 4074 if(str.find("AT+CIFSR") != std::string::npos) {
arsenalist 48:053e92b6dc1e 4075 str.erase(str.begin(),str.begin()+str.find("AT+CIFSR")+10);
arsenalist 48:053e92b6dc1e 4076 str.erase(str.end()-2,str.end());
arsenalist 48:053e92b6dc1e 4077 DBG.printf("IP Addr:%s\r\n",str.c_str());
arsenalist 48:053e92b6dc1e 4078 //test connection
arsenalist 50:50d98fe6cc15 4079 connPort = restPORT;
arsenalist 49:d11a1914ee8b 4080 sprintf(cmd,"GET /%s/api/controller/test HTTP/1.0\nHost: %s:%d\n\n\n\n%c",appNAME.c_str(),restSERVER.c_str(),connPort,26);
arsenalist 48:053e92b6dc1e 4081 str = gprsREST(restSERVER,connPort,cmd);
arsenalist 48:053e92b6dc1e 4082 DBG.printf("str:%s\r\n",str.c_str());
arsenalist 48:053e92b6dc1e 4083 if(str.find("\"status\":\"OK\"") != std::string::npos) {
arsenalist 48:053e92b6dc1e 4084 gprsConnected = true;
arsenalist 48:053e92b6dc1e 4085 } else {
arsenalist 48:053e92b6dc1e 4086 gprsConnected = false;
arsenalist 48:053e92b6dc1e 4087 }
arsenalist 48:053e92b6dc1e 4088 } else {
arsenalist 48:053e92b6dc1e 4089 gprsConnected = false;
arsenalist 48:053e92b6dc1e 4090 }
arsenalist 48:053e92b6dc1e 4091 }
arsenalist 8:51a0ca9079ca 4092 }
arsenalist 0:f4e449fa34d7 4093
arsenalist 0:f4e449fa34d7 4094 void addChar(char *s, char c) {
arsenalist 0:f4e449fa34d7 4095 uint16_t k; //customized for EMS
arsenalist 0:f4e449fa34d7 4096 k = strlen(s);
arsenalist 0:f4e449fa34d7 4097 s[k] = c;
arsenalist 0:f4e449fa34d7 4098 s[k + 1] = 0;
arsenalist 0:f4e449fa34d7 4099 }
arsenalist 0:f4e449fa34d7 4100
arsenalist 0:f4e449fa34d7 4101 string calculateMD5(string text) {
arsenalist 0:f4e449fa34d7 4102 char s[64];
arsenalist 0:f4e449fa34d7 4103 memset(s,0,sizeof(s)); //for unknown reason, after reading UID, the 's' will contaion UID data
arsenalist 0:f4e449fa34d7 4104 uint8_t hash[16];
arsenalist 0:f4e449fa34d7 4105 MD5::computeHash(hash, (uint8_t*)text.c_str(), strlen(text.c_str()));
arsenalist 0:f4e449fa34d7 4106 for(int i=0; i<16; ++i) {
arsenalist 0:f4e449fa34d7 4107 sprintf(s,"%s%02x",s,hash[i]);
arsenalist 0:f4e449fa34d7 4108 }
arsenalist 0:f4e449fa34d7 4109 return s;
arsenalist 0:f4e449fa34d7 4110 }
arsenalist 0:f4e449fa34d7 4111
arsenalist 0:f4e449fa34d7 4112 bool writeFirmwareHexToChar(string value) {
arsenalist 0:f4e449fa34d7 4113 FILE *fp;
arsenalist 0:f4e449fa34d7 4114 char s[32];
arsenalist 0:f4e449fa34d7 4115 int number;
arsenalist 0:f4e449fa34d7 4116 string chunk;
arsenalist 0:f4e449fa34d7 4117
arsenalist 0:f4e449fa34d7 4118 sprintf(s,"/sd/newFirmware/firmware.bin");
arsenalist 0:f4e449fa34d7 4119 fp = fopen(s,"a");
arsenalist 0:f4e449fa34d7 4120 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 4121 for(int ch=0; ch<value.size(); ch+=2) {
arsenalist 0:f4e449fa34d7 4122 chunk = value.substr(ch,2);
arsenalist 0:f4e449fa34d7 4123 sscanf(chunk.c_str(),"%x",&number);
arsenalist 0:f4e449fa34d7 4124 fprintf(fp,"%c",number);
arsenalist 0:f4e449fa34d7 4125 }
arsenalist 0:f4e449fa34d7 4126 fclose(fp);
arsenalist 30:08d290158fa7 4127 free(fp);
arsenalist 0:f4e449fa34d7 4128 return true;
arsenalist 0:f4e449fa34d7 4129 }
arsenalist 30:08d290158fa7 4130 fclose(fp);
arsenalist 30:08d290158fa7 4131 free(fp);
arsenalist 0:f4e449fa34d7 4132 return false;
arsenalist 0:f4e449fa34d7 4133 }
arsenalist 0:f4e449fa34d7 4134
arsenalist 0:f4e449fa34d7 4135 bool clearFirmware(void) {
arsenalist 0:f4e449fa34d7 4136 FILE *fp;
arsenalist 0:f4e449fa34d7 4137 char s[32];
arsenalist 0:f4e449fa34d7 4138
arsenalist 0:f4e449fa34d7 4139 sprintf(s,"/sd/newFirmware/firmware.bin");
arsenalist 0:f4e449fa34d7 4140 fp = fopen(s,"w");
arsenalist 0:f4e449fa34d7 4141 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 4142 fprintf(fp,"");
arsenalist 0:f4e449fa34d7 4143 fclose(fp);
arsenalist 30:08d290158fa7 4144 free(fp);
arsenalist 0:f4e449fa34d7 4145 return true;
arsenalist 0:f4e449fa34d7 4146 }
arsenalist 30:08d290158fa7 4147 fclose(fp);
arsenalist 30:08d290158fa7 4148 free(fp);
arsenalist 0:f4e449fa34d7 4149 return false;
arsenalist 0:f4e449fa34d7 4150 }
arsenalist 27:562a95c120cc 4151
arsenalist 27:562a95c120cc 4152 bool writeLog(string logTime, string logData) {
arsenalist 27:562a95c120cc 4153 FILE *fp;
arsenalist 27:562a95c120cc 4154 char s[255];
arsenalist 27:562a95c120cc 4155 string logAll;
arsenalist 27:562a95c120cc 4156 logAll = logTime + "\t" + logData + "\r\n";
arsenalist 27:562a95c120cc 4157
arsenalist 27:562a95c120cc 4158 sprintf(s,"/sd/log.txt");
arsenalist 27:562a95c120cc 4159 fp = fopen(s,"a");
arsenalist 27:562a95c120cc 4160 if(fp != NULL) {
arsenalist 27:562a95c120cc 4161 fprintf(fp,logAll.c_str());
arsenalist 27:562a95c120cc 4162 fclose(fp);
arsenalist 30:08d290158fa7 4163 free(fp);
arsenalist 27:562a95c120cc 4164 return true;
arsenalist 27:562a95c120cc 4165 }
arsenalist 30:08d290158fa7 4166 fclose(fp);
arsenalist 30:08d290158fa7 4167 free(fp);
arsenalist 30:08d290158fa7 4168 return false;
arsenalist 30:08d290158fa7 4169 }
arsenalist 30:08d290158fa7 4170
arsenalist 30:08d290158fa7 4171 bool writeDbg(string dbgTime, string dbgData) {
arsenalist 30:08d290158fa7 4172 FILE *fp;
arsenalist 30:08d290158fa7 4173 char s[255];
arsenalist 30:08d290158fa7 4174 string dbgAll;
arsenalist 30:08d290158fa7 4175 dbgAll = dbgTime + "\t" + dbgData + "\r\n";
arsenalist 30:08d290158fa7 4176
arsenalist 30:08d290158fa7 4177 sprintf(s,"/sd/dbg.txt");
arsenalist 30:08d290158fa7 4178 fp = fopen(s,"a");
arsenalist 30:08d290158fa7 4179 if(fp != NULL) {
arsenalist 30:08d290158fa7 4180 fprintf(fp,dbgAll.c_str());
arsenalist 30:08d290158fa7 4181 fclose(fp);
arsenalist 30:08d290158fa7 4182 free(fp);
arsenalist 30:08d290158fa7 4183 return true;
arsenalist 30:08d290158fa7 4184 }
arsenalist 30:08d290158fa7 4185 fclose(fp);
arsenalist 30:08d290158fa7 4186 free(fp);
arsenalist 27:562a95c120cc 4187 return false;
arsenalist 27:562a95c120cc 4188 }
arsenalist 0:f4e449fa34d7 4189 /*end emma private function*/