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:
Thu Aug 20 08:45:02 2015 +0000
Revision:
41:2d023bd80a9c
Parent:
40:77bd44f57ad3
Child:
42:b32e51a374c7
support multiple command for WiFi 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 0:f4e449fa34d7 17 MQTTEthernet ipstack(&spi, PB_12, PC_6); //spi, cs, reset
arsenalist 37:93f196daf5cf 18 //MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
arsenalist 0:f4e449fa34d7 19
arsenalist 0:f4e449fa34d7 20 //init sd card
arsenalist 8:51a0ca9079ca 21 SDFileSystem sd(PA_7, PA_6, PA_5, PB_3, "sd"); //mosi, miso, sck, cs
arsenalist 0:f4e449fa34d7 22
arsenalist 8:51a0ca9079ca 23 //init ade7758 - without cs pin
arsenalist 14:8287f0f5d987 24 ADE7758 ADE(PB_6, PB_4, PB_5, PB_7); //mosi, miso, sck, irq
arsenalist 0:f4e449fa34d7 25
arsenalist 12:96f637ed37f9 26 //init tft lcd
arsenalist 12:96f637ed37f9 27 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 28 //init touch screen - without cs pin
arsenalist 12:96f637ed37f9 29 TouchScreenADS7843 TP(PB_10, PB_0, PB_1, PB_2, &TFT); //mosi, miso, sclk, irq, tft
arsenalist 12:96f637ed37f9 30
arsenalist 21:33bd8b82560f 31 //init dht sensor
arsenalist 21:33bd8b82560f 32 DHT11 d(PD_2);
arsenalist 21:33bd8b82560f 33
arsenalist 0:f4e449fa34d7 34 //emma settings
arsenalist 0:f4e449fa34d7 35 string emmaUID;
arsenalist 4:76ab12e2f8a3 36 string hmac;
arsenalist 38:e44f35f8129d 37 string appNAME;
arsenalist 38:e44f35f8129d 38 string mqttDOMAIN;
arsenalist 38:e44f35f8129d 39 string mqttKEY;
arsenalist 38:e44f35f8129d 40 string mqttSECRET;
arsenalist 38:e44f35f8129d 41 string mqttSERVER;
arsenalist 38:e44f35f8129d 42 string mqttPORT;
arsenalist 38:e44f35f8129d 43 string restSERVER;
arsenalist 38:e44f35f8129d 44 string restPORT;
arsenalist 0:f4e449fa34d7 45 string wifiSSID;
arsenalist 0:f4e449fa34d7 46 string wifiPASS;
arsenalist 0:f4e449fa34d7 47 string gprsAPN;
arsenalist 0:f4e449fa34d7 48 string proxySERVER;
arsenalist 0:f4e449fa34d7 49 string proxyPORT;
arsenalist 0:f4e449fa34d7 50 string proxyAUTH;
arsenalist 0:f4e449fa34d7 51
arsenalist 4:76ab12e2f8a3 52 //nodes settings
arsenalist 4:76ab12e2f8a3 53 class NODES {
arsenalist 4:76ab12e2f8a3 54 public:
arsenalist 4:76ab12e2f8a3 55 REST *restConn;
arsenalist 4:76ab12e2f8a3 56 NODES(REST *r);
arsenalist 40:77bd44f57ad3 57 int type;
arsenalist 4:76ab12e2f8a3 58 string macAddr;
arsenalist 4:76ab12e2f8a3 59 string ipAddr;
arsenalist 4:76ab12e2f8a3 60 };
arsenalist 4:76ab12e2f8a3 61 NODES::NODES(REST *r) {
arsenalist 4:76ab12e2f8a3 62 restConn = r;
arsenalist 4:76ab12e2f8a3 63 }
arsenalist 4:76ab12e2f8a3 64 REST restObj[NODES_MAX] = {REST(&esp),REST(&esp),REST(&esp),REST(&esp),REST(&esp)};
arsenalist 4:76ab12e2f8a3 65 NODES nodes[NODES_MAX] = {NODES(&restObj[0]),NODES(&restObj[1]),NODES(&restObj[2]),NODES(&restObj[3]),NODES(&restObj[4])};
arsenalist 4:76ab12e2f8a3 66
arsenalist 12:96f637ed37f9 67 //mode box class for main menu
arsenalist 12:96f637ed37f9 68 class modeBox {
arsenalist 12:96f637ed37f9 69 public:
arsenalist 12:96f637ed37f9 70 int xTL; //TopLeft
arsenalist 12:96f637ed37f9 71 int yTL;
arsenalist 12:96f637ed37f9 72 int xBR; //BottomRight
arsenalist 12:96f637ed37f9 73 int yBR;
arsenalist 29:4d94a03d38e0 74 string text;
arsenalist 29:4d94a03d38e0 75 string name;
arsenalist 12:96f637ed37f9 76 };
arsenalist 12:96f637ed37f9 77
arsenalist 0:f4e449fa34d7 78 //ade7758 variables
arsenalist 25:36c6a5db50ed 79 uint32_t AWattHrValue, BWattHrValue, CWattHrValue;
arsenalist 25:36c6a5db50ed 80 uint32_t AVAHrValue, BVAHrValue, CVAHrValue;
arsenalist 19:7e3e9332f719 81 uint32_t AWattHrSum = 0;
arsenalist 19:7e3e9332f719 82 uint32_t BWattHrSum = 0;
arsenalist 19:7e3e9332f719 83 uint32_t CWattHrSum = 0;
arsenalist 0:f4e449fa34d7 84 float AWattHr, BWattHr, CWattHr;
arsenalist 4:76ab12e2f8a3 85 float AVrms, BVrms, CVrms;
arsenalist 4:76ab12e2f8a3 86 float AIrms, BIrms, CIrms;
arsenalist 4:76ab12e2f8a3 87 float AWatt, BWatt, CWatt;
arsenalist 4:76ab12e2f8a3 88 float XWattHr,XVrms,XWatt;
arsenalist 0:f4e449fa34d7 89
arsenalist 0:f4e449fa34d7 90 //variables
arsenalist 0:f4e449fa34d7 91 bool ethAvailable = false;
arsenalist 0:f4e449fa34d7 92 bool wifiAvailable = false;
arsenalist 0:f4e449fa34d7 93 bool gprsAvailable = false;
arsenalist 0:f4e449fa34d7 94 bool ethConnected = false;
arsenalist 0:f4e449fa34d7 95 bool wifiConnected = false;
arsenalist 9:a58c04da4476 96 bool gprsConnected = false;
arsenalist 0:f4e449fa34d7 97 bool useProxy = false;
arsenalist 0:f4e449fa34d7 98 bool newCommand = false;
arsenalist 37:93f196daf5cf 99 bool espFreeMemory = true; //for after bootup
arsenalist 27:562a95c120cc 100 bool espDHCPClientStart = false;
arsenalist 22:e18d361bf688 101 bool newEnergyData = false;
arsenalist 0:f4e449fa34d7 102 string globalCommand;
arsenalist 4:76ab12e2f8a3 103 string rxBuf;
arsenalist 27:562a95c120cc 104 string rxLog;
arsenalist 27:562a95c120cc 105 string rxLogA;
arsenalist 0:f4e449fa34d7 106
arsenalist 12:96f637ed37f9 107 /*start lcd and touch*/
arsenalist 12:96f637ed37f9 108 int emmaModeSelection(void) {
arsenalist 12:96f637ed37f9 109 bool modeSelected = false;
arsenalist 12:96f637ed37f9 110 int md=0;
arsenalist 12:96f637ed37f9 111 int TPx;
arsenalist 12:96f637ed37f9 112 int TPy;
arsenalist 31:e2abded4d85f 113 Timer t;
arsenalist 12:96f637ed37f9 114
arsenalist 12:96f637ed37f9 115 TFT.background(Black);
arsenalist 12:96f637ed37f9 116 TFT.foreground(White);
arsenalist 12:96f637ed37f9 117
arsenalist 12:96f637ed37f9 118 TFT.set_font((unsigned char*) Arial12x12);
arsenalist 12:96f637ed37f9 119 TFT.set_orientation(1);
arsenalist 12:96f637ed37f9 120 TFT.cls();
arsenalist 12:96f637ed37f9 121 TFT.locate(0,0);
arsenalist 12:96f637ed37f9 122 TFT.printf("Hello, I'm Emma!");
arsenalist 13:e8adfe305dbc 123 wait(2);
arsenalist 12:96f637ed37f9 124 TFT.cls();
arsenalist 12:96f637ed37f9 125
arsenalist 12:96f637ed37f9 126 Matrix matrix;
arsenalist 12:96f637ed37f9 127 Coordinate ScreenSample[3];
arsenalist 12:96f637ed37f9 128
arsenalist 12:96f637ed37f9 129 matrix.An = 580;
arsenalist 12:96f637ed37f9 130 matrix.Bn = 75980;
arsenalist 12:96f637ed37f9 131 matrix.Cn = -3410580;
arsenalist 12:96f637ed37f9 132 matrix.Dn = 57855;
arsenalist 12:96f637ed37f9 133 matrix.En = -2465;
arsenalist 12:96f637ed37f9 134 matrix.Fn = -3483515;
arsenalist 12:96f637ed37f9 135 matrix.Divider = 209144;
arsenalist 12:96f637ed37f9 136
arsenalist 12:96f637ed37f9 137 ScreenSample[0].x = 230;
arsenalist 12:96f637ed37f9 138 ScreenSample[0].y = 167;
arsenalist 12:96f637ed37f9 139 ScreenSample[1].x = 754;
arsenalist 12:96f637ed37f9 140 ScreenSample[1].y = 163;
arsenalist 12:96f637ed37f9 141 ScreenSample[2].x = 771;
arsenalist 12:96f637ed37f9 142 ScreenSample[2].y = 562;
arsenalist 12:96f637ed37f9 143
arsenalist 12:96f637ed37f9 144 TP.SetCalibration(&matrix, &ScreenSample[0]);
arsenalist 12:96f637ed37f9 145
arsenalist 12:96f637ed37f9 146 //draw border
arsenalist 12:96f637ed37f9 147 TFT.line(15,15,310,15,Orange);
arsenalist 12:96f637ed37f9 148 TFT.line(310,15,310,250,Orange);
arsenalist 12:96f637ed37f9 149 TFT.line(310,250,15,250,Orange);
arsenalist 12:96f637ed37f9 150 TFT.line(15,250,15,15,Orange);
arsenalist 12:96f637ed37f9 151
arsenalist 12:96f637ed37f9 152 //init main menu
arsenalist 12:96f637ed37f9 153 modeBox menu[6];
arsenalist 12:96f637ed37f9 154
arsenalist 12:96f637ed37f9 155 //wifi config mode
arsenalist 12:96f637ed37f9 156 menu[MODE_WIFI_CONFIG].xTL = 25;
arsenalist 12:96f637ed37f9 157 menu[MODE_WIFI_CONFIG].yTL = 25;
arsenalist 12:96f637ed37f9 158 menu[MODE_WIFI_CONFIG].xBR = 110;
arsenalist 12:96f637ed37f9 159 menu[MODE_WIFI_CONFIG].yBR = 90;
arsenalist 29:4d94a03d38e0 160 menu[MODE_WIFI_CONFIG].text = "1.wifi config";
arsenalist 29:4d94a03d38e0 161 menu[MODE_WIFI_CONFIG].name = "wifi config";
arsenalist 12:96f637ed37f9 162
arsenalist 12:96f637ed37f9 163 //setting mode
arsenalist 29:4d94a03d38e0 164 menu[MODE_SETTINGS].xTL = 25;
arsenalist 29:4d94a03d38e0 165 menu[MODE_SETTINGS].yTL = 100;
arsenalist 29:4d94a03d38e0 166 menu[MODE_SETTINGS].xBR = 110;
arsenalist 29:4d94a03d38e0 167 menu[MODE_SETTINGS].yBR = 165;
arsenalist 29:4d94a03d38e0 168 menu[MODE_SETTINGS].text = "2.settings ";
arsenalist 29:4d94a03d38e0 169 menu[MODE_SETTINGS].name = "settings";
arsenalist 12:96f637ed37f9 170
arsenalist 12:96f637ed37f9 171 //register mode
arsenalist 29:4d94a03d38e0 172 menu[MODE_REGISTER].xTL = 120;
arsenalist 29:4d94a03d38e0 173 menu[MODE_REGISTER].yTL = 25;
arsenalist 29:4d94a03d38e0 174 menu[MODE_REGISTER].xBR = 205;
arsenalist 29:4d94a03d38e0 175 menu[MODE_REGISTER].yBR = 90;
arsenalist 29:4d94a03d38e0 176 menu[MODE_REGISTER].text = "3.register ";
arsenalist 29:4d94a03d38e0 177 menu[MODE_REGISTER].name = "register";
arsenalist 12:96f637ed37f9 178
arsenalist 12:96f637ed37f9 179 //operational mode
arsenalist 12:96f637ed37f9 180 menu[MODE_OPERATION].xTL = 120;
arsenalist 12:96f637ed37f9 181 menu[MODE_OPERATION].yTL = 100;
arsenalist 12:96f637ed37f9 182 menu[MODE_OPERATION].xBR = 205;
arsenalist 12:96f637ed37f9 183 menu[MODE_OPERATION].yBR = 165;
arsenalist 29:4d94a03d38e0 184 menu[MODE_OPERATION].text = "4.operation ";
arsenalist 29:4d94a03d38e0 185 menu[MODE_OPERATION].name = "operation";
arsenalist 12:96f637ed37f9 186
arsenalist 12:96f637ed37f9 187 //firmware download mode
arsenalist 12:96f637ed37f9 188 menu[MODE_FIRMWARE_DOWNLOAD].xTL = 215;
arsenalist 12:96f637ed37f9 189 menu[MODE_FIRMWARE_DOWNLOAD].yTL = 25;
arsenalist 12:96f637ed37f9 190 menu[MODE_FIRMWARE_DOWNLOAD].xBR = 300;
arsenalist 12:96f637ed37f9 191 menu[MODE_FIRMWARE_DOWNLOAD].yBR = 90;
arsenalist 29:4d94a03d38e0 192 menu[MODE_FIRMWARE_DOWNLOAD].text = "5.fw dwld ";
arsenalist 29:4d94a03d38e0 193 menu[MODE_FIRMWARE_DOWNLOAD].name = "fw dwld";
arsenalist 12:96f637ed37f9 194
arsenalist 12:96f637ed37f9 195 //reserved mode
arsenalist 12:96f637ed37f9 196 menu[5].xTL = 215;
arsenalist 12:96f637ed37f9 197 menu[5].yTL = 100;
arsenalist 12:96f637ed37f9 198 menu[5].xBR = 300;
arsenalist 12:96f637ed37f9 199 menu[5].yBR = 165;
arsenalist 29:4d94a03d38e0 200 menu[5].text = "6.reserved ";
arsenalist 29:4d94a03d38e0 201 menu[5].name = "reserved";
arsenalist 12:96f637ed37f9 202
arsenalist 17:b3ced0e67916 203 //draw main menu
arsenalist 12:96f637ed37f9 204 for(int i=0; i<6; i++) {
arsenalist 12:96f637ed37f9 205 TFT.fillrect(menu[i].xTL,menu[i].yTL,menu[i].xBR,menu[i].yBR,Orange);
arsenalist 12:96f637ed37f9 206 }
arsenalist 12:96f637ed37f9 207
arsenalist 17:b3ced0e67916 208 //add text to main menu
arsenalist 17:b3ced0e67916 209 for(int i=0; i<6; i++) {
arsenalist 17:b3ced0e67916 210 TFT.locate(menu[i].xTL,menu[i].yTL);
arsenalist 31:e2abded4d85f 211 TFT.printf("%s",menu[i].text.c_str());
arsenalist 31:e2abded4d85f 212 }
arsenalist 31:e2abded4d85f 213
arsenalist 31:e2abded4d85f 214 //read emma settings
arsenalist 31:e2abded4d85f 215 emmaReadSettings();
arsenalist 31:e2abded4d85f 216
arsenalist 38:e44f35f8129d 217 //mqttDOMAIN is not empty -> has been registered
arsenalist 38:e44f35f8129d 218 if(!mqttDOMAIN.empty()) {
arsenalist 31:e2abded4d85f 219 TFT.locate(25,170);
arsenalist 31:e2abded4d85f 220 TFT.printf(" auto select enabled");
arsenalist 31:e2abded4d85f 221 t.start();
arsenalist 17:b3ced0e67916 222 }
arsenalist 17:b3ced0e67916 223
arsenalist 12:96f637ed37f9 224 while(!modeSelected) {
arsenalist 12:96f637ed37f9 225 if(!TP._tp_irq) {
arsenalist 12:96f637ed37f9 226 if(TP.Read_Ads7843()) {
arsenalist 12:96f637ed37f9 227 TP.getDisplayPoint();
arsenalist 12:96f637ed37f9 228 TPx = TP.display.x;
arsenalist 12:96f637ed37f9 229 TPy = TP.display.y;
arsenalist 12:96f637ed37f9 230 TP.TP_DrawPoint(TPx,TPy, Blue);
arsenalist 12:96f637ed37f9 231
arsenalist 12:96f637ed37f9 232 for(int i=0; i<6; i++) {
arsenalist 12:96f637ed37f9 233 if((menu[i].xTL < TPx && TPx < menu[i].xBR) && (menu[i].yTL < TPy && TPy < menu[i].yBR)) {
arsenalist 12:96f637ed37f9 234 md = i;
arsenalist 12:96f637ed37f9 235 modeSelected = true;
arsenalist 12:96f637ed37f9 236 }
arsenalist 12:96f637ed37f9 237 }
arsenalist 12:96f637ed37f9 238 }
arsenalist 31:e2abded4d85f 239 }
arsenalist 31:e2abded4d85f 240
arsenalist 31:e2abded4d85f 241 //auto select (to emmaModeOperation) after some times
arsenalist 38:e44f35f8129d 242 if(!mqttDOMAIN.empty()) {
arsenalist 31:e2abded4d85f 243 if(t.read()>20) {
arsenalist 31:e2abded4d85f 244 md = MODE_OPERATION;
arsenalist 31:e2abded4d85f 245 modeSelected = true;
arsenalist 31:e2abded4d85f 246 t.stop();
arsenalist 31:e2abded4d85f 247 t.reset();
arsenalist 31:e2abded4d85f 248 }
arsenalist 31:e2abded4d85f 249 }
arsenalist 31:e2abded4d85f 250
arsenalist 12:96f637ed37f9 251 }
arsenalist 12:96f637ed37f9 252
arsenalist 12:96f637ed37f9 253 TFT.locate(25,170);
arsenalist 12:96f637ed37f9 254 TFT.printf(" ");
arsenalist 12:96f637ed37f9 255 TFT.locate(25,170);
arsenalist 29:4d94a03d38e0 256 TFT.printf("mode: %s is selected",menu[md].name.c_str());
arsenalist 13:e8adfe305dbc 257 wait(2);
arsenalist 12:96f637ed37f9 258 TFT.cls();
arsenalist 12:96f637ed37f9 259
arsenalist 12:96f637ed37f9 260 return md;
arsenalist 12:96f637ed37f9 261 }
arsenalist 12:96f637ed37f9 262 /*end lcd and touch*/
arsenalist 12:96f637ed37f9 263
arsenalist 31:e2abded4d85f 264 /*start emma read settings*/
arsenalist 31:e2abded4d85f 265 void emmaReadSettings(void) {
arsenalist 4:76ab12e2f8a3 266 char s[64];
arsenalist 0:f4e449fa34d7 267 DBG.baud(19200);
arsenalist 31:e2abded4d85f 268 DBG.printf("\r\nemmaReadSettings\r\n");
arsenalist 0:f4e449fa34d7 269
arsenalist 0:f4e449fa34d7 270 //read settings
arsenalist 9:a58c04da4476 271 emmaUID = readSetting("emmaUID");
arsenalist 9:a58c04da4476 272 //emmaUID = "066eff575349896767073038";
arsenalist 0:f4e449fa34d7 273 DBG.printf("emmaUID:%s\r\n",emmaUID.c_str());
arsenalist 37:93f196daf5cf 274
arsenalist 4:76ab12e2f8a3 275 //calculate hmac
arsenalist 4:76ab12e2f8a3 276 for(int i=0; i<sizeof(s); i++) {
arsenalist 4:76ab12e2f8a3 277 s[i]=0; }
arsenalist 4:76ab12e2f8a3 278 sprintf(s,"emma-%s",emmaUID.c_str());
arsenalist 4:76ab12e2f8a3 279 hmac = calculateMD5(s);
arsenalist 4:76ab12e2f8a3 280 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 37:93f196daf5cf 281
arsenalist 38:e44f35f8129d 282 appNAME = readSetting("appNAME");
arsenalist 38:e44f35f8129d 283 DBG.printf("appNAME:%s\r\n",appNAME.c_str());
arsenalist 38:e44f35f8129d 284
arsenalist 38:e44f35f8129d 285 mqttDOMAIN = readSetting("mqttDOMAIN");
arsenalist 38:e44f35f8129d 286 DBG.printf("mqttDOMAIN:%s\r\n",mqttDOMAIN.c_str());
arsenalist 38:e44f35f8129d 287 mqttKEY = readSetting("mqttKEY");
arsenalist 38:e44f35f8129d 288 DBG.printf("mqttKEY:%s\r\n",mqttKEY.c_str());
arsenalist 38:e44f35f8129d 289 mqttSECRET = readSetting("mqttSECRET");
arsenalist 38:e44f35f8129d 290 DBG.printf("mqttSECRET:%s\r\n",mqttSECRET.c_str());
arsenalist 38:e44f35f8129d 291 mqttSERVER = readSetting("mqttSERVER");
arsenalist 38:e44f35f8129d 292 DBG.printf("mqttSERVER:%s\r\n",mqttSERVER.c_str());
arsenalist 38:e44f35f8129d 293 mqttPORT = readSetting("mqttPORT");
arsenalist 38:e44f35f8129d 294 DBG.printf("mqttPORT:%s\r\n",mqttPORT.c_str());
arsenalist 38:e44f35f8129d 295
arsenalist 38:e44f35f8129d 296 restSERVER = readSetting("restSERVER");
arsenalist 38:e44f35f8129d 297 DBG.printf("restSERVER:%s\r\n",restSERVER.c_str());
arsenalist 38:e44f35f8129d 298 restPORT = readSetting("restPORT");
arsenalist 38:e44f35f8129d 299 DBG.printf("restPORT:%s\r\n",restPORT.c_str());
arsenalist 38:e44f35f8129d 300
arsenalist 9:a58c04da4476 301 proxySERVER = readSetting("proxySERVER");
arsenalist 0:f4e449fa34d7 302 DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str());
arsenalist 9:a58c04da4476 303 proxyPORT = readSetting("proxyPORT");
arsenalist 0:f4e449fa34d7 304 DBG.printf("proxyPORT:%s\r\n",proxyPORT.c_str());
arsenalist 9:a58c04da4476 305 proxyAUTH = readSetting("proxyAUTH");
arsenalist 0:f4e449fa34d7 306 DBG.printf("proxyAUTH:%s\r\n",proxyAUTH.c_str());
arsenalist 28:7561035e3df5 307 wifiSSID = readSetting("wifiSSID");
arsenalist 28:7561035e3df5 308 DBG.printf("wifiSSID:%s\r\n",wifiSSID.c_str());
arsenalist 28:7561035e3df5 309 wifiPASS = readSetting("wifiPASS");
arsenalist 28:7561035e3df5 310 DBG.printf("wifiPASS:%s\r\n",wifiPASS.c_str());
arsenalist 31:e2abded4d85f 311 }
arsenalist 31:e2abded4d85f 312 /*end emma read settings*/
arsenalist 31:e2abded4d85f 313
arsenalist 31:e2abded4d85f 314 /*start emma mode*/
arsenalist 31:e2abded4d85f 315 void emmaInit(int mode) {
arsenalist 31:e2abded4d85f 316 DBG.printf("\r\nemmaInit\r\n");
arsenalist 31:e2abded4d85f 317 DBG.printf("mode:%d\r\n",mode);
arsenalist 0:f4e449fa34d7 318
arsenalist 0:f4e449fa34d7 319 //check proxy
arsenalist 0:f4e449fa34d7 320 if(!proxySERVER.empty() && !proxyPORT.empty() && !proxyAUTH.empty()) {
arsenalist 0:f4e449fa34d7 321 useProxy = true;
arsenalist 0:f4e449fa34d7 322 } else {
arsenalist 0:f4e449fa34d7 323 useProxy = false;
arsenalist 0:f4e449fa34d7 324 }
arsenalist 0:f4e449fa34d7 325 //testing purpose
arsenalist 32:78633fb9b2a1 326 //useProxy = false;
arsenalist 0:f4e449fa34d7 327 DBG.printf("proxy:%d\r\n",useProxy);
arsenalist 0:f4e449fa34d7 328
arsenalist 8:51a0ca9079ca 329 //check available interface
arsenalist 8:51a0ca9079ca 330 isEthAvailable(); //check whether cable is connected
arsenalist 31:e2abded4d85f 331 wifiAvailable = true; //wifi module will always on the board
arsenalist 31:e2abded4d85f 332 gprsAvailable = true; //gprs module will always on the board
arsenalist 8:51a0ca9079ca 333 DBG.printf("eth:%d\r\n",ethAvailable);
arsenalist 8:51a0ca9079ca 334 DBG.printf("wifi:%d\r\n",wifiAvailable);
arsenalist 8:51a0ca9079ca 335 DBG.printf("gprs:%d\r\n",gprsAvailable);
arsenalist 0:f4e449fa34d7 336 }
arsenalist 0:f4e449fa34d7 337 void emmaModeWiFiConfig(void) {
arsenalist 0:f4e449fa34d7 338 string str;
arsenalist 18:87f30ba9ddc5 339 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 340 TFT.printf(" please wait");
arsenalist 0:f4e449fa34d7 341
arsenalist 0:f4e449fa34d7 342 if(wifiAvailable) {
arsenalist 0:f4e449fa34d7 343 DBG.printf("emmaModeWiFiConfig\r\n");
arsenalist 0:f4e449fa34d7 344
arsenalist 0:f4e449fa34d7 345 //set wifi module to configuration
arsenalist 0:f4e449fa34d7 346 _ESP.printf("MODE=C");
arsenalist 0:f4e449fa34d7 347 while(1) {
arsenalist 0:f4e449fa34d7 348 char rcv[128] = {};
arsenalist 0:f4e449fa34d7 349 rcvReply(rcv,3000);
arsenalist 0:f4e449fa34d7 350 str = rcv;
arsenalist 0:f4e449fa34d7 351 if(str.find("SC_STATUS_FIND_CHANNEL") != std::string::npos)
arsenalist 0:f4e449fa34d7 352 break;
arsenalist 0:f4e449fa34d7 353 }
arsenalist 18:87f30ba9ddc5 354
arsenalist 18:87f30ba9ddc5 355 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 356 TFT.printf(" emmaModeWiFiConfig");
arsenalist 18:87f30ba9ddc5 357 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 358 TFT.printf(" connect with emma app now");
arsenalist 18:87f30ba9ddc5 359
arsenalist 0:f4e449fa34d7 360 DBG.printf("entering wifi configuration mode\r\n");
arsenalist 0:f4e449fa34d7 361 while(1) {
arsenalist 0:f4e449fa34d7 362 char rcv[128] = {};
arsenalist 0:f4e449fa34d7 363 rcvReply(rcv,3000);
arsenalist 0:f4e449fa34d7 364 str = rcv;
arsenalist 0:f4e449fa34d7 365 if(str.find("MODE=C OK") != std::string::npos) {
arsenalist 0:f4e449fa34d7 366 //save wifiSSID and wifiPASS
arsenalist 0:f4e449fa34d7 367 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 368 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 369 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 370
arsenalist 0:f4e449fa34d7 371 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 372 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 373
arsenalist 0:f4e449fa34d7 374 char *parameter[2] = {"wifiSSID","wifiPASS"};
arsenalist 0:f4e449fa34d7 375
arsenalist 0:f4e449fa34d7 376 for(int i=0; i<2; i++) {
arsenalist 0:f4e449fa34d7 377 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 378 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 11:2311b2d5157d 379 int st = writeSetting(parameter[i],val.c_str());
arsenalist 11:2311b2d5157d 380 if(st) {
arsenalist 11:2311b2d5157d 381 DBG.printf("%s is saved\r\n",parameter[i]);
arsenalist 18:87f30ba9ddc5 382 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 383 TFT.printf(" %s is saved",parameter[i]);
arsenalist 18:87f30ba9ddc5 384 wait(3);
arsenalist 18:87f30ba9ddc5 385 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 386 TFT.printf(" ");
arsenalist 11:2311b2d5157d 387 } else {
arsenalist 11:2311b2d5157d 388 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 18:87f30ba9ddc5 389 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 390 TFT.printf(" %s is not saved",parameter[i]);
arsenalist 18:87f30ba9ddc5 391 wait(3);
arsenalist 18:87f30ba9ddc5 392 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 393 TFT.printf(" ");
arsenalist 11:2311b2d5157d 394 }
arsenalist 0:f4e449fa34d7 395 }
arsenalist 0:f4e449fa34d7 396 }
arsenalist 18:87f30ba9ddc5 397
arsenalist 18:87f30ba9ddc5 398 //wificonfig finish
arsenalist 18:87f30ba9ddc5 399 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 400 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 401 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 402 TFT.printf(" wificonfig finish. please restart.");
arsenalist 18:87f30ba9ddc5 403
arsenalist 0:f4e449fa34d7 404 }
arsenalist 18:87f30ba9ddc5 405 } else if(str.find("SC_STATUS_GETTING_SSID_PSWD") != std::string::npos){
arsenalist 18:87f30ba9ddc5 406 DBG.printf("app connected\r\n");
arsenalist 18:87f30ba9ddc5 407 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 408 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 409 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 410 TFT.printf(" app connected");
arsenalist 0:f4e449fa34d7 411 }
arsenalist 0:f4e449fa34d7 412 }
arsenalist 0:f4e449fa34d7 413 } else {
arsenalist 0:f4e449fa34d7 414 DBG.printf("no wifi found\r\n");
arsenalist 18:87f30ba9ddc5 415 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 416 TFT.printf(" no wifi found");
arsenalist 0:f4e449fa34d7 417 }
arsenalist 0:f4e449fa34d7 418 }
arsenalist 0:f4e449fa34d7 419 void emmaModeSettings(void) {
arsenalist 0:f4e449fa34d7 420 bool clientIsConnected = false;
arsenalist 0:f4e449fa34d7 421 bool serverIsListened = false;
arsenalist 0:f4e449fa34d7 422 char s[32];
arsenalist 0:f4e449fa34d7 423 string str;
arsenalist 0:f4e449fa34d7 424
arsenalist 18:87f30ba9ddc5 425 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 426 TFT.printf(" please wait");
arsenalist 18:87f30ba9ddc5 427
arsenalist 8:51a0ca9079ca 428 //create settings dir
arsenalist 0:f4e449fa34d7 429 mkdir("/sd/settings",0777);
arsenalist 8:51a0ca9079ca 430
arsenalist 0:f4e449fa34d7 431 //get and write emmaUID
arsenalist 0:f4e449fa34d7 432 string uid = getUID();
arsenalist 0:f4e449fa34d7 433 sprintf(s,"(%s)",uid.c_str());
arsenalist 0:f4e449fa34d7 434 uid = s;
arsenalist 0:f4e449fa34d7 435 writeSetting("emmaUID",uid);
arsenalist 0:f4e449fa34d7 436
arsenalist 8:51a0ca9079ca 437 if(ethAvailable) {
arsenalist 0:f4e449fa34d7 438 DBG.printf("emmaModeSettings - eth\r\n");
arsenalist 37:93f196daf5cf 439 wait(2);
arsenalist 37:93f196daf5cf 440 TFT.locate(0,0);
arsenalist 37:93f196daf5cf 441 TFT.printf(" ");
arsenalist 37:93f196daf5cf 442 TFT.locate(0,0);
arsenalist 37:93f196daf5cf 443 TFT.printf(" emmaModeSettings");
arsenalist 0:f4e449fa34d7 444
arsenalist 0:f4e449fa34d7 445 TCPSocketServer svr;
arsenalist 0:f4e449fa34d7 446 TCPSocketConnection clientSock;
arsenalist 0:f4e449fa34d7 447
arsenalist 0:f4e449fa34d7 448 if(svr.bind(SERVER_PORT) < 0) {
arsenalist 0:f4e449fa34d7 449 DBG.printf("tcp server bind failed\r\n");
arsenalist 0:f4e449fa34d7 450 } else {
arsenalist 0:f4e449fa34d7 451 DBG.printf("tcp server bind success\r\n");
arsenalist 0:f4e449fa34d7 452 serverIsListened = true;
arsenalist 0:f4e449fa34d7 453 }
arsenalist 0:f4e449fa34d7 454
arsenalist 0:f4e449fa34d7 455 DBG.printf("please connect to %s\r\n",ipstack.getEth().getIPAddress());
arsenalist 0:f4e449fa34d7 456
arsenalist 0:f4e449fa34d7 457 if(svr.listen(1) < 0) {
arsenalist 37:93f196daf5cf 458 DBG.printf("tcp server listen failed\r\n");
arsenalist 37:93f196daf5cf 459 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 460 TFT.printf(" settings error. please restart.");
arsenalist 37:93f196daf5cf 461 while(1);
arsenalist 0:f4e449fa34d7 462 } else {
arsenalist 37:93f196daf5cf 463 DBG.printf("tcp server is listening...\r\n");
arsenalist 37:93f196daf5cf 464 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 465 TFT.printf(" connect with emma app now!");
arsenalist 0:f4e449fa34d7 466 }
arsenalist 0:f4e449fa34d7 467
arsenalist 0:f4e449fa34d7 468 clientSock.set_blocking(false,30000); //timeout after 30sec
arsenalist 0:f4e449fa34d7 469
arsenalist 0:f4e449fa34d7 470 //listening
arsenalist 0:f4e449fa34d7 471 while (serverIsListened) {
arsenalist 0:f4e449fa34d7 472 if(svr.accept(clientSock) < 0) {
arsenalist 37:93f196daf5cf 473 DBG.printf("failed to accept connection\r\n");
arsenalist 37:93f196daf5cf 474 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 475 TFT.printf(" ");
arsenalist 37:93f196daf5cf 476 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 477 TFT.printf("failed to accept connection");
arsenalist 0:f4e449fa34d7 478 } else {
arsenalist 0:f4e449fa34d7 479 DBG.printf("connection success!\r\nIP: %s\r\n",clientSock.get_address());
arsenalist 37:93f196daf5cf 480 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 481 TFT.printf(" ");
arsenalist 37:93f196daf5cf 482 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 483 //TFT.printf(" connection success! IP: %s",clientSock.get_address());
arsenalist 37:93f196daf5cf 484 TFT.printf(" connection success!");
arsenalist 0:f4e449fa34d7 485 clientIsConnected = true;
arsenalist 0:f4e449fa34d7 486
arsenalist 0:f4e449fa34d7 487 while(clientIsConnected) {
arsenalist 0:f4e449fa34d7 488 char buffer[1024] = {};
arsenalist 0:f4e449fa34d7 489 switch(clientSock.receive(buffer,1023)) {
arsenalist 0:f4e449fa34d7 490 case 0:
arsenalist 0:f4e449fa34d7 491 DBG.printf("received buffer is empty\r\n");
arsenalist 0:f4e449fa34d7 492 clientIsConnected = false;
arsenalist 0:f4e449fa34d7 493 break;
arsenalist 0:f4e449fa34d7 494 case -1:
arsenalist 0:f4e449fa34d7 495 DBG.printf("failed to read data from client\r\n");
arsenalist 0:f4e449fa34d7 496 clientIsConnected = false;
arsenalist 0:f4e449fa34d7 497 break;
arsenalist 0:f4e449fa34d7 498 default:
arsenalist 0:f4e449fa34d7 499 //DBG.printf("received data: %d\r\n%s\r\n",strlen(buffer),buffer);
arsenalist 0:f4e449fa34d7 500 DBG.printf("\r\n");
arsenalist 0:f4e449fa34d7 501
arsenalist 0:f4e449fa34d7 502 str = buffer;
arsenalist 0:f4e449fa34d7 503 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 504 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 505 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 506
arsenalist 0:f4e449fa34d7 507 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 508 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 509
arsenalist 14:8287f0f5d987 510 char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
arsenalist 0:f4e449fa34d7 511
arsenalist 0:f4e449fa34d7 512 for(int i=0; i<4; i++) {
arsenalist 0:f4e449fa34d7 513 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 514 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 0:f4e449fa34d7 515 int st = writeSetting(parameter[i],val.c_str());
arsenalist 0:f4e449fa34d7 516 if(st) {
arsenalist 0:f4e449fa34d7 517 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 37:93f196daf5cf 518 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 519 TFT.printf(" %s: %s is saved",parameter[i],val.c_str());
arsenalist 37:93f196daf5cf 520 wait(1);
arsenalist 37:93f196daf5cf 521 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 522 TFT.printf(" ");
arsenalist 0:f4e449fa34d7 523 } else {
arsenalist 0:f4e449fa34d7 524 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 37:93f196daf5cf 525 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 526 TFT.printf(" %s is not saved",parameter[i]);
arsenalist 37:93f196daf5cf 527 wait(1);
arsenalist 37:93f196daf5cf 528 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 529 TFT.printf(" ");
arsenalist 0:f4e449fa34d7 530 }
arsenalist 0:f4e449fa34d7 531 }
arsenalist 0:f4e449fa34d7 532 }
arsenalist 14:8287f0f5d987 533
arsenalist 14:8287f0f5d987 534 //set time
arsenalist 14:8287f0f5d987 535 if(jsonValue.hasMember(parameter[4])) {
arsenalist 14:8287f0f5d987 536 string epTime = jsonValue[parameter[4]].get<std::string>();
arsenalist 20:ea14f175bbb4 537 time_t seconds;
arsenalist 14:8287f0f5d987 538 sscanf(epTime.c_str(),"%d",&seconds);
arsenalist 14:8287f0f5d987 539 set_time(seconds);
arsenalist 14:8287f0f5d987 540 DBG.printf("time is set\r\n");
arsenalist 37:93f196daf5cf 541 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 542 TFT.printf(" time is set");
arsenalist 37:93f196daf5cf 543 wait(1);
arsenalist 37:93f196daf5cf 544 TFT.locate(0,40);
arsenalist 37:93f196daf5cf 545 TFT.printf(" ");
arsenalist 14:8287f0f5d987 546 }
arsenalist 0:f4e449fa34d7 547 }
arsenalist 0:f4e449fa34d7 548 break;
arsenalist 0:f4e449fa34d7 549 }
arsenalist 0:f4e449fa34d7 550 }
arsenalist 0:f4e449fa34d7 551 DBG.printf("close connection\r\n");
arsenalist 37:93f196daf5cf 552 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 553 TFT.printf(" ");
arsenalist 37:93f196daf5cf 554 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 555 TFT.printf(" settings finish. please restart.");
arsenalist 0:f4e449fa34d7 556 clientSock.close();
arsenalist 0:f4e449fa34d7 557 }
arsenalist 0:f4e449fa34d7 558 }
arsenalist 8:51a0ca9079ca 559 } else if(wifiAvailable) {
arsenalist 8:51a0ca9079ca 560 DBG.printf("emmaModeSettings - wifi\r\n");
arsenalist 8:51a0ca9079ca 561
arsenalist 8:51a0ca9079ca 562 _ESP.printf("MODE=S");
arsenalist 8:51a0ca9079ca 563 while(1) {
arsenalist 8:51a0ca9079ca 564 char rcv[128] = {};
arsenalist 8:51a0ca9079ca 565 rcvReply(rcv,3000);
arsenalist 8:51a0ca9079ca 566 str = rcv;
arsenalist 8:51a0ca9079ca 567 if(str.find("MODE=S_OK") != std::string::npos)
arsenalist 8:51a0ca9079ca 568 break;
arsenalist 8:51a0ca9079ca 569 }
arsenalist 8:51a0ca9079ca 570 DBG.printf("entering settings mode\r\n");
arsenalist 17:b3ced0e67916 571 TFT.locate(0,0);
arsenalist 17:b3ced0e67916 572 TFT.printf(" emmaModeSettings");
arsenalist 17:b3ced0e67916 573 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 574 TFT.printf(" connect with emma app now!");
arsenalist 17:b3ced0e67916 575
arsenalist 8:51a0ca9079ca 576 while(1) {
arsenalist 8:51a0ca9079ca 577 char rcv[512] = {};
arsenalist 8:51a0ca9079ca 578 rcvReply(rcv,3000);
arsenalist 8:51a0ca9079ca 579 //DBG.printf("rcv:%s\r\n",rcv);
arsenalist 8:51a0ca9079ca 580 str = rcv;
arsenalist 8:51a0ca9079ca 581 if(str.find("MODE=S_Config") != std::string::npos) {
arsenalist 8:51a0ca9079ca 582 //save gprs and proxy setting
arsenalist 8:51a0ca9079ca 583 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 8:51a0ca9079ca 584 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 8:51a0ca9079ca 585 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 8:51a0ca9079ca 586
arsenalist 8:51a0ca9079ca 587 MbedJSONValue jsonValue;
arsenalist 8:51a0ca9079ca 588 parse(jsonValue,str.c_str());
arsenalist 8:51a0ca9079ca 589
arsenalist 14:8287f0f5d987 590 char *parameter[5] = {"gprsAPN","proxySERVER","proxyPORT","proxyAUTH","epochTime"};
arsenalist 8:51a0ca9079ca 591
arsenalist 8:51a0ca9079ca 592 for(int i=0; i<4; i++) {
arsenalist 8:51a0ca9079ca 593 if(jsonValue.hasMember(parameter[i])) {
arsenalist 8:51a0ca9079ca 594 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 8:51a0ca9079ca 595 int st = writeSetting(parameter[i],val.c_str());
arsenalist 8:51a0ca9079ca 596 if(st) {
arsenalist 8:51a0ca9079ca 597 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 17:b3ced0e67916 598 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 599 TFT.printf(" %s: %s is saved",parameter[i],val.c_str());
arsenalist 37:93f196daf5cf 600 wait(1);
arsenalist 17:b3ced0e67916 601 TFT.locate(0,40);
arsenalist 17:b3ced0e67916 602 TFT.printf(" ");
arsenalist 8:51a0ca9079ca 603 } else {
arsenalist 8:51a0ca9079ca 604 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 17:b3ced0e67916 605 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 606 TFT.printf(" %s is not saved",parameter[i]);
arsenalist 37:93f196daf5cf 607 wait(1);
arsenalist 17:b3ced0e67916 608 TFT.locate(0,40);
arsenalist 17:b3ced0e67916 609 TFT.printf(" ");
arsenalist 8:51a0ca9079ca 610 }
arsenalist 8:51a0ca9079ca 611 }
arsenalist 8:51a0ca9079ca 612 }
arsenalist 14:8287f0f5d987 613
arsenalist 14:8287f0f5d987 614 //set time
arsenalist 14:8287f0f5d987 615 if(jsonValue.hasMember(parameter[4])) {
arsenalist 14:8287f0f5d987 616 string epTime = jsonValue[parameter[4]].get<std::string>();
arsenalist 14:8287f0f5d987 617 time_t seconds;
arsenalist 14:8287f0f5d987 618 sscanf(epTime.c_str(),"%d",&seconds);
arsenalist 14:8287f0f5d987 619 set_time(seconds);
arsenalist 14:8287f0f5d987 620 DBG.printf("time is set\r\n");
arsenalist 17:b3ced0e67916 621 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 622 TFT.printf(" time is set");
arsenalist 37:93f196daf5cf 623 wait(1);
arsenalist 17:b3ced0e67916 624 TFT.locate(0,40);
arsenalist 17:b3ced0e67916 625 TFT.printf(" ");
arsenalist 14:8287f0f5d987 626 }
arsenalist 18:87f30ba9ddc5 627
arsenalist 18:87f30ba9ddc5 628 //setting finish
arsenalist 18:87f30ba9ddc5 629 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 630 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 631 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 632 TFT.printf(" settings finish. please restart.");
arsenalist 8:51a0ca9079ca 633 }
arsenalist 8:51a0ca9079ca 634 } else if(str.find("connect") != std::string::npos) {
arsenalist 8:51a0ca9079ca 635 DBG.printf("connection success!\r\n");
arsenalist 17:b3ced0e67916 636 TFT.locate(0,20);
arsenalist 17:b3ced0e67916 637 TFT.printf(" ");
arsenalist 17:b3ced0e67916 638 TFT.locate(0,20);
arsenalist 37:93f196daf5cf 639 TFT.printf(" connection success!");
arsenalist 8:51a0ca9079ca 640 }
arsenalist 8:51a0ca9079ca 641 }
arsenalist 0:f4e449fa34d7 642 } else {
arsenalist 9:a58c04da4476 643 DBG.printf("no eth or wifi is available\r\n");
arsenalist 17:b3ced0e67916 644 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 645 TFT.printf(" no iface avail, please restart!");
arsenalist 0:f4e449fa34d7 646 }
arsenalist 0:f4e449fa34d7 647 }
arsenalist 0:f4e449fa34d7 648 void emmaModeRegister(void) {
arsenalist 0:f4e449fa34d7 649 bool emmaGetRegKey = false;
arsenalist 0:f4e449fa34d7 650 bool emmaRegistered = false;
arsenalist 39:f51aa948acee 651 char connBody[256];
arsenalist 0:f4e449fa34d7 652 char s[512];
arsenalist 0:f4e449fa34d7 653 char r[256];
arsenalist 39:f51aa948acee 654 int connBodyLen;
arsenalist 0:f4e449fa34d7 655 int connPort;
arsenalist 0:f4e449fa34d7 656 int loop = 0;
arsenalist 0:f4e449fa34d7 657 string connData;
arsenalist 0:f4e449fa34d7 658 string connHost;
arsenalist 0:f4e449fa34d7 659 string str;
arsenalist 0:f4e449fa34d7 660 string regKey;
arsenalist 0:f4e449fa34d7 661 Timer t;
arsenalist 0:f4e449fa34d7 662
arsenalist 18:87f30ba9ddc5 663 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 664 TFT.printf(" please wait");
arsenalist 18:87f30ba9ddc5 665
arsenalist 0:f4e449fa34d7 666 //check connected interface
arsenalist 8:51a0ca9079ca 667 isEthConnected();
arsenalist 8:51a0ca9079ca 668 isWiFiConnected();
arsenalist 8:51a0ca9079ca 669 isGprsConnected();
arsenalist 0:f4e449fa34d7 670 DBG.printf("ethConnected:%d\r\n",ethConnected);
arsenalist 0:f4e449fa34d7 671 DBG.printf("wifiConnected:%d\r\n",wifiConnected);
arsenalist 0:f4e449fa34d7 672
arsenalist 9:a58c04da4476 673 if(ethConnected) {
arsenalist 9:a58c04da4476 674 DBG.printf("emmaModeRegister - eth\r\n");
arsenalist 38:e44f35f8129d 675 wait(2);
arsenalist 38:e44f35f8129d 676 TFT.locate(0,0);
arsenalist 38:e44f35f8129d 677 TFT.printf(" ");
arsenalist 38:e44f35f8129d 678 TFT.locate(0,0);
arsenalist 38:e44f35f8129d 679 TFT.printf(" emmaModeRegister");
arsenalist 9:a58c04da4476 680
arsenalist 39:f51aa948acee 681 //set connBody, connHost, connPort, connData
arsenalist 39:f51aa948acee 682 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 683
arsenalist 9:a58c04da4476 684 if(useProxy) {
arsenalist 9:a58c04da4476 685 DBG.printf("use proxy\r\n");
arsenalist 9:a58c04da4476 686 connHost = proxySERVER;
arsenalist 9:a58c04da4476 687 sscanf(proxyPORT.c_str(),"%d",&connPort);
arsenalist 9:a58c04da4476 688 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 689 s[i]=0; }
arsenalist 39:f51aa948acee 690 //sprintf(s,"GET http://%s:%s/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
arsenalist 39:f51aa948acee 691 //sprintf(s,"POST http://%s:%s/emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: 76\n\n{\"uid\":\"%s\",\"hmac\":\"%s\"}\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),emmaUID.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 692 sprintf(s,"POST http://%s:%s/emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 693 connData = s;
arsenalist 9:a58c04da4476 694 } else {
arsenalist 9:a58c04da4476 695 DBG.printf("no proxy\r\n");
arsenalist 38:e44f35f8129d 696 connHost = restSERVER;
arsenalist 38:e44f35f8129d 697 //connPort = restPORT;
arsenalist 38:e44f35f8129d 698 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 9:a58c04da4476 699 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 700 s[i]=0; }
arsenalist 39:f51aa948acee 701 //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 702 //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 39:f51aa948acee 703 sprintf(s,"POST /emma/api/controller/register HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 704 connData = s;
arsenalist 9:a58c04da4476 705 }
arsenalist 9:a58c04da4476 706
arsenalist 9:a58c04da4476 707 //register
arsenalist 9:a58c04da4476 708 while(!emmaGetRegKey) {
arsenalist 38:e44f35f8129d 709 //DBG.printf("post:%s\r\n",s);
arsenalist 38:e44f35f8129d 710 str.clear();
arsenalist 40:77bd44f57ad3 711 str = ethREST(connHost,connPort,connData);
arsenalist 9:a58c04da4476 712 DBG.printf("rsp reg:%s\r\n",str.c_str());
arsenalist 9:a58c04da4476 713
arsenalist 38:e44f35f8129d 714 //check and save mqtt setting
arsenalist 9:a58c04da4476 715 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 9:a58c04da4476 716 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 9:a58c04da4476 717 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 9:a58c04da4476 718
arsenalist 9:a58c04da4476 719 MbedJSONValue jsonValue;
arsenalist 9:a58c04da4476 720 parse(jsonValue,str.c_str());
arsenalist 9:a58c04da4476 721
arsenalist 38:e44f35f8129d 722 char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"};
arsenalist 9:a58c04da4476 723
arsenalist 38:e44f35f8129d 724 //save mqtt parameter
arsenalist 9:a58c04da4476 725 writeSetting(parameter[0],"()"); //sd card need to be initialized
arsenalist 9:a58c04da4476 726 for(int i=0; i<3; i++) {
arsenalist 9:a58c04da4476 727 if(jsonValue.hasMember(parameter[i])) {
arsenalist 9:a58c04da4476 728 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 9:a58c04da4476 729 int st = writeSetting(parameter[i],val.c_str());
arsenalist 9:a58c04da4476 730 if(st) {
arsenalist 9:a58c04da4476 731 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 9:a58c04da4476 732 } else {
arsenalist 9:a58c04da4476 733 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 9:a58c04da4476 734 }
arsenalist 9:a58c04da4476 735 }
arsenalist 9:a58c04da4476 736 }
arsenalist 9:a58c04da4476 737
arsenalist 9:a58c04da4476 738 //get registrationKey
arsenalist 9:a58c04da4476 739 if(jsonValue.hasMember(parameter[3])) {
arsenalist 9:a58c04da4476 740 string val = jsonValue[parameter[3]].get<std::string>();
arsenalist 9:a58c04da4476 741 if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
arsenalist 9:a58c04da4476 742 val.erase(val.begin(),val.begin()+val.find("(")+1);
arsenalist 9:a58c04da4476 743 val.erase(val.begin()+val.find(")"),val.end());
arsenalist 9:a58c04da4476 744 regKey = val;
arsenalist 9:a58c04da4476 745 DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 38:e44f35f8129d 746
arsenalist 38:e44f35f8129d 747 TFT.locate(0,20);
arsenalist 38:e44f35f8129d 748 TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 38:e44f35f8129d 749
arsenalist 9:a58c04da4476 750 emmaGetRegKey = true;
arsenalist 9:a58c04da4476 751 }
arsenalist 9:a58c04da4476 752 }
arsenalist 9:a58c04da4476 753 }
arsenalist 9:a58c04da4476 754 }
arsenalist 9:a58c04da4476 755
arsenalist 9:a58c04da4476 756 //calculate hmac
arsenalist 9:a58c04da4476 757 for(int i=0; i<sizeof(r); i++) {
arsenalist 9:a58c04da4476 758 r[i]=0; }
arsenalist 9:a58c04da4476 759 sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
arsenalist 9:a58c04da4476 760 hmac = calculateMD5(r);
arsenalist 9:a58c04da4476 761 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 9:a58c04da4476 762
arsenalist 39:f51aa948acee 763 //set connBody and connData
arsenalist 39:f51aa948acee 764 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 765
arsenalist 9:a58c04da4476 766 if(useProxy) {
arsenalist 9:a58c04da4476 767 DBG.printf("use proxy\r\n");
arsenalist 9:a58c04da4476 768 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 769 s[i]=0; }
arsenalist 39:f51aa948acee 770 //sprintf(s,"GET http://%s:%s/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),emmaUID.c_str(),regKey.c_str(),hmac.c_str(),restSERVER.c_str());
arsenalist 39:f51aa948acee 771 sprintf(s,"POST http://%s:%s/emma/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 772 connData = s;
arsenalist 9:a58c04da4476 773 } else {
arsenalist 9:a58c04da4476 774 DBG.printf("no proxy\r\n");
arsenalist 9:a58c04da4476 775 for(int i=0; i<sizeof(s); i++) {
arsenalist 9:a58c04da4476 776 s[i]=0; }
arsenalist 39:f51aa948acee 777 //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 39:f51aa948acee 778 sprintf(s,"POST /emma/api/controller/verify HTTP/1.0\nHost: %s\nContent-Length: %d\n\n%s\r\n\r\n",restSERVER.c_str(),connBodyLen,connBody);
arsenalist 9:a58c04da4476 779 connData = s;
arsenalist 9:a58c04da4476 780 }
arsenalist 9:a58c04da4476 781
arsenalist 9:a58c04da4476 782 //verify registration
arsenalist 9:a58c04da4476 783 while(!emmaRegistered && loop < 12){
arsenalist 9:a58c04da4476 784 str.clear();
arsenalist 40:77bd44f57ad3 785 str = ethREST(connHost,connPort,connData);
arsenalist 9:a58c04da4476 786 DBG.printf("rsp vrf:%s\r\n",str.c_str());
arsenalist 38:e44f35f8129d 787
arsenalist 38:e44f35f8129d 788 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 789 TFT.printf(" ");
arsenalist 38:e44f35f8129d 790 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 791 TFT.printf(" wait:%d\r\n",loop);
arsenalist 9:a58c04da4476 792
arsenalist 9:a58c04da4476 793 //check verification
arsenalist 9:a58c04da4476 794 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 9:a58c04da4476 795 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 9:a58c04da4476 796 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 9:a58c04da4476 797
arsenalist 9:a58c04da4476 798 MbedJSONValue jsonValue;
arsenalist 9:a58c04da4476 799 parse(jsonValue,str.c_str());
arsenalist 9:a58c04da4476 800 if(jsonValue.hasMember("user")) {
arsenalist 9:a58c04da4476 801 string val = jsonValue["user"].get<std::string>();
arsenalist 9:a58c04da4476 802 DBG.printf("%s is registered\r\n",val.c_str());
arsenalist 38:e44f35f8129d 803 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 804 TFT.printf(" ");
arsenalist 38:e44f35f8129d 805 TFT.locate(0,40);
arsenalist 38:e44f35f8129d 806 TFT.printf(" %s is registered\r\n",val.c_str());
arsenalist 9:a58c04da4476 807 emmaRegistered = true;
arsenalist 9:a58c04da4476 808 }
arsenalist 9:a58c04da4476 809 }
arsenalist 9:a58c04da4476 810 wait(5);
arsenalist 9:a58c04da4476 811 loop++;
arsenalist 9:a58c04da4476 812 }
arsenalist 9:a58c04da4476 813
arsenalist 9:a58c04da4476 814 //check whether registration success
arsenalist 9:a58c04da4476 815 if(emmaRegistered) {
arsenalist 9:a58c04da4476 816 DBG.printf("registration successful\r\n");
arsenalist 38:e44f35f8129d 817 TFT.locate(0,60);
arsenalist 38:e44f35f8129d 818 TFT.printf(" registration successful\r\n");
arsenalist 9:a58c04da4476 819 } else {
arsenalist 9:a58c04da4476 820 DBG.printf("registration unsuccessful\r\n");
arsenalist 38:e44f35f8129d 821 TFT.locate(0,60);
arsenalist 38:e44f35f8129d 822 TFT.printf(" registration unsuccessful. please restart.\r\n");
arsenalist 9:a58c04da4476 823 }
arsenalist 9:a58c04da4476 824 while(1);
arsenalist 9:a58c04da4476 825
arsenalist 9:a58c04da4476 826 } else if(wifiConnected) {
arsenalist 0:f4e449fa34d7 827 DBG.printf("emmaModeRegister - wifi\r\n");
arsenalist 0:f4e449fa34d7 828
arsenalist 0:f4e449fa34d7 829 _ESP.printf("MODE=B");
arsenalist 0:f4e449fa34d7 830 wait(1);
arsenalist 0:f4e449fa34d7 831 while(!esp.ready());
arsenalist 0:f4e449fa34d7 832
arsenalist 0:f4e449fa34d7 833 //set connHost, connPort
arsenalist 0:f4e449fa34d7 834 if(useProxy) {
arsenalist 0:f4e449fa34d7 835 connHost = proxySERVER;
arsenalist 0:f4e449fa34d7 836 sscanf(proxyPORT.c_str(),"%d",&connPort);
arsenalist 0:f4e449fa34d7 837 } else {
arsenalist 38:e44f35f8129d 838 connHost = restSERVER;
arsenalist 38:e44f35f8129d 839 //connPort = restPORT;
arsenalist 38:e44f35f8129d 840 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 0:f4e449fa34d7 841 }
arsenalist 18:87f30ba9ddc5 842 TFT.locate(0,0);
arsenalist 18:87f30ba9ddc5 843 TFT.printf(" emmaModeRegister");
arsenalist 0:f4e449fa34d7 844
arsenalist 0:f4e449fa34d7 845 //rest begin
arsenalist 0:f4e449fa34d7 846 if(!rest.begin(connHost.c_str(),connPort,false)) {
arsenalist 0:f4e449fa34d7 847 DBG.printf("EMMA: fail to setup rest");
arsenalist 32:78633fb9b2a1 848 TFT.locate(0,20);
arsenalist 32:78633fb9b2a1 849 TFT.printf(" fail setup connection");
arsenalist 32:78633fb9b2a1 850 TFT.locate(0,40);
arsenalist 32:78633fb9b2a1 851 TFT.printf(" check your wifi connection or");
arsenalist 32:78633fb9b2a1 852 TFT.locate(0,60);
arsenalist 32:78633fb9b2a1 853 TFT.printf(" you should setup proxy!");
arsenalist 0:f4e449fa34d7 854 while(1);
arsenalist 0:f4e449fa34d7 855 }
arsenalist 0:f4e449fa34d7 856 //wifiConnected = true; //with custom firmware, wifi module should connect automatically
arsenalist 0:f4e449fa34d7 857
arsenalist 0:f4e449fa34d7 858 esp.process();
arsenalist 0:f4e449fa34d7 859 if(wifiConnected) {
arsenalist 32:78633fb9b2a1 860 //check proxy
arsenalist 0:f4e449fa34d7 861 if(useProxy) {
arsenalist 39:f51aa948acee 862 sprintf(r,"http://%s:%s/emma/api/controller/register",restSERVER.c_str(),restPORT.c_str());
arsenalist 38:e44f35f8129d 863 //sprintf(r,"http://%s:%d/emma/api/controller/register HTTP/1.0\nHost: %s:%d\nProxy-Authorization: Basic %s\nCache-Control: no-cache\nContent-Length: %d\n\n",restSERVER,restPORT,restSERVER,restPORT,proxyAUTH.c_str(),76);
arsenalist 38:e44f35f8129d 864 //sprintf(r,"http://%s:%d/emma/api/controller/register HTTP/1.0\nAccept: */*\nAccept-Encoding: gzip,deflate\nContent-Length: %d\r\n\r\n",restSERVER,restPORT,76);
arsenalist 39:f51aa948acee 865 //sprintf(r,"http://%s:%d/emma/coba",restSERVER,REST_SERVER_PORT);
arsenalist 0:f4e449fa34d7 866 } else {
arsenalist 32:78633fb9b2a1 867 sprintf(r,"/emma/api/controller/register");
arsenalist 0:f4e449fa34d7 868 }
arsenalist 0:f4e449fa34d7 869
arsenalist 0:f4e449fa34d7 870 //register
arsenalist 0:f4e449fa34d7 871 while(!emmaGetRegKey) {
arsenalist 32:78633fb9b2a1 872 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 39:f51aa948acee 873 //DBG.printf("s:%s\r\n",s);
arsenalist 32:78633fb9b2a1 874 //string iniStr = "{\"uid\":\"005300553533510334313732\",\"hmac\":\"45fc6a4447989a9434d1f21087a78061\"}";
arsenalist 32:78633fb9b2a1 875 //DBG.printf("s:%s\r\n",iniStr.c_str());
arsenalist 32:78633fb9b2a1 876
arsenalist 32:78633fb9b2a1 877 //rest.post(r,iniStr.c_str());
arsenalist 32:78633fb9b2a1 878 rest.post(r,s);
arsenalist 0:f4e449fa34d7 879 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 880 s[i]=0; }
arsenalist 0:f4e449fa34d7 881 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 882 DBG.printf("rsp reg:%s\r\n",s);
arsenalist 0:f4e449fa34d7 883
arsenalist 38:e44f35f8129d 884 //check and save mqtt setting
arsenalist 0:f4e449fa34d7 885 str = s;
arsenalist 0:f4e449fa34d7 886 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 887 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 888 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 889
arsenalist 0:f4e449fa34d7 890 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 891 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 892
arsenalist 38:e44f35f8129d 893 char *parameter[4] = {"mqttDOMAIN","mqttKEY","mqttSECRET","registrationKey"};
arsenalist 0:f4e449fa34d7 894
arsenalist 38:e44f35f8129d 895 //save mqtt parameter
arsenalist 0:f4e449fa34d7 896 writeSetting(parameter[0],"()"); //sd card need to be initialized
arsenalist 0:f4e449fa34d7 897 for(int i=0; i<3; i++) {
arsenalist 0:f4e449fa34d7 898 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 899 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 0:f4e449fa34d7 900 int st = writeSetting(parameter[i],val.c_str());
arsenalist 0:f4e449fa34d7 901 if(st) {
arsenalist 0:f4e449fa34d7 902 DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
arsenalist 0:f4e449fa34d7 903 } else {
arsenalist 0:f4e449fa34d7 904 DBG.printf("%s is not saved\r\n",parameter[i]);
arsenalist 0:f4e449fa34d7 905 }
arsenalist 0:f4e449fa34d7 906 }
arsenalist 0:f4e449fa34d7 907 }
arsenalist 0:f4e449fa34d7 908
arsenalist 0:f4e449fa34d7 909 //get registrationKey
arsenalist 0:f4e449fa34d7 910 if(jsonValue.hasMember(parameter[3])) {
arsenalist 0:f4e449fa34d7 911 string val = jsonValue[parameter[3]].get<std::string>();
arsenalist 0:f4e449fa34d7 912 if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 913 val.erase(val.begin(),val.begin()+val.find("(")+1);
arsenalist 0:f4e449fa34d7 914 val.erase(val.begin()+val.find(")"),val.end());
arsenalist 0:f4e449fa34d7 915 regKey = val;
arsenalist 0:f4e449fa34d7 916 DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 18:87f30ba9ddc5 917
arsenalist 18:87f30ba9ddc5 918 TFT.locate(0,20);
arsenalist 18:87f30ba9ddc5 919 TFT.printf(" %s: %s\r\n",parameter[3],regKey.c_str());
arsenalist 18:87f30ba9ddc5 920
arsenalist 0:f4e449fa34d7 921 emmaGetRegKey = true;
arsenalist 0:f4e449fa34d7 922 }
arsenalist 0:f4e449fa34d7 923 }
arsenalist 0:f4e449fa34d7 924 }
arsenalist 32:78633fb9b2a1 925 wait(1);
arsenalist 0:f4e449fa34d7 926 }
arsenalist 0:f4e449fa34d7 927
arsenalist 0:f4e449fa34d7 928 //calculate hmac
arsenalist 0:f4e449fa34d7 929 for(int i=0; i<sizeof(r); i++) {
arsenalist 0:f4e449fa34d7 930 r[i]=0; }
arsenalist 0:f4e449fa34d7 931 sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
arsenalist 0:f4e449fa34d7 932 hmac = calculateMD5(r);
arsenalist 0:f4e449fa34d7 933 DBG.printf("hmac:%s\r\n",hmac.c_str());
arsenalist 0:f4e449fa34d7 934
arsenalist 32:78633fb9b2a1 935 //check proxy
arsenalist 0:f4e449fa34d7 936 if(useProxy) {
arsenalist 38:e44f35f8129d 937 sprintf(r,"http://%s:%d/emma/api/controller/verify",restSERVER.c_str(),restPORT.c_str());
arsenalist 0:f4e449fa34d7 938 } else {
arsenalist 32:78633fb9b2a1 939 sprintf(r,"/emma/api/controller/verify");
arsenalist 0:f4e449fa34d7 940 }
arsenalist 0:f4e449fa34d7 941
arsenalist 0:f4e449fa34d7 942 //verify registration
arsenalist 0:f4e449fa34d7 943 while(!emmaRegistered && loop < 12){
arsenalist 32:78633fb9b2a1 944 sprintf(s,"{\"uid\":\"%s\",\"registrationKey\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),regKey.c_str(),hmac.c_str());
arsenalist 32:78633fb9b2a1 945 rest.post(r,s);
arsenalist 0:f4e449fa34d7 946 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 947 DBG.printf("rsp vrf:%s\r\n",s);
arsenalist 18:87f30ba9ddc5 948 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 949 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 950 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 951 TFT.printf(" wait:%d\r\n",loop);
arsenalist 0:f4e449fa34d7 952
arsenalist 0:f4e449fa34d7 953 //check verification
arsenalist 0:f4e449fa34d7 954 str = s;
arsenalist 0:f4e449fa34d7 955 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 956 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 957 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 958
arsenalist 0:f4e449fa34d7 959 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 960 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 961 if(jsonValue.hasMember("user")) {
arsenalist 0:f4e449fa34d7 962 string val = jsonValue["user"].get<std::string>();
arsenalist 18:87f30ba9ddc5 963 DBG.printf(" %s is registered\r\n",val.c_str());
arsenalist 18:87f30ba9ddc5 964 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 965 TFT.printf(" ");
arsenalist 18:87f30ba9ddc5 966 TFT.locate(0,40);
arsenalist 18:87f30ba9ddc5 967 TFT.printf(" %s is registered\r\n",val.c_str());
arsenalist 0:f4e449fa34d7 968 emmaRegistered = true;
arsenalist 0:f4e449fa34d7 969 }
arsenalist 0:f4e449fa34d7 970 }
arsenalist 0:f4e449fa34d7 971 wait(5);
arsenalist 0:f4e449fa34d7 972 loop++;
arsenalist 0:f4e449fa34d7 973 }
arsenalist 0:f4e449fa34d7 974
arsenalist 0:f4e449fa34d7 975 //check whether registration success
arsenalist 0:f4e449fa34d7 976 if(emmaRegistered) {
arsenalist 0:f4e449fa34d7 977 DBG.printf("registration successful\r\n");
arsenalist 18:87f30ba9ddc5 978 TFT.locate(0,60);
arsenalist 18:87f30ba9ddc5 979 TFT.printf(" registration successful\r\n");
arsenalist 0:f4e449fa34d7 980 } else {
arsenalist 0:f4e449fa34d7 981 DBG.printf("registration unsuccessful\r\n");
arsenalist 18:87f30ba9ddc5 982 TFT.locate(0,60);
arsenalist 38:e44f35f8129d 983 TFT.printf(" registration unsuccessful. please restart.\r\n");
arsenalist 0:f4e449fa34d7 984 }
arsenalist 0:f4e449fa34d7 985 while(1);
arsenalist 0:f4e449fa34d7 986 }
arsenalist 0:f4e449fa34d7 987
arsenalist 9:a58c04da4476 988 } else if(gprsConnected) {
arsenalist 0:f4e449fa34d7 989 DBG.printf("emmaModeRegister - gprs\r\n");
arsenalist 0:f4e449fa34d7 990
arsenalist 0:f4e449fa34d7 991 } else {
arsenalist 9:a58c04da4476 992 DBG.printf("no eth, wifi, or gprs is connected\r\n");
arsenalist 18:87f30ba9ddc5 993 TFT.locate(0,60);
arsenalist 18:87f30ba9ddc5 994 TFT.printf(" no iface connected. please restart.\r\n");
arsenalist 0:f4e449fa34d7 995 }
arsenalist 0:f4e449fa34d7 996 }
arsenalist 0:f4e449fa34d7 997
arsenalist 0:f4e449fa34d7 998 void emmaModeOperation(void) {
arsenalist 27:562a95c120cc 999 //char mqttClientId[32];
arsenalist 40:77bd44f57ad3 1000 char connBody[256];
arsenalist 22:e18d361bf688 1001 char p[64];
arsenalist 14:8287f0f5d987 1002 char q[32];
arsenalist 4:76ab12e2f8a3 1003 char r[32];
arsenalist 34:8555d471b880 1004 char s[4096];
arsenalist 40:77bd44f57ad3 1005 int connBodyLen;
arsenalist 38:e44f35f8129d 1006 int connPort;
arsenalist 0:f4e449fa34d7 1007 int loop=0;
arsenalist 21:33bd8b82560f 1008 int trial=0;
arsenalist 22:e18d361bf688 1009 string hmacTime;
arsenalist 23:fb369e171b7d 1010 string hmacCmd;
arsenalist 0:f4e449fa34d7 1011 string str;
arsenalist 14:8287f0f5d987 1012 time_t seconds;
arsenalist 0:f4e449fa34d7 1013 Timer t;
arsenalist 24:5d58515ba510 1014 Timer tPanelEnergy;
arsenalist 24:5d58515ba510 1015 Timer tPanel;
arsenalist 4:76ab12e2f8a3 1016 Timer tNodes;
arsenalist 0:f4e449fa34d7 1017
arsenalist 19:7e3e9332f719 1018 TFT.locate(0,0);
arsenalist 19:7e3e9332f719 1019 TFT.printf(" please wait");
arsenalist 19:7e3e9332f719 1020
arsenalist 0:f4e449fa34d7 1021 //check connected interface
arsenalist 8:51a0ca9079ca 1022 isEthConnected();
arsenalist 8:51a0ca9079ca 1023 isWiFiConnected();
arsenalist 8:51a0ca9079ca 1024 isGprsConnected();
arsenalist 0:f4e449fa34d7 1025 DBG.printf("ethConnected:%d\r\n",ethConnected);
arsenalist 0:f4e449fa34d7 1026 DBG.printf("wifiConnected:%d\r\n",wifiConnected);
arsenalist 0:f4e449fa34d7 1027
arsenalist 13:e8adfe305dbc 1028 TFT.locate(0,0);
arsenalist 13:e8adfe305dbc 1029 TFT.printf(" emmaModeOperation");
arsenalist 13:e8adfe305dbc 1030
arsenalist 13:e8adfe305dbc 1031 TFT.locate(0,20);
arsenalist 13:e8adfe305dbc 1032 if(ethConnected) {
arsenalist 13:e8adfe305dbc 1033 TFT.printf("ETH:Avail");
arsenalist 13:e8adfe305dbc 1034 } else {
arsenalist 13:e8adfe305dbc 1035 TFT.printf("ETH:N/A");
arsenalist 13:e8adfe305dbc 1036 }
arsenalist 13:e8adfe305dbc 1037
arsenalist 13:e8adfe305dbc 1038 TFT.locate(80,20);
arsenalist 13:e8adfe305dbc 1039 if(wifiConnected) {
arsenalist 13:e8adfe305dbc 1040 TFT.printf("WiFi:Avail");
arsenalist 13:e8adfe305dbc 1041 } else {
arsenalist 13:e8adfe305dbc 1042 TFT.printf("WiFi:N/A");
arsenalist 13:e8adfe305dbc 1043 }
arsenalist 13:e8adfe305dbc 1044
arsenalist 13:e8adfe305dbc 1045 TFT.locate(160,20);
arsenalist 13:e8adfe305dbc 1046 if(gprsConnected) {
arsenalist 13:e8adfe305dbc 1047 TFT.printf("GPRS:Avail");
arsenalist 13:e8adfe305dbc 1048 } else {
arsenalist 13:e8adfe305dbc 1049 TFT.printf("GPRS:N/A");
arsenalist 13:e8adfe305dbc 1050 }
arsenalist 13:e8adfe305dbc 1051
arsenalist 10:1dc69e0ce7fd 1052 if(ethConnected) {
arsenalist 10:1dc69e0ce7fd 1053 DBG.printf("emmaModeOperation - eth\r\n");
arsenalist 37:93f196daf5cf 1054 /*
arsenalist 40:77bd44f57ad3 1055 //start new log indicator
arsenalist 40:77bd44f57ad3 1056 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1057 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1058 DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++Ethernet++++++++++"));
arsenalist 40:77bd44f57ad3 1059
arsenalist 40:77bd44f57ad3 1060 //set ade7758 parameter
arsenalist 40:77bd44f57ad3 1061 ADE.begin();
arsenalist 40:77bd44f57ad3 1062
arsenalist 40:77bd44f57ad3 1063 ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET);
arsenalist 40:77bd44f57ad3 1064
arsenalist 40:77bd44f57ad3 1065 ADE.write16bits(AWG, 0);
arsenalist 40:77bd44f57ad3 1066 ADE.write16bits(BWG, 0);
arsenalist 40:77bd44f57ad3 1067 ADE.write16bits(CWG, 0);
arsenalist 40:77bd44f57ad3 1068 ADE.write16bits(AVAG, 0);
arsenalist 40:77bd44f57ad3 1069 ADE.write16bits(BVAG, 0);
arsenalist 40:77bd44f57ad3 1070 ADE.write16bits(CVAG, 0);
arsenalist 40:77bd44f57ad3 1071
arsenalist 40:77bd44f57ad3 1072 //check firmware update
arsenalist 40:77bd44f57ad3 1073
arsenalist 40:77bd44f57ad3 1074 //execute last state of switches on board
arsenalist 40:77bd44f57ad3 1075
arsenalist 40:77bd44f57ad3 1076 //get list of nodes from server
arsenalist 40:77bd44f57ad3 1077 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
arsenalist 40:77bd44f57ad3 1078
arsenalist 40:77bd44f57ad3 1079 //sprintf(s,"GET /emma/api/controller/remotes?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),hmac.c_str(),restSERVER.c_str());
arsenalist 40:77bd44f57ad3 1080 sprintf(s,"POST /emma/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",restSERVER.c_str(),connBodyLen,connBody);
arsenalist 40:77bd44f57ad3 1081 str.clear();
arsenalist 40:77bd44f57ad3 1082 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 40:77bd44f57ad3 1083 str = ethREST(restSERVER,connPort,s);
arsenalist 40:77bd44f57ad3 1084 DBG.printf("str:%s\r\n",str.c_str());
arsenalist 40:77bd44f57ad3 1085 if(str.rfind("[{\"type\"") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1086 DBG.printf("get nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1087 str.erase(str.begin(),str.begin()+str.rfind("[{\"type\""));
arsenalist 40:77bd44f57ad3 1088 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 40:77bd44f57ad3 1089
arsenalist 40:77bd44f57ad3 1090 MbedJSONValue jsonValue;
arsenalist 40:77bd44f57ad3 1091 parse(jsonValue,str.c_str());
arsenalist 40:77bd44f57ad3 1092 char *parameter[3] = {"type","mac","ip"};
arsenalist 40:77bd44f57ad3 1093
arsenalist 40:77bd44f57ad3 1094 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1095 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1096 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1097 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 40:77bd44f57ad3 1098 wait(0.5);
arsenalist 40:77bd44f57ad3 1099 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1100 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1101
arsenalist 40:77bd44f57ad3 1102 //check whether nodes valid
arsenalist 40:77bd44f57ad3 1103 bool validNodes = true;
arsenalist 40:77bd44f57ad3 1104 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1105 for(int j=0; j<3; j++) {
arsenalist 40:77bd44f57ad3 1106 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 40:77bd44f57ad3 1107 }
arsenalist 40:77bd44f57ad3 1108 }
arsenalist 40:77bd44f57ad3 1109 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 40:77bd44f57ad3 1110
arsenalist 40:77bd44f57ad3 1111 if(validNodes) {
arsenalist 40:77bd44f57ad3 1112 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1113 string typeValue = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 40:77bd44f57ad3 1114 string macValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 40:77bd44f57ad3 1115 string ipValue = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 40:77bd44f57ad3 1116 nodes[i].type = typeValue;
arsenalist 40:77bd44f57ad3 1117 nodes[i].macAddr = macValue;
arsenalist 40:77bd44f57ad3 1118 nodes[i].ipAddr = ipValue;
arsenalist 40:77bd44f57ad3 1119 DBG.printf("nodes[%d]type:%s\r\n",i,nodes[i].type.c_str());
arsenalist 40:77bd44f57ad3 1120 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 40:77bd44f57ad3 1121 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 40:77bd44f57ad3 1122 }
arsenalist 36:ed696fd1b44e 1123 }
arsenalist 40:77bd44f57ad3 1124
arsenalist 40:77bd44f57ad3 1125 } else {
arsenalist 40:77bd44f57ad3 1126 DBG.printf("no nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1127 }
arsenalist 40:77bd44f57ad3 1128
arsenalist 40:77bd44f57ad3 1129 //define thread
arsenalist 40:77bd44f57ad3 1130 osThreadDef(energyThread, osPriorityBelowNormal, (8*DEFAULT_STACK_SIZE));
arsenalist 40:77bd44f57ad3 1131 //create thread
arsenalist 40:77bd44f57ad3 1132 osThreadCreate(osThread(energyThread),NULL);
arsenalist 40:77bd44f57ad3 1133
arsenalist 40:77bd44f57ad3 1134 tPanelEnergy.start();
arsenalist 40:77bd44f57ad3 1135 tPanel.start();
arsenalist 40:77bd44f57ad3 1136 tNodes.start();
arsenalist 40:77bd44f57ad3 1137 wait(1);
arsenalist 40:77bd44f57ad3 1138 while(1) {
arsenalist 40:77bd44f57ad3 1139 //checkVoltagePower(); //need revision to support ethernet
arsenalist 40:77bd44f57ad3 1140
arsenalist 40:77bd44f57ad3 1141 //panelEnergy, panelVoltage, and panelPower
arsenalist 40:77bd44f57ad3 1142 if(tPanelEnergy.read() > 30.0f) {
arsenalist 40:77bd44f57ad3 1143 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
arsenalist 40:77bd44f57ad3 1144 TFT.locate(0,60);
arsenalist 40:77bd44f57ad3 1145 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1146 TFT.locate(0,60);
arsenalist 40:77bd44f57ad3 1147 TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
arsenalist 40:77bd44f57ad3 1148
arsenalist 40:77bd44f57ad3 1149 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
arsenalist 40:77bd44f57ad3 1150 TFT.locate(0,80);
arsenalist 40:77bd44f57ad3 1151 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1152 TFT.locate(0,80);
arsenalist 40:77bd44f57ad3 1153 TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
arsenalist 40:77bd44f57ad3 1154
arsenalist 40:77bd44f57ad3 1155 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
arsenalist 40:77bd44f57ad3 1156 TFT.locate(0,100);
arsenalist 40:77bd44f57ad3 1157 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1158 TFT.locate(0,100);
arsenalist 40:77bd44f57ad3 1159 TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
arsenalist 40:77bd44f57ad3 1160
arsenalist 40:77bd44f57ad3 1161 if(newEnergyData) {
arsenalist 40:77bd44f57ad3 1162 //for(int i=1; i<4; i++) {
arsenalist 40:77bd44f57ad3 1163 for(int i=1; i<2; i++) {
arsenalist 40:77bd44f57ad3 1164 DBG.printf("sending channel: %d\r\n",i);
arsenalist 40:77bd44f57ad3 1165 if(i==1){
arsenalist 40:77bd44f57ad3 1166 XWattHr = AWattHr;
arsenalist 40:77bd44f57ad3 1167 XVrms = AVrms;
arsenalist 40:77bd44f57ad3 1168 XWatt = AWatt;
arsenalist 40:77bd44f57ad3 1169 } else if(i==2) {
arsenalist 40:77bd44f57ad3 1170 XWattHr = BWattHr;
arsenalist 40:77bd44f57ad3 1171 XVrms = BVrms;
arsenalist 40:77bd44f57ad3 1172 XWatt = BWatt;
arsenalist 40:77bd44f57ad3 1173 } else {
arsenalist 40:77bd44f57ad3 1174 XWattHr = CWattHr;
arsenalist 40:77bd44f57ad3 1175 XVrms = CVrms;
arsenalist 40:77bd44f57ad3 1176 XWatt = CWatt;
arsenalist 40:77bd44f57ad3 1177 }
arsenalist 40:77bd44f57ad3 1178
arsenalist 40:77bd44f57ad3 1179 if(XWattHr != 0.0f) {
arsenalist 40:77bd44f57ad3 1180 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1181 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1182
arsenalist 40:77bd44f57ad3 1183 //calculate hmacTime
arsenalist 40:77bd44f57ad3 1184 for(int j=0; j<sizeof(p); j++) {
arsenalist 40:77bd44f57ad3 1185 p[j]=0; }
arsenalist 40:77bd44f57ad3 1186 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 40:77bd44f57ad3 1187 hmacTime = calculateMD5(p);
arsenalist 40:77bd44f57ad3 1188
arsenalist 40:77bd44f57ad3 1189 connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 40:77bd44f57ad3 1190 emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
arsenalist 40:77bd44f57ad3 1191 //DBG.printf("dataEnergy:\r\n%s\r\n",connBody);
arsenalist 40:77bd44f57ad3 1192
arsenalist 40:77bd44f57ad3 1193 sprintf(r,"POST /emma/api/controller/energy/%d HTTP/1.0\nHost:%s\nContent-Length:%d\n\n%s\r\n\r\n",i,restSERVER.c_str(),connBodyLen,connBody);
arsenalist 40:77bd44f57ad3 1194
arsenalist 40:77bd44f57ad3 1195 str.clear();
arsenalist 40:77bd44f57ad3 1196 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 40:77bd44f57ad3 1197 str = ethREST(restSERVER,connPort,r);
arsenalist 40:77bd44f57ad3 1198 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1199 //logging purpose
arsenalist 40:77bd44f57ad3 1200 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1201 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1202 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData success"));
arsenalist 40:77bd44f57ad3 1203
arsenalist 40:77bd44f57ad3 1204 DBG.printf("send channel: %d success\r\n",i);
arsenalist 40:77bd44f57ad3 1205 TFT.foreground(Green);
arsenalist 40:77bd44f57ad3 1206 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1207 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1208 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1209 TFT.printf("send ch%d success",i);
arsenalist 40:77bd44f57ad3 1210 wait(1);
arsenalist 40:77bd44f57ad3 1211 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1212 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1213 TFT.foreground(White);
arsenalist 40:77bd44f57ad3 1214 } else {
arsenalist 40:77bd44f57ad3 1215 //logging purpose
arsenalist 40:77bd44f57ad3 1216 seconds = time(NULL);
arsenalist 40:77bd44f57ad3 1217 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1218 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData failed"));
arsenalist 40:77bd44f57ad3 1219
arsenalist 40:77bd44f57ad3 1220 DBG.printf("send channel: %d failed\r\n",i);
arsenalist 40:77bd44f57ad3 1221 TFT.foreground(Red);
arsenalist 40:77bd44f57ad3 1222 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1223 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1224 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1225 TFT.printf("send ch%d failed",i);
arsenalist 40:77bd44f57ad3 1226 wait(1);
arsenalist 40:77bd44f57ad3 1227 TFT.locate(0,120);
arsenalist 40:77bd44f57ad3 1228 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1229 TFT.foreground(White);
arsenalist 40:77bd44f57ad3 1230 }
arsenalist 40:77bd44f57ad3 1231 }
arsenalist 40:77bd44f57ad3 1232 }
arsenalist 40:77bd44f57ad3 1233
arsenalist 40:77bd44f57ad3 1234 newEnergyData = false;
arsenalist 40:77bd44f57ad3 1235 }
arsenalist 40:77bd44f57ad3 1236
arsenalist 40:77bd44f57ad3 1237 tPanelEnergy.reset();
arsenalist 40:77bd44f57ad3 1238 loop++;
arsenalist 36:ed696fd1b44e 1239 }
arsenalist 40:77bd44f57ad3 1240
arsenalist 40:77bd44f57ad3 1241 //panel environment
arsenalist 40:77bd44f57ad3 1242
arsenalist 40:77bd44f57ad3 1243 osDelay(5000);
arsenalist 10:1dc69e0ce7fd 1244 }
arsenalist 37:93f196daf5cf 1245 */
arsenalist 40:77bd44f57ad3 1246
arsenalist 10:1dc69e0ce7fd 1247 } else if(wifiConnected) {
arsenalist 0:f4e449fa34d7 1248 DBG.printf("emmaModeOperation - wifi\r\n");
arsenalist 29:4d94a03d38e0 1249
arsenalist 30:08d290158fa7 1250 //start new log and dbg indicator
arsenalist 29:4d94a03d38e0 1251 seconds = time(NULL);
arsenalist 29:4d94a03d38e0 1252 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 40:77bd44f57ad3 1253 DBG.printf("newLog:%d\r\n",writeLog(q,"++++++++++WiFi++++++++++"));
arsenalist 36:ed696fd1b44e 1254 //DBG.printf("newDbg:%d\r\n",writeDbg(q,"++++++++++++++++++++"));
arsenalist 0:f4e449fa34d7 1255
arsenalist 0:f4e449fa34d7 1256 //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work
arsenalist 0:f4e449fa34d7 1257 //_ESP.printf("MODE=B");
arsenalist 0:f4e449fa34d7 1258 //wait(1);
arsenalist 0:f4e449fa34d7 1259 //while(!esp.ready());
arsenalist 0:f4e449fa34d7 1260
arsenalist 24:5d58515ba510 1261 /*
arsenalist 0:f4e449fa34d7 1262 DBG.printf("emma: setup mqtt client\r\n");
arsenalist 0:f4e449fa34d7 1263 sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
arsenalist 0:f4e449fa34d7 1264
arsenalist 38:e44f35f8129d 1265 if(mqtt.begin(mqttClientId, mqttKEY.c_str(), mqttSECRET.c_str(), 120, 1)) {
arsenalist 0:f4e449fa34d7 1266 mqtt.connectedCb.attach(&mqttConnected);
arsenalist 0:f4e449fa34d7 1267 mqtt.disconnectedCb.attach(&mqttDisconnected);
arsenalist 38:e44f35f8129d 1268 mqtt.connect(mqttSERVER,mqttPORT,false);
arsenalist 13:e8adfe305dbc 1269 DBG.printf("emma: success to setup mqtt\r\n");
arsenalist 13:e8adfe305dbc 1270 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1271 TFT.printf("emma: success to setup mqtt");
arsenalist 0:f4e449fa34d7 1272 }
arsenalist 0:f4e449fa34d7 1273 DBG.printf("emma: system started\r\n");
arsenalist 24:5d58515ba510 1274 */
arsenalist 0:f4e449fa34d7 1275
arsenalist 24:5d58515ba510 1276 //t.start();
arsenalist 24:5d58515ba510 1277 //while(t.read_ms() < 5000) {
arsenalist 24:5d58515ba510 1278 // esp.process();
arsenalist 24:5d58515ba510 1279 //}
arsenalist 24:5d58515ba510 1280 //t.stop();
arsenalist 24:5d58515ba510 1281 //t.reset();
arsenalist 0:f4e449fa34d7 1282
arsenalist 0:f4e449fa34d7 1283 //set ade7758 parameter
arsenalist 0:f4e449fa34d7 1284 ADE.begin();
arsenalist 0:f4e449fa34d7 1285
arsenalist 0:f4e449fa34d7 1286 ADE.writeRMSOffset(AIRMSOFFSET, BIRMSOFFSET, CIRMSOFFSET, AVRMSOFFSET, BVRMSOFFSET, CVRMSOFFSET);
arsenalist 0:f4e449fa34d7 1287
arsenalist 0:f4e449fa34d7 1288 ADE.write16bits(AWG, 0);
arsenalist 0:f4e449fa34d7 1289 ADE.write16bits(BWG, 0);
arsenalist 0:f4e449fa34d7 1290 ADE.write16bits(CWG, 0);
arsenalist 0:f4e449fa34d7 1291 ADE.write16bits(AVAG, 0);
arsenalist 0:f4e449fa34d7 1292 ADE.write16bits(BVAG, 0);
arsenalist 0:f4e449fa34d7 1293 ADE.write16bits(CVAG, 0);
arsenalist 0:f4e449fa34d7 1294
arsenalist 4:76ab12e2f8a3 1295 //init rest to server
arsenalist 38:e44f35f8129d 1296 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 38:e44f35f8129d 1297 if(rest.begin(restSERVER.c_str(),connPort,false)) {
arsenalist 4:76ab12e2f8a3 1298 DBG.printf("rest to server is created\r\n");
arsenalist 13:e8adfe305dbc 1299 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1300 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1301 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1302 TFT.printf("rest to server is created");
arsenalist 4:76ab12e2f8a3 1303 } else {
arsenalist 4:76ab12e2f8a3 1304 DBG.printf("rest to server is NOT created\r\n");
arsenalist 13:e8adfe305dbc 1305 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1306 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1307 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1308 TFT.printf("rest to server is NOT created");
arsenalist 4:76ab12e2f8a3 1309 }
arsenalist 4:76ab12e2f8a3 1310
arsenalist 7:7e8c6ad3fd64 1311 //check firmware update
arsenalist 7:7e8c6ad3fd64 1312
arsenalist 7:7e8c6ad3fd64 1313 //execute last state of switches on board
arsenalist 7:7e8c6ad3fd64 1314
arsenalist 7:7e8c6ad3fd64 1315 //get list of nodes from server
arsenalist 40:77bd44f57ad3 1316 /*
arsenalist 40:77bd44f57ad3 1317 //working
arsenalist 7:7e8c6ad3fd64 1318 sprintf(s,"/emma/api/controller/remotes?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
arsenalist 7:7e8c6ad3fd64 1319 rest.get(s);
arsenalist 7:7e8c6ad3fd64 1320 for(int i=0; i<sizeof(s); i++) {
arsenalist 7:7e8c6ad3fd64 1321 s[i]=0; }
arsenalist 7:7e8c6ad3fd64 1322 rest.getResponse(s,sizeof(s));
arsenalist 7:7e8c6ad3fd64 1323 str = s;
arsenalist 7:7e8c6ad3fd64 1324 if(str.rfind("[{\"mac\"") != std::string::npos) {
arsenalist 7:7e8c6ad3fd64 1325 DBG.printf("get nodes from server\r\n");
arsenalist 7:7e8c6ad3fd64 1326 str.erase(str.begin(),str.begin()+str.rfind("[{\"mac\""));
arsenalist 7:7e8c6ad3fd64 1327 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 7:7e8c6ad3fd64 1328
arsenalist 7:7e8c6ad3fd64 1329 MbedJSONValue jsonValue;
arsenalist 7:7e8c6ad3fd64 1330 parse(jsonValue,str.c_str());
arsenalist 7:7e8c6ad3fd64 1331 char *parameter[2] = {"mac","ip"};
arsenalist 7:7e8c6ad3fd64 1332
arsenalist 13:e8adfe305dbc 1333 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1334 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1335 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1336 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 13:e8adfe305dbc 1337 wait(0.5);
arsenalist 13:e8adfe305dbc 1338 TFT.locate(0,40);
arsenalist 13:e8adfe305dbc 1339 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1340
arsenalist 7:7e8c6ad3fd64 1341 //check whether nodes valid
arsenalist 7:7e8c6ad3fd64 1342 bool validNodes = true;
arsenalist 7:7e8c6ad3fd64 1343 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 7:7e8c6ad3fd64 1344 for(int j=0; j<2; j++) {
arsenalist 7:7e8c6ad3fd64 1345 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 7:7e8c6ad3fd64 1346 }
arsenalist 7:7e8c6ad3fd64 1347 }
arsenalist 7:7e8c6ad3fd64 1348 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 7:7e8c6ad3fd64 1349
arsenalist 7:7e8c6ad3fd64 1350 if(validNodes) {
arsenalist 7:7e8c6ad3fd64 1351 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 7:7e8c6ad3fd64 1352 string macValue = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 7:7e8c6ad3fd64 1353 string ipValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 7:7e8c6ad3fd64 1354 nodes[i].macAddr = macValue;
arsenalist 7:7e8c6ad3fd64 1355 nodes[i].ipAddr = ipValue;
arsenalist 13:e8adfe305dbc 1356 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 13:e8adfe305dbc 1357 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 7:7e8c6ad3fd64 1358 }
arsenalist 7:7e8c6ad3fd64 1359 }
arsenalist 7:7e8c6ad3fd64 1360
arsenalist 7:7e8c6ad3fd64 1361 } else {
arsenalist 7:7e8c6ad3fd64 1362 DBG.printf("no nodes from server\r\n");
arsenalist 7:7e8c6ad3fd64 1363 }
arsenalist 40:77bd44f57ad3 1364 */
arsenalist 40:77bd44f57ad3 1365
arsenalist 40:77bd44f57ad3 1366 _ESP.attach(&rxInterrupt,Serial::RxIrq);
arsenalist 40:77bd44f57ad3 1367
arsenalist 40:77bd44f57ad3 1368 //get list of nodes from server (post)
arsenalist 40:77bd44f57ad3 1369 sprintf(p,"/emma/api/controller/wifinodes");
arsenalist 40:77bd44f57ad3 1370 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmacTime.c_str());
arsenalist 40:77bd44f57ad3 1371 rest.post(p,s);
arsenalist 40:77bd44f57ad3 1372 wait(2);
arsenalist 40:77bd44f57ad3 1373 str = rxBuf;
arsenalist 40:77bd44f57ad3 1374 if(str.rfind("[{\"type\"") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1375 DBG.printf("get nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1376 str.erase(str.begin(),str.begin()+str.rfind("[{\"type\""));
arsenalist 40:77bd44f57ad3 1377 str.erase(str.begin()+str.rfind("}]")+2,str.end());
arsenalist 40:77bd44f57ad3 1378 //DBG.printf("strCrop:%s\r\n",str.c_str());
arsenalist 40:77bd44f57ad3 1379
arsenalist 40:77bd44f57ad3 1380 //start special handler
arsenalist 40:77bd44f57ad3 1381 while(1){
arsenalist 40:77bd44f57ad3 1382 if(str.find("}],") != std::string::npos) {
arsenalist 40:77bd44f57ad3 1383 str.erase(str.begin()+str.find("}],")+1,str.begin()+str.find("}],")+2);
arsenalist 40:77bd44f57ad3 1384 } else {
arsenalist 40:77bd44f57ad3 1385 break;
arsenalist 40:77bd44f57ad3 1386 }
arsenalist 40:77bd44f57ad3 1387 }
arsenalist 40:77bd44f57ad3 1388 //end special handler
arsenalist 40:77bd44f57ad3 1389
arsenalist 40:77bd44f57ad3 1390 DBG.printf("strCrop:%s\r\n",str.c_str());
arsenalist 40:77bd44f57ad3 1391
arsenalist 40:77bd44f57ad3 1392 MbedJSONValue jsonValue;
arsenalist 40:77bd44f57ad3 1393 parse(jsonValue,str.c_str());
arsenalist 40:77bd44f57ad3 1394 char *parameter[3] = {"type","mac","ip"};
arsenalist 40:77bd44f57ad3 1395
arsenalist 40:77bd44f57ad3 1396 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1397 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1398 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1399 TFT.printf("get %d nodes from server",jsonValue.size());
arsenalist 40:77bd44f57ad3 1400 wait(0.5);
arsenalist 40:77bd44f57ad3 1401 TFT.locate(0,40);
arsenalist 40:77bd44f57ad3 1402 TFT.printf(" ");
arsenalist 40:77bd44f57ad3 1403
arsenalist 40:77bd44f57ad3 1404 //check whether nodes valid
arsenalist 40:77bd44f57ad3 1405 bool validNodes = true;
arsenalist 40:77bd44f57ad3 1406 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1407 for(int j=0; j<3; j++) {
arsenalist 40:77bd44f57ad3 1408 validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
arsenalist 40:77bd44f57ad3 1409 }
arsenalist 40:77bd44f57ad3 1410 }
arsenalist 40:77bd44f57ad3 1411 DBG.printf("nodes validity:%d\r\n",validNodes);
arsenalist 40:77bd44f57ad3 1412
arsenalist 40:77bd44f57ad3 1413 if(validNodes) {
arsenalist 40:77bd44f57ad3 1414 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 40:77bd44f57ad3 1415 int typeValue = jsonValue[i][parameter[0]].get<int>();
arsenalist 40:77bd44f57ad3 1416 string macValue = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 40:77bd44f57ad3 1417 string ipValue = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 40:77bd44f57ad3 1418 nodes[i].type = typeValue;
arsenalist 40:77bd44f57ad3 1419 nodes[i].macAddr = macValue;
arsenalist 40:77bd44f57ad3 1420 nodes[i].ipAddr = ipValue;
arsenalist 40:77bd44f57ad3 1421 DBG.printf("nodes[%d]type:%d\r\n",i,nodes[i].type);
arsenalist 40:77bd44f57ad3 1422 DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
arsenalist 40:77bd44f57ad3 1423 DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
arsenalist 40:77bd44f57ad3 1424 }
arsenalist 40:77bd44f57ad3 1425 }
arsenalist 40:77bd44f57ad3 1426
arsenalist 40:77bd44f57ad3 1427 } else {
arsenalist 40:77bd44f57ad3 1428 DBG.printf("no nodes from server\r\n");
arsenalist 40:77bd44f57ad3 1429 }
arsenalist 7:7e8c6ad3fd64 1430
arsenalist 24:5d58515ba510 1431 /*
arsenalist 36:ed696fd1b44e 1432 //working
arsenalist 24:5d58515ba510 1433 DBG.printf("emma: setup mqtt client\r\n");
arsenalist 24:5d58515ba510 1434 sprintf(mqttClientId,"emma/%s",emmaUID.c_str());
arsenalist 38:e44f35f8129d 1435 if(mqtt.begin(mqttClientId, mqttKEY.c_str(), mqttSECRET.c_str(), 120, 1)) {
arsenalist 24:5d58515ba510 1436 mqtt.connectedCb.attach(&mqttConnected);
arsenalist 24:5d58515ba510 1437 mqtt.disconnectedCb.attach(&mqttDisconnected);
arsenalist 38:e44f35f8129d 1438 mqtt.connect(mqttSERVER,mqttPORT,false);
arsenalist 24:5d58515ba510 1439 DBG.printf("emma: success to setup mqtt\r\n");
arsenalist 24:5d58515ba510 1440 TFT.locate(0,40);
arsenalist 24:5d58515ba510 1441 TFT.printf("emma: success to setup mqtt");
arsenalist 24:5d58515ba510 1442 }
arsenalist 24:5d58515ba510 1443 DBG.printf("emma: system started\r\n");
arsenalist 24:5d58515ba510 1444
arsenalist 24:5d58515ba510 1445 t.start();
arsenalist 24:5d58515ba510 1446 while(t.read_ms() < 5000) {
arsenalist 24:5d58515ba510 1447 esp.process();
arsenalist 24:5d58515ba510 1448 }
arsenalist 24:5d58515ba510 1449 t.stop();
arsenalist 24:5d58515ba510 1450 t.reset();
arsenalist 24:5d58515ba510 1451 */
arsenalist 7:7e8c6ad3fd64 1452
arsenalist 40:77bd44f57ad3 1453 //disable UART2
arsenalist 40:77bd44f57ad3 1454 NVIC_DisableIRQ(USART2_IRQn);
arsenalist 40:77bd44f57ad3 1455
arsenalist 4:76ab12e2f8a3 1456 //init rest to remotes
arsenalist 4:76ab12e2f8a3 1457 for(int i=0; i<NODES_MAX; i++) {
arsenalist 4:76ab12e2f8a3 1458 if(!nodes[i].ipAddr.empty()) {
arsenalist 4:76ab12e2f8a3 1459 DBG.printf("restConn nodes[%d] is created\r\n",i);
arsenalist 4:76ab12e2f8a3 1460 nodes[i].restConn->begin(nodes[i].ipAddr.c_str(),16038,false);
arsenalist 4:76ab12e2f8a3 1461 wait(1);
arsenalist 4:76ab12e2f8a3 1462 } else {
arsenalist 4:76ab12e2f8a3 1463 DBG.printf("restConn nodes[%d] is NOT created\r\n",i);
arsenalist 4:76ab12e2f8a3 1464 wait(1);
arsenalist 4:76ab12e2f8a3 1465 }
arsenalist 4:76ab12e2f8a3 1466 }
arsenalist 4:76ab12e2f8a3 1467
arsenalist 40:77bd44f57ad3 1468 //enable UART2
arsenalist 40:77bd44f57ad3 1469 NVIC_EnableIRQ(USART2_IRQn);
arsenalist 40:77bd44f57ad3 1470
arsenalist 40:77bd44f57ad3 1471 //_ESP.attach(&rxInterrupt,Serial::RxIrq);
arsenalist 4:76ab12e2f8a3 1472
arsenalist 4:76ab12e2f8a3 1473 //define thread
arsenalist 17:b3ced0e67916 1474 osThreadDef(energyThread, osPriorityBelowNormal, (8*DEFAULT_STACK_SIZE));
arsenalist 0:f4e449fa34d7 1475 //create thread
arsenalist 2:fdfdeb5fd6fd 1476 osThreadCreate(osThread(energyThread),NULL);
arsenalist 0:f4e449fa34d7 1477
arsenalist 24:5d58515ba510 1478 tPanelEnergy.start();
arsenalist 24:5d58515ba510 1479 tPanel.start();
arsenalist 4:76ab12e2f8a3 1480 tNodes.start();
arsenalist 4:76ab12e2f8a3 1481 wait(1);
arsenalist 0:f4e449fa34d7 1482 while(1) {
arsenalist 4:76ab12e2f8a3 1483 checkRxBuffer();
arsenalist 20:ea14f175bbb4 1484 checkVoltagePower();
arsenalist 20:ea14f175bbb4 1485
arsenalist 24:5d58515ba510 1486 //whether espFreeMemory occurs
arsenalist 24:5d58515ba510 1487 if(espFreeMemory) {
arsenalist 30:08d290158fa7 1488 //logging
arsenalist 27:562a95c120cc 1489 seconds = time(NULL);
arsenalist 27:562a95c120cc 1490 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1491 DBG.printf("logFreeMemory:%d\r\n",writeLog(q,rxLog.c_str()));
arsenalist 24:5d58515ba510 1492
arsenalist 27:562a95c120cc 1493 espFreeMemory = false;
arsenalist 27:562a95c120cc 1494 }
arsenalist 27:562a95c120cc 1495
arsenalist 27:562a95c120cc 1496 //whether espDHCPClientStart occurs
arsenalist 27:562a95c120cc 1497 if(espDHCPClientStart) {
arsenalist 30:08d290158fa7 1498 //logging
arsenalist 27:562a95c120cc 1499 seconds = time(NULL);
arsenalist 27:562a95c120cc 1500 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1501 DBG.printf("logDHCPClientStart:%d\r\n",writeLog(q,rxLogA.c_str()));
arsenalist 27:562a95c120cc 1502
arsenalist 27:562a95c120cc 1503 _ESP.printf("MODE=B");
arsenalist 27:562a95c120cc 1504 wait(2);
arsenalist 27:562a95c120cc 1505 espDHCPClientStart = false;
arsenalist 24:5d58515ba510 1506 }
arsenalist 24:5d58515ba510 1507
arsenalist 4:76ab12e2f8a3 1508 //panelEnergy, panelVoltage, and panelPower
arsenalist 24:5d58515ba510 1509 if(tPanelEnergy.read() > 30.0f) {
arsenalist 4:76ab12e2f8a3 1510 DBG.printf("[%d]WattHR for each phase: %.2f, %.2f, %.2f\r\n", loop, AWattHr, BWattHr, CWattHr);
arsenalist 13:e8adfe305dbc 1511 TFT.locate(0,60);
arsenalist 13:e8adfe305dbc 1512 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1513 TFT.locate(0,60);
arsenalist 13:e8adfe305dbc 1514 TFT.printf("[%d]WHR: %.1f, %.1f, %.1f", loop, AWattHr, BWattHr, CWattHr);
arsenalist 13:e8adfe305dbc 1515
arsenalist 4:76ab12e2f8a3 1516 DBG.printf("VRMS for each phase: %.2f, %.2f, %.2f\r\n", AVrms, BVrms, CVrms);
arsenalist 13:e8adfe305dbc 1517 TFT.locate(0,80);
arsenalist 13:e8adfe305dbc 1518 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1519 TFT.locate(0,80);
arsenalist 13:e8adfe305dbc 1520 TFT.printf("VRMS: %.1f, %.1f, %.1f", AVrms, BVrms, CVrms);
arsenalist 13:e8adfe305dbc 1521
arsenalist 4:76ab12e2f8a3 1522 DBG.printf("Watt for each phase: %.2f, %.2f, %.2f\r\n", AWatt, BWatt, CWatt);
arsenalist 13:e8adfe305dbc 1523 TFT.locate(0,100);
arsenalist 13:e8adfe305dbc 1524 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1525 TFT.locate(0,100);
arsenalist 13:e8adfe305dbc 1526 TFT.printf("Watt: %.1f, %.1f, %.1f", AWatt, BWatt, CWatt);
arsenalist 4:76ab12e2f8a3 1527
arsenalist 22:e18d361bf688 1528 if(newEnergyData) {
arsenalist 22:e18d361bf688 1529 //for(int i=1; i<4; i++) {
arsenalist 22:e18d361bf688 1530 for(int i=1; i<2; i++) {
arsenalist 22:e18d361bf688 1531 DBG.printf("sending channel: %d\r\n",i);
arsenalist 22:e18d361bf688 1532 if(i==1){
arsenalist 22:e18d361bf688 1533 XWattHr = AWattHr;
arsenalist 22:e18d361bf688 1534 XVrms = AVrms;
arsenalist 22:e18d361bf688 1535 XWatt = AWatt;
arsenalist 22:e18d361bf688 1536 } else if(i==2) {
arsenalist 22:e18d361bf688 1537 XWattHr = BWattHr;
arsenalist 22:e18d361bf688 1538 XVrms = BVrms;
arsenalist 22:e18d361bf688 1539 XWatt = BWatt;
arsenalist 22:e18d361bf688 1540 } else {
arsenalist 22:e18d361bf688 1541 XWattHr = CWattHr;
arsenalist 22:e18d361bf688 1542 XVrms = CVrms;
arsenalist 22:e18d361bf688 1543 XWatt = CWatt;
arsenalist 22:e18d361bf688 1544 }
arsenalist 24:5d58515ba510 1545
arsenalist 24:5d58515ba510 1546 if(XWattHr != 0.0f) {
arsenalist 22:e18d361bf688 1547
arsenalist 24:5d58515ba510 1548 sprintf(r,"/emma/api/controller/energy/%d",i);
arsenalist 24:5d58515ba510 1549 seconds = time(NULL);
arsenalist 24:5d58515ba510 1550 //for(int j=0; j<sizeof(q); j++) {
arsenalist 24:5d58515ba510 1551 // q[j]=0; }
arsenalist 24:5d58515ba510 1552 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 24:5d58515ba510 1553
arsenalist 24:5d58515ba510 1554 //calculate hmacTime
arsenalist 24:5d58515ba510 1555 for(int j=0; j<sizeof(p); j++) {
arsenalist 24:5d58515ba510 1556 p[j]=0; }
arsenalist 24:5d58515ba510 1557 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 24:5d58515ba510 1558 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 1559
arsenalist 24:5d58515ba510 1560 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"energy\":%.2f,\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 24:5d58515ba510 1561 emmaUID.c_str(),hmacTime.c_str(),q,XWattHr,XVrms,XWatt);
arsenalist 24:5d58515ba510 1562 //DBG.printf("dataEnergy:\r\n%s\r\n",s);
arsenalist 24:5d58515ba510 1563 rest.post(r,s);
arsenalist 24:5d58515ba510 1564 wait(2);
arsenalist 24:5d58515ba510 1565 if(rxBuf.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 27:562a95c120cc 1566 //logging purpose
arsenalist 27:562a95c120cc 1567 seconds = time(NULL);
arsenalist 27:562a95c120cc 1568 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1569 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData success"));
arsenalist 27:562a95c120cc 1570
arsenalist 24:5d58515ba510 1571 DBG.printf("send channel: %d success\r\n",i);
arsenalist 24:5d58515ba510 1572 TFT.foreground(Green);
arsenalist 24:5d58515ba510 1573 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1574 TFT.printf(" ");
arsenalist 24:5d58515ba510 1575 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1576 TFT.printf("send ch%d success",i);
arsenalist 24:5d58515ba510 1577 wait(1);
arsenalist 24:5d58515ba510 1578 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1579 TFT.printf(" ");
arsenalist 24:5d58515ba510 1580 TFT.foreground(White);
arsenalist 24:5d58515ba510 1581 } else {
arsenalist 27:562a95c120cc 1582 //logging purpose
arsenalist 27:562a95c120cc 1583 seconds = time(NULL);
arsenalist 27:562a95c120cc 1584 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1585 DBG.printf("logPE:%d\r\n",writeLog(q,"sendEnergyData failed"));
arsenalist 27:562a95c120cc 1586
arsenalist 24:5d58515ba510 1587 DBG.printf("send channel: %d failed\r\n",i);
arsenalist 24:5d58515ba510 1588 TFT.foreground(Red);
arsenalist 24:5d58515ba510 1589 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1590 TFT.printf(" ");
arsenalist 24:5d58515ba510 1591 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1592 TFT.printf("send ch%d failed",i);
arsenalist 24:5d58515ba510 1593 wait(1);
arsenalist 24:5d58515ba510 1594 TFT.locate(0,120);
arsenalist 24:5d58515ba510 1595 TFT.printf(" ");
arsenalist 24:5d58515ba510 1596 TFT.foreground(White);
arsenalist 24:5d58515ba510 1597 }
arsenalist 22:e18d361bf688 1598 }
arsenalist 4:76ab12e2f8a3 1599 }
arsenalist 22:e18d361bf688 1600
arsenalist 22:e18d361bf688 1601 newEnergyData = false;
arsenalist 0:f4e449fa34d7 1602 }
arsenalist 22:e18d361bf688 1603
arsenalist 24:5d58515ba510 1604 tPanelEnergy.reset();
arsenalist 4:76ab12e2f8a3 1605 loop++;
arsenalist 0:f4e449fa34d7 1606 }
arsenalist 0:f4e449fa34d7 1607
arsenalist 21:33bd8b82560f 1608 //panel environment
arsenalist 5:d00233dd36f5 1609 checkRxBuffer();
arsenalist 25:36c6a5db50ed 1610 if(tPanel.read() > 900.0f) { //900 is 15 minutes
arsenalist 21:33bd8b82560f 1611 int dTemp=0;
arsenalist 21:33bd8b82560f 1612 int dHum=0;
arsenalist 21:33bd8b82560f 1613 int dGas=0;
arsenalist 21:33bd8b82560f 1614
arsenalist 21:33bd8b82560f 1615 DBG.printf("getPanelEnvironment\r\n");
arsenalist 21:33bd8b82560f 1616
arsenalist 21:33bd8b82560f 1617 //get environment sensor
arsenalist 21:33bd8b82560f 1618 trial=0;
arsenalist 21:33bd8b82560f 1619 while(1) {
arsenalist 21:33bd8b82560f 1620 if(trial>=2) { //two times trial
arsenalist 21:33bd8b82560f 1621 break;
arsenalist 21:33bd8b82560f 1622 }
arsenalist 21:33bd8b82560f 1623 if(d.readData() == DHT11::OK) {
arsenalist 21:33bd8b82560f 1624 dTemp = d.readTemperature();
arsenalist 21:33bd8b82560f 1625 dHum = d.readHumidity();
arsenalist 21:33bd8b82560f 1626 break;
arsenalist 21:33bd8b82560f 1627 }
arsenalist 21:33bd8b82560f 1628 trial++;
arsenalist 21:33bd8b82560f 1629 wait(3);
arsenalist 21:33bd8b82560f 1630 }
arsenalist 21:33bd8b82560f 1631
arsenalist 21:33bd8b82560f 1632 //send environment sensor
arsenalist 24:5d58515ba510 1633 if(dTemp!=0 && dHum!=0) {
arsenalist 24:5d58515ba510 1634 seconds = time(NULL);
arsenalist 24:5d58515ba510 1635 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 22:e18d361bf688 1636
arsenalist 24:5d58515ba510 1637 //calculate hmacTime
arsenalist 24:5d58515ba510 1638 for(int j=0; j<sizeof(p); j++) {
arsenalist 24:5d58515ba510 1639 p[j]=0; }
arsenalist 24:5d58515ba510 1640 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 24:5d58515ba510 1641 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 1642
arsenalist 24:5d58515ba510 1643 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}",
arsenalist 24:5d58515ba510 1644 emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas);
arsenalist 24:5d58515ba510 1645 //DBG.printf("dataEnvironment:\r\n%s\r\n",s);
arsenalist 24:5d58515ba510 1646 rest.post("/emma/api/controller/environment",s);
arsenalist 24:5d58515ba510 1647 wait(2);
arsenalist 24:5d58515ba510 1648 str = rxBuf;
arsenalist 24:5d58515ba510 1649 if(str.rfind("/environment") != std::string::npos) {
arsenalist 24:5d58515ba510 1650 str.erase(str.begin(),str.begin()+str.rfind("/environment"));
arsenalist 24:5d58515ba510 1651 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 27:562a95c120cc 1652 //logging purpose
arsenalist 27:562a95c120cc 1653 seconds = time(NULL);
arsenalist 27:562a95c120cc 1654 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1655 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv success"));
arsenalist 24:5d58515ba510 1656 DBG.printf("send panel environment success\r\n");
arsenalist 24:5d58515ba510 1657 } else {
arsenalist 27:562a95c120cc 1658 //logging purpose
arsenalist 27:562a95c120cc 1659 seconds = time(NULL);
arsenalist 27:562a95c120cc 1660 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 27:562a95c120cc 1661 DBG.printf("logPEnv:%d\r\n",writeLog(q,"sendPanelEnv failed"));
arsenalist 24:5d58515ba510 1662 DBG.printf("send panel environment failed\r\n");
arsenalist 24:5d58515ba510 1663 }
arsenalist 21:33bd8b82560f 1664 }
arsenalist 24:5d58515ba510 1665 checkRxBuffer();
arsenalist 21:33bd8b82560f 1666 }
arsenalist 21:33bd8b82560f 1667 tPanel.reset();
arsenalist 21:33bd8b82560f 1668 }
arsenalist 21:33bd8b82560f 1669
arsenalist 0:f4e449fa34d7 1670 //nodeTemp
arsenalist 21:33bd8b82560f 1671 checkRxBuffer();
arsenalist 25:36c6a5db50ed 1672 if(tNodes.read() > 900.0f) { //900 is 15 minutes
arsenalist 5:d00233dd36f5 1673 DBG.printf("getNodesTemperature\r\n");
arsenalist 5:d00233dd36f5 1674
arsenalist 5:d00233dd36f5 1675 for(int i=0; i<NODES_MAX; i++) {
arsenalist 5:d00233dd36f5 1676 if(!nodes[i].ipAddr.empty()) {
arsenalist 5:d00233dd36f5 1677 //get node's temp
arsenalist 5:d00233dd36f5 1678 string temp;
arsenalist 5:d00233dd36f5 1679 nodes[i].restConn->get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
arsenalist 5:d00233dd36f5 1680 wait(2);
arsenalist 5:d00233dd36f5 1681 temp = rxBuf;
arsenalist 5:d00233dd36f5 1682 if(temp.rfind(nodes[i].ipAddr) != std::string::npos) {
arsenalist 5:d00233dd36f5 1683 temp.erase(temp.begin(),temp.begin()+temp.rfind(nodes[i].ipAddr));
arsenalist 5:d00233dd36f5 1684 if(temp.rfind("temp=") != std::string::npos) {
arsenalist 5:d00233dd36f5 1685 temp.erase(temp.begin(),temp.begin()+temp.rfind("temp=")+6);
arsenalist 5:d00233dd36f5 1686 temp.erase(temp.begin()+temp.find("\""),temp.end());
arsenalist 5:d00233dd36f5 1687 } else {
arsenalist 5:d00233dd36f5 1688 temp = "0"; //connect to node, but receive none
arsenalist 5:d00233dd36f5 1689 }
arsenalist 5:d00233dd36f5 1690 } else {
arsenalist 5:d00233dd36f5 1691 temp = "0"; //not connected to node
arsenalist 5:d00233dd36f5 1692 }
arsenalist 0:f4e449fa34d7 1693
arsenalist 5:d00233dd36f5 1694 DBG.printf("nodeTemp[%d]:%s\r\n",i,temp.c_str());
arsenalist 5:d00233dd36f5 1695
arsenalist 5:d00233dd36f5 1696 //send node's temp
arsenalist 25:36c6a5db50ed 1697 if(temp != "0") {
arsenalist 25:36c6a5db50ed 1698 seconds = time(NULL);
arsenalist 25:36c6a5db50ed 1699 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 22:e18d361bf688 1700
arsenalist 25:36c6a5db50ed 1701 //calculate hmacTime
arsenalist 25:36c6a5db50ed 1702 for(int j=0; j<sizeof(p); j++) {
arsenalist 25:36c6a5db50ed 1703 p[j]=0; }
arsenalist 25:36c6a5db50ed 1704 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 25:36c6a5db50ed 1705 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 1706
arsenalist 25:36c6a5db50ed 1707 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"mac\":\"%s\",\"value\":%s}",
arsenalist 25:36c6a5db50ed 1708 emmaUID.c_str(),hmacTime.c_str(),q,nodes[i].macAddr.c_str(),temp.c_str());
arsenalist 25:36c6a5db50ed 1709 //DBG.printf("dataNodeTemp:\r\n%s\r\n",s);
arsenalist 25:36c6a5db50ed 1710 rest.post("/emma/api/controller/nodetemp",s);
arsenalist 25:36c6a5db50ed 1711 wait(2);
arsenalist 25:36c6a5db50ed 1712 str = rxBuf;
arsenalist 25:36c6a5db50ed 1713 if(str.rfind("/nodetemp") != std::string::npos) {
arsenalist 25:36c6a5db50ed 1714 str.erase(str.begin(),str.begin()+str.rfind("/nodetemp"));
arsenalist 25:36c6a5db50ed 1715 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 25:36c6a5db50ed 1716 DBG.printf("send nodeTemp success\r\n");
arsenalist 25:36c6a5db50ed 1717 } else {
arsenalist 25:36c6a5db50ed 1718 DBG.printf("send nodeTemp failed\r\n");
arsenalist 25:36c6a5db50ed 1719 }
arsenalist 5:d00233dd36f5 1720 }
arsenalist 25:36c6a5db50ed 1721 checkRxBuffer();
arsenalist 5:d00233dd36f5 1722 }
arsenalist 5:d00233dd36f5 1723 }
arsenalist 0:f4e449fa34d7 1724 }
arsenalist 19:7e3e9332f719 1725 tNodes.reset();
arsenalist 19:7e3e9332f719 1726 }
arsenalist 21:33bd8b82560f 1727
arsenalist 21:33bd8b82560f 1728 //command
arsenalist 40:77bd44f57ad3 1729 sprintf(r,"/emma/api/controller/command");
arsenalist 40:77bd44f57ad3 1730 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 40:77bd44f57ad3 1731 emmaUID.c_str(),hmacTime.c_str());
arsenalist 40:77bd44f57ad3 1732 //rest.get("/emma/api/controller/command");
arsenalist 40:77bd44f57ad3 1733 rest.post(r,s);
arsenalist 40:77bd44f57ad3 1734
arsenalist 5:d00233dd36f5 1735 checkRxBuffer();
arsenalist 0:f4e449fa34d7 1736 if(newCommand) {
arsenalist 5:d00233dd36f5 1737 DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str());
arsenalist 13:e8adfe305dbc 1738 TFT.locate(0,160);
arsenalist 13:e8adfe305dbc 1739 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1740 TFT.locate(0,160);
arsenalist 13:e8adfe305dbc 1741 TFT.printf("newCommand");
arsenalist 13:e8adfe305dbc 1742
arsenalist 0:f4e449fa34d7 1743 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 1744 parse(jsonValue,globalCommand.c_str());
arsenalist 40:77bd44f57ad3 1745 char *parameter[5] = {"from","nType","nAddr","dType","cmd"};
arsenalist 0:f4e449fa34d7 1746
arsenalist 41:2d023bd80a9c 1747 DBG.printf("get %d command\r\n",jsonValue.size());
arsenalist 41:2d023bd80a9c 1748
arsenalist 41:2d023bd80a9c 1749 //processing each command
arsenalist 41:2d023bd80a9c 1750 bool validCommand;
arsenalist 41:2d023bd80a9c 1751 for(int i=0; i<jsonValue.size(); i++) {
arsenalist 41:2d023bd80a9c 1752 //check whether command is valid
arsenalist 41:2d023bd80a9c 1753 DBG.printf("\r\nprocessing cmd[%d]\r\n",i);
arsenalist 41:2d023bd80a9c 1754 validCommand = true;
arsenalist 41:2d023bd80a9c 1755 for(int j=0; j<5; j++) {
arsenalist 41:2d023bd80a9c 1756 validCommand = validCommand && jsonValue[i].hasMember(parameter[j]);
arsenalist 41:2d023bd80a9c 1757 }
arsenalist 41:2d023bd80a9c 1758 DBG.printf("command validity:%d\r\n",validCommand);
arsenalist 41:2d023bd80a9c 1759
arsenalist 41:2d023bd80a9c 1760 if(validCommand) {
arsenalist 41:2d023bd80a9c 1761 string commandFrom = jsonValue[i][parameter[0]].get<std::string>();
arsenalist 41:2d023bd80a9c 1762 string commandNType = jsonValue[i][parameter[1]].get<std::string>();
arsenalist 41:2d023bd80a9c 1763 string commandNAddr = jsonValue[i][parameter[2]].get<std::string>();
arsenalist 41:2d023bd80a9c 1764 string commandDType = jsonValue[i][parameter[3]].get<std::string>();
arsenalist 41:2d023bd80a9c 1765 string commandCmd = jsonValue[i][parameter[4]].get<std::string>();
arsenalist 41:2d023bd80a9c 1766
arsenalist 41:2d023bd80a9c 1767 if(commandNType == "0") { //switch on panel controller
arsenalist 41:2d023bd80a9c 1768 DBG.printf("command for switch\r\n");
arsenalist 41:2d023bd80a9c 1769 }
arsenalist 41:2d023bd80a9c 1770 else if(commandNType == "1") { //remote node
arsenalist 41:2d023bd80a9c 1771 DBG.printf("command for remote\r\n");
arsenalist 41:2d023bd80a9c 1772 //get node ip address based on node mac address
arsenalist 41:2d023bd80a9c 1773 //string nodeIP;
arsenalist 41:2d023bd80a9c 1774 //nodeIp = readNodeIP(commandNAddr);
arsenalist 41:2d023bd80a9c 1775 //nodeIP = "192.168.2.15";
arsenalist 41:2d023bd80a9c 1776 //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
arsenalist 41:2d023bd80a9c 1777
arsenalist 41:2d023bd80a9c 1778 //get index of node list based on mac address
arsenalist 41:2d023bd80a9c 1779 int idx = NODES_INVALID;
arsenalist 41:2d023bd80a9c 1780 for(int i=0; i<NODES_MAX; i++) {
arsenalist 41:2d023bd80a9c 1781 if(!nodes[i].macAddr.compare(commandNAddr)) {
arsenalist 41:2d023bd80a9c 1782 idx = i;
arsenalist 41:2d023bd80a9c 1783 }
arsenalist 41:2d023bd80a9c 1784 }
arsenalist 41:2d023bd80a9c 1785
arsenalist 41:2d023bd80a9c 1786 //execution process
arsenalist 41:2d023bd80a9c 1787 string execResult = "failed";
arsenalist 41:2d023bd80a9c 1788 if(idx != NODES_INVALID) {
arsenalist 41:2d023bd80a9c 1789 DBG.printf("index found at %d\r\n",idx);
arsenalist 41:2d023bd80a9c 1790
arsenalist 41:2d023bd80a9c 1791 //get cmd string based on device type and command number
arsenalist 41:2d023bd80a9c 1792 string nodeCmd;
arsenalist 41:2d023bd80a9c 1793 nodeCmd = readNodeCmd(commandDType,commandCmd);
arsenalist 41:2d023bd80a9c 1794
arsenalist 41:2d023bd80a9c 1795 //execute command
arsenalist 41:2d023bd80a9c 1796 DBG.printf("executing command\r\n");
arsenalist 41:2d023bd80a9c 1797 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
arsenalist 41:2d023bd80a9c 1798
arsenalist 41:2d023bd80a9c 1799 trial=0;
arsenalist 41:2d023bd80a9c 1800 while(1) {
arsenalist 41:2d023bd80a9c 1801 rxBuf.clear();
arsenalist 41:2d023bd80a9c 1802 if(trial>=2) { //two times trial
arsenalist 41:2d023bd80a9c 1803 DBG.printf("cmd is not executed\r\n");
arsenalist 41:2d023bd80a9c 1804 TFT.foreground(Red);
arsenalist 41:2d023bd80a9c 1805 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1806 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1807 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1808 TFT.printf("cmd is not executed");
arsenalist 41:2d023bd80a9c 1809 wait(1);
arsenalist 41:2d023bd80a9c 1810 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1811 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1812 TFT.foreground(White);
arsenalist 41:2d023bd80a9c 1813 break;
arsenalist 41:2d023bd80a9c 1814 }
arsenalist 41:2d023bd80a9c 1815 nodes[idx].restConn->get("/",s);
arsenalist 41:2d023bd80a9c 1816 wait(2);
arsenalist 41:2d023bd80a9c 1817 if(rxBuf.find("REST: status = 200") != std::string::npos) {
arsenalist 41:2d023bd80a9c 1818 DBG.printf("cmd is executed\r\n");
arsenalist 41:2d023bd80a9c 1819 TFT.foreground(Green);
arsenalist 41:2d023bd80a9c 1820 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1821 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1822 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1823 TFT.printf("cmd is executed");
arsenalist 41:2d023bd80a9c 1824 wait(1);
arsenalist 41:2d023bd80a9c 1825 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1826 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1827 TFT.foreground(White);
arsenalist 41:2d023bd80a9c 1828 execResult = "success";
arsenalist 41:2d023bd80a9c 1829 break;
arsenalist 41:2d023bd80a9c 1830 }
arsenalist 41:2d023bd80a9c 1831 trial++;
arsenalist 41:2d023bd80a9c 1832 }
arsenalist 41:2d023bd80a9c 1833 } else {
arsenalist 41:2d023bd80a9c 1834 TFT.foreground(Red);
arsenalist 41:2d023bd80a9c 1835 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1836 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1837 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1838 TFT.printf("node is invalid");
arsenalist 41:2d023bd80a9c 1839 wait(1);
arsenalist 41:2d023bd80a9c 1840 TFT.locate(0,180);
arsenalist 41:2d023bd80a9c 1841 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1842 TFT.foreground(White);
arsenalist 41:2d023bd80a9c 1843 }
arsenalist 41:2d023bd80a9c 1844
arsenalist 41:2d023bd80a9c 1845 wait(2);
arsenalist 41:2d023bd80a9c 1846 //send execution result
arsenalist 41:2d023bd80a9c 1847 //DBG.printf("send execution result\r\n");
arsenalist 41:2d023bd80a9c 1848
arsenalist 41:2d023bd80a9c 1849 //calculate hmacCmd
arsenalist 41:2d023bd80a9c 1850 //for(int j=0; j<sizeof(p); j++) {
arsenalist 41:2d023bd80a9c 1851 // p[j]=0; }
arsenalist 41:2d023bd80a9c 1852 sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());
arsenalist 41:2d023bd80a9c 1853 hmacCmd = calculateMD5(p);
arsenalist 41:2d023bd80a9c 1854
arsenalist 41:2d023bd80a9c 1855 sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 41:2d023bd80a9c 1856 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 1857
arsenalist 41:2d023bd80a9c 1858 trial=0;
arsenalist 41:2d023bd80a9c 1859 while(1) {
arsenalist 41:2d023bd80a9c 1860 if(trial>=2) { //two times trial
arsenalist 41:2d023bd80a9c 1861 DBG.printf("failed to send execution result\r\n");
arsenalist 41:2d023bd80a9c 1862 break;
arsenalist 41:2d023bd80a9c 1863 }
arsenalist 41:2d023bd80a9c 1864 rest.post("/emma/api/controller/result",s);
arsenalist 41:2d023bd80a9c 1865 wait(2);
arsenalist 41:2d023bd80a9c 1866 str = rxBuf;
arsenalist 41:2d023bd80a9c 1867 if(str.rfind("/result") != std::string::npos) {
arsenalist 41:2d023bd80a9c 1868 str.erase(str.begin(),str.begin()+str.rfind("/result"));
arsenalist 41:2d023bd80a9c 1869 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 41:2d023bd80a9c 1870 DBG.printf("success to send execution result\r\n");
arsenalist 41:2d023bd80a9c 1871 break;
arsenalist 41:2d023bd80a9c 1872 }
arsenalist 41:2d023bd80a9c 1873 }
arsenalist 41:2d023bd80a9c 1874 checkRxBuffer();
arsenalist 41:2d023bd80a9c 1875 trial++;
arsenalist 41:2d023bd80a9c 1876 }
arsenalist 41:2d023bd80a9c 1877 }
arsenalist 41:2d023bd80a9c 1878 }
arsenalist 41:2d023bd80a9c 1879 }
arsenalist 41:2d023bd80a9c 1880 //clear text on lcd
arsenalist 41:2d023bd80a9c 1881 TFT.locate(0,160);
arsenalist 41:2d023bd80a9c 1882 TFT.printf(" ");
arsenalist 41:2d023bd80a9c 1883
arsenalist 41:2d023bd80a9c 1884 newCommand = false;
arsenalist 41:2d023bd80a9c 1885
arsenalist 41:2d023bd80a9c 1886 /*
arsenalist 7:7e8c6ad3fd64 1887 //check whether command is valid
arsenalist 0:f4e449fa34d7 1888 bool validCommand = true;
arsenalist 15:136526c28afb 1889 for(int i=0; i<5; i++) {
arsenalist 0:f4e449fa34d7 1890 validCommand = validCommand && jsonValue.hasMember(parameter[i]);
arsenalist 0:f4e449fa34d7 1891 }
arsenalist 0:f4e449fa34d7 1892 DBG.printf("command validity:%d\r\n",validCommand);
arsenalist 0:f4e449fa34d7 1893
arsenalist 0:f4e449fa34d7 1894 if(validCommand) {
arsenalist 40:77bd44f57ad3 1895 string commandFrom = jsonValue[parameter[0]].get<std::string>();
arsenalist 15:136526c28afb 1896 string commandNType = jsonValue[parameter[1]].get<std::string>();
arsenalist 15:136526c28afb 1897 string commandNAddr = jsonValue[parameter[2]].get<std::string>();
arsenalist 15:136526c28afb 1898 string commandDType = jsonValue[parameter[3]].get<std::string>();
arsenalist 15:136526c28afb 1899 string commandCmd = jsonValue[parameter[4]].get<std::string>();
arsenalist 0:f4e449fa34d7 1900
arsenalist 0:f4e449fa34d7 1901 if(commandNType == "0") { //switch on panel controller
arsenalist 0:f4e449fa34d7 1902 DBG.printf("command for switch\r\n");
arsenalist 0:f4e449fa34d7 1903 }
arsenalist 0:f4e449fa34d7 1904 else if(commandNType == "1") { //node with mac address
arsenalist 0:f4e449fa34d7 1905 DBG.printf("command for node\r\n");
arsenalist 0:f4e449fa34d7 1906 //get node ip address based on node mac address
arsenalist 5:d00233dd36f5 1907 //string nodeIP;
arsenalist 1:d314a43ae6f7 1908 //nodeIp = readNodeIP(commandNAddr);
arsenalist 5:d00233dd36f5 1909 //nodeIP = "192.168.2.15";
arsenalist 0:f4e449fa34d7 1910 //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
arsenalist 5:d00233dd36f5 1911
arsenalist 5:d00233dd36f5 1912 //get index of node list based on mac address
arsenalist 5:d00233dd36f5 1913 int idx = NODES_INVALID;
arsenalist 5:d00233dd36f5 1914 for(int i=0; i<NODES_MAX; i++) {
arsenalist 5:d00233dd36f5 1915 if(!nodes[i].macAddr.compare(commandNAddr)) {
arsenalist 5:d00233dd36f5 1916 idx = i;
arsenalist 5:d00233dd36f5 1917 }
arsenalist 5:d00233dd36f5 1918 }
arsenalist 5:d00233dd36f5 1919
arsenalist 5:d00233dd36f5 1920 //execution process
arsenalist 21:33bd8b82560f 1921 //int trial;
arsenalist 15:136526c28afb 1922 string execResult = "failed";
arsenalist 5:d00233dd36f5 1923 if(idx != NODES_INVALID) {
arsenalist 5:d00233dd36f5 1924 DBG.printf("index found at %d\r\n",idx);
arsenalist 0:f4e449fa34d7 1925
arsenalist 5:d00233dd36f5 1926 //get cmd string based on device type and command number
arsenalist 5:d00233dd36f5 1927 string nodeCmd;
arsenalist 14:8287f0f5d987 1928 nodeCmd = readNodeCmd(commandDType,commandCmd);
arsenalist 14:8287f0f5d987 1929 //nodeCmd = "020129A0163B161315131613153C151316131514143C153C16141414141415151315141414141514141415141414143D1514143D141415141414143D14000D"; //turn off
arsenalist 5:d00233dd36f5 1930 //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
arsenalist 0:f4e449fa34d7 1931
arsenalist 5:d00233dd36f5 1932 //execute command
arsenalist 5:d00233dd36f5 1933 DBG.printf("executing command\r\n");
arsenalist 0:f4e449fa34d7 1934 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
arsenalist 0:f4e449fa34d7 1935
arsenalist 5:d00233dd36f5 1936 trial=0;
arsenalist 0:f4e449fa34d7 1937 while(1) {
arsenalist 35:b8ba2b73201b 1938 rxBuf.clear();
arsenalist 5:d00233dd36f5 1939 //cmdExecuted = false;
arsenalist 5:d00233dd36f5 1940 if(trial>=2) { //two times trial
arsenalist 0:f4e449fa34d7 1941 DBG.printf("cmd is not executed\r\n");
arsenalist 13:e8adfe305dbc 1942 TFT.foreground(Red);
arsenalist 13:e8adfe305dbc 1943 TFT.locate(0,180);
arsenalist 13:e8adfe305dbc 1944 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1945 TFT.locate(0,180);
arsenalist 13:e8adfe305dbc 1946 TFT.printf("cmd is not executed");
arsenalist 14:8287f0f5d987 1947 wait(1);
arsenalist 13:e8adfe305dbc 1948 TFT.locate(0,180);
arsenalist 13:e8adfe305dbc 1949 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1950 TFT.foreground(White);
arsenalist 0:f4e449fa34d7 1951 break;
arsenalist 0:f4e449fa34d7 1952 }
arsenalist 5:d00233dd36f5 1953 nodes[idx].restConn->get("/",s);
arsenalist 5:d00233dd36f5 1954 wait(2);
arsenalist 35:b8ba2b73201b 1955 if(rxBuf.find("REST: status = 200") != std::string::npos) {
arsenalist 13:e8adfe305dbc 1956 DBG.printf("cmd is executed\r\n");
arsenalist 13:e8adfe305dbc 1957 TFT.foreground(Green);
arsenalist 13:e8adfe305dbc 1958 TFT.locate(0,180);
arsenalist 13:e8adfe305dbc 1959 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1960 TFT.locate(0,180);
arsenalist 13:e8adfe305dbc 1961 TFT.printf("cmd is executed");
arsenalist 14:8287f0f5d987 1962 wait(1);
arsenalist 13:e8adfe305dbc 1963 TFT.locate(0,180);
arsenalist 13:e8adfe305dbc 1964 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 1965 TFT.foreground(White);
arsenalist 15:136526c28afb 1966 execResult = "success";
arsenalist 5:d00233dd36f5 1967 break;
arsenalist 5:d00233dd36f5 1968 }
arsenalist 0:f4e449fa34d7 1969 trial++;
arsenalist 5:d00233dd36f5 1970 }
arsenalist 14:8287f0f5d987 1971 } else {
arsenalist 14:8287f0f5d987 1972 TFT.foreground(Red);
arsenalist 14:8287f0f5d987 1973 TFT.locate(0,180);
arsenalist 14:8287f0f5d987 1974 TFT.printf(" ");
arsenalist 14:8287f0f5d987 1975 TFT.locate(0,180);
arsenalist 14:8287f0f5d987 1976 TFT.printf("node is invalid");
arsenalist 14:8287f0f5d987 1977 wait(1);
arsenalist 14:8287f0f5d987 1978 TFT.locate(0,180);
arsenalist 14:8287f0f5d987 1979 TFT.printf(" ");
arsenalist 14:8287f0f5d987 1980 TFT.foreground(White);
arsenalist 5:d00233dd36f5 1981 }
arsenalist 5:d00233dd36f5 1982
arsenalist 5:d00233dd36f5 1983 wait(2);
arsenalist 5:d00233dd36f5 1984 //send execution result
arsenalist 21:33bd8b82560f 1985 //DBG.printf("send execution result\r\n");
arsenalist 23:fb369e171b7d 1986
arsenalist 23:fb369e171b7d 1987 //calculate hmacCmd
arsenalist 23:fb369e171b7d 1988 for(int j=0; j<sizeof(p); j++) {
arsenalist 23:fb369e171b7d 1989 p[j]=0; }
arsenalist 23:fb369e171b7d 1990 sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str());
arsenalist 23:fb369e171b7d 1991 hmacCmd = calculateMD5(p);
arsenalist 23:fb369e171b7d 1992
arsenalist 40:77bd44f57ad3 1993 sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}",
arsenalist 40:77bd44f57ad3 1994 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 5:d00233dd36f5 1995
arsenalist 5:d00233dd36f5 1996 trial=0;
arsenalist 5:d00233dd36f5 1997 while(1) {
arsenalist 5:d00233dd36f5 1998 if(trial>=2) { //two times trial
arsenalist 23:fb369e171b7d 1999 DBG.printf("failed to send execution result\r\n");
arsenalist 5:d00233dd36f5 2000 break;
arsenalist 0:f4e449fa34d7 2001 }
arsenalist 23:fb369e171b7d 2002 rest.post("/emma/api/controller/result",s);
arsenalist 5:d00233dd36f5 2003 wait(2);
arsenalist 23:fb369e171b7d 2004 str = rxBuf;
arsenalist 23:fb369e171b7d 2005 if(str.rfind("/result") != std::string::npos) {
arsenalist 23:fb369e171b7d 2006 str.erase(str.begin(),str.begin()+str.rfind("/result"));
arsenalist 23:fb369e171b7d 2007 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 23:fb369e171b7d 2008 DBG.printf("success to send execution result\r\n");
arsenalist 23:fb369e171b7d 2009 break;
arsenalist 23:fb369e171b7d 2010 }
arsenalist 0:f4e449fa34d7 2011 }
arsenalist 23:fb369e171b7d 2012 checkRxBuffer();
arsenalist 5:d00233dd36f5 2013 trial++;
arsenalist 0:f4e449fa34d7 2014 }
arsenalist 0:f4e449fa34d7 2015 }
arsenalist 0:f4e449fa34d7 2016 }
arsenalist 13:e8adfe305dbc 2017
arsenalist 13:e8adfe305dbc 2018 //clear text on lcd
arsenalist 13:e8adfe305dbc 2019 TFT.locate(0,160);
arsenalist 13:e8adfe305dbc 2020 TFT.printf(" ");
arsenalist 13:e8adfe305dbc 2021
arsenalist 0:f4e449fa34d7 2022 newCommand = false;
arsenalist 41:2d023bd80a9c 2023 */
arsenalist 2:fdfdeb5fd6fd 2024 }
arsenalist 30:08d290158fa7 2025
arsenalist 4:76ab12e2f8a3 2026 osDelay(5000);
arsenalist 0:f4e449fa34d7 2027 }
arsenalist 0:f4e449fa34d7 2028 }
arsenalist 0:f4e449fa34d7 2029 }
arsenalist 0:f4e449fa34d7 2030 void emmaModeFirmwareDownload(void) {
arsenalist 0:f4e449fa34d7 2031 bool emmaGetFirmwareParam = false;
arsenalist 0:f4e449fa34d7 2032
arsenalist 0:f4e449fa34d7 2033 DBG.printf("emmaModeFirmwareDownload\r\n");
arsenalist 0:f4e449fa34d7 2034
arsenalist 0:f4e449fa34d7 2035 char s[384];
arsenalist 0:f4e449fa34d7 2036 string str;
arsenalist 0:f4e449fa34d7 2037 string connData;
arsenalist 0:f4e449fa34d7 2038 string chunk;
arsenalist 0:f4e449fa34d7 2039
arsenalist 0:f4e449fa34d7 2040 //firmware parameter
arsenalist 0:f4e449fa34d7 2041 string firmwareVer;
arsenalist 0:f4e449fa34d7 2042 string firmwareName;
arsenalist 0:f4e449fa34d7 2043 int numPart;
arsenalist 0:f4e449fa34d7 2044
arsenalist 0:f4e449fa34d7 2045 //downloading
arsenalist 0:f4e449fa34d7 2046 string firmwarePart;
arsenalist 0:f4e449fa34d7 2047 string calcMD5;
arsenalist 0:f4e449fa34d7 2048 string srvrMD5;
arsenalist 0:f4e449fa34d7 2049 bool nextPart;
arsenalist 0:f4e449fa34d7 2050
arsenalist 0:f4e449fa34d7 2051 //set wifi to mode bridge
arsenalist 0:f4e449fa34d7 2052 _ESP.printf("MODE=B");
arsenalist 0:f4e449fa34d7 2053 DBG.printf("set mode bridge\r\n");
arsenalist 0:f4e449fa34d7 2054 while(1) {
arsenalist 0:f4e449fa34d7 2055 char rcv[128] = {};
arsenalist 0:f4e449fa34d7 2056 rcvReply(rcv,3000);
arsenalist 0:f4e449fa34d7 2057 str = rcv;
arsenalist 0:f4e449fa34d7 2058 if(str.find("MODE=B_OK") != std::string::npos)
arsenalist 0:f4e449fa34d7 2059 break;
arsenalist 0:f4e449fa34d7 2060 }
arsenalist 0:f4e449fa34d7 2061 DBG.printf("MODE=B\r\n");
arsenalist 0:f4e449fa34d7 2062
arsenalist 0:f4e449fa34d7 2063 esp.enable();
arsenalist 0:f4e449fa34d7 2064 wait(1);
arsenalist 0:f4e449fa34d7 2065 while(!esp.ready());
arsenalist 0:f4e449fa34d7 2066
arsenalist 0:f4e449fa34d7 2067 if(!rest.begin("candra.tritronik.com",3128,false)) {
arsenalist 0:f4e449fa34d7 2068 DBG.printf("EMMA: fail to setup rest");
arsenalist 0:f4e449fa34d7 2069 while(1);
arsenalist 0:f4e449fa34d7 2070 }
arsenalist 0:f4e449fa34d7 2071
arsenalist 0:f4e449fa34d7 2072 //wifiConnected = true; //with custom firmware, panel should connect wifi automatically
arsenalist 0:f4e449fa34d7 2073 useProxy = true;
arsenalist 0:f4e449fa34d7 2074
arsenalist 0:f4e449fa34d7 2075 esp.process();
arsenalist 0:f4e449fa34d7 2076 //set connData
arsenalist 0:f4e449fa34d7 2077 if(useProxy) {
arsenalist 0:f4e449fa34d7 2078 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2079 s[i]=0; }
arsenalist 38:e44f35f8129d 2080 //sprintf(s,"http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s",restSERVER,restPORT,emmaUID.c_str(),hmac.c_str());
arsenalist 0:f4e449fa34d7 2081 sprintf(s,"http://192.168.128.69/emmaController/firmware/firmwareParameter");
arsenalist 0:f4e449fa34d7 2082 connData = s;
arsenalist 0:f4e449fa34d7 2083 } else {
arsenalist 0:f4e449fa34d7 2084 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2085 s[i]=0; }
arsenalist 0:f4e449fa34d7 2086 //sprintf(s,"/emma/api/controller/register?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
arsenalist 0:f4e449fa34d7 2087 sprintf(s,"/emmaController/firmware/firmwareParameter");
arsenalist 0:f4e449fa34d7 2088 connData = s;
arsenalist 0:f4e449fa34d7 2089 }
arsenalist 0:f4e449fa34d7 2090
arsenalist 0:f4e449fa34d7 2091 //get parameter of firmware to be downloaded
arsenalist 0:f4e449fa34d7 2092 while(!emmaGetFirmwareParam) {
arsenalist 0:f4e449fa34d7 2093 rest.get(connData.c_str());
arsenalist 0:f4e449fa34d7 2094 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2095 s[i]=0; }
arsenalist 0:f4e449fa34d7 2096 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 2097 //DBG.printf("rsp param:%s\r\n",s);
arsenalist 0:f4e449fa34d7 2098
arsenalist 0:f4e449fa34d7 2099 str = s;
arsenalist 0:f4e449fa34d7 2100 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2101 str.erase(str.begin(),str.begin()+str.find("[")+1);
arsenalist 0:f4e449fa34d7 2102 str.erase(str.begin()+str.find("]"),str.end());
arsenalist 0:f4e449fa34d7 2103
arsenalist 0:f4e449fa34d7 2104 MbedJSONValue jsonValue;
arsenalist 0:f4e449fa34d7 2105 parse(jsonValue,str.c_str());
arsenalist 0:f4e449fa34d7 2106
arsenalist 0:f4e449fa34d7 2107 char *parameter[2] = {"firmwareVer","numPart"};
arsenalist 0:f4e449fa34d7 2108
arsenalist 0:f4e449fa34d7 2109 for(int i=0; i<2; i++) {
arsenalist 0:f4e449fa34d7 2110 if(jsonValue.hasMember(parameter[i])) {
arsenalist 0:f4e449fa34d7 2111 string val = jsonValue[parameter[i]].get<std::string>();
arsenalist 0:f4e449fa34d7 2112 if(i==0) {
arsenalist 0:f4e449fa34d7 2113 firmwareVer = val;
arsenalist 0:f4e449fa34d7 2114 } else if(i==1) {
arsenalist 0:f4e449fa34d7 2115 sscanf(val.c_str(),"%d",&numPart);
arsenalist 0:f4e449fa34d7 2116 }
arsenalist 0:f4e449fa34d7 2117 }
arsenalist 0:f4e449fa34d7 2118 }
arsenalist 0:f4e449fa34d7 2119
arsenalist 0:f4e449fa34d7 2120 if(!firmwareVer.empty() && numPart!=0) {
arsenalist 0:f4e449fa34d7 2121 emmaGetFirmwareParam = true;
arsenalist 0:f4e449fa34d7 2122 }
arsenalist 0:f4e449fa34d7 2123 }
arsenalist 0:f4e449fa34d7 2124 }
arsenalist 0:f4e449fa34d7 2125 DBG.printf("firmwareVer:%s\r\n",firmwareVer.c_str());
arsenalist 0:f4e449fa34d7 2126 DBG.printf("numPart:%d\r\n",numPart);
arsenalist 0:f4e449fa34d7 2127
arsenalist 0:f4e449fa34d7 2128 //clear firmware file
arsenalist 0:f4e449fa34d7 2129 while(1) {
arsenalist 0:f4e449fa34d7 2130 if(clearFirmware()){
arsenalist 0:f4e449fa34d7 2131 DBG.printf("clear firmware on sd card\r\n\r\n");
arsenalist 0:f4e449fa34d7 2132 break;
arsenalist 0:f4e449fa34d7 2133 }
arsenalist 0:f4e449fa34d7 2134 wait(1);
arsenalist 0:f4e449fa34d7 2135 }
arsenalist 0:f4e449fa34d7 2136
arsenalist 0:f4e449fa34d7 2137 //set connData
arsenalist 0:f4e449fa34d7 2138 if(useProxy) {
arsenalist 0:f4e449fa34d7 2139 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2140 s[i]=0; }
arsenalist 0:f4e449fa34d7 2141 sprintf(s,"http://192.168.128.69/emmaController/firmware/%s",firmwareVer.c_str());
arsenalist 0:f4e449fa34d7 2142 connData = s;
arsenalist 0:f4e449fa34d7 2143 } else {
arsenalist 0:f4e449fa34d7 2144 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2145 s[i]=0; }
arsenalist 0:f4e449fa34d7 2146 sprintf(s,"/emmaController/firmware/%s",firmwareVer.c_str());
arsenalist 0:f4e449fa34d7 2147 connData = s;
arsenalist 0:f4e449fa34d7 2148 }
arsenalist 0:f4e449fa34d7 2149
arsenalist 0:f4e449fa34d7 2150 //download firmware
arsenalist 0:f4e449fa34d7 2151 for(int n=0; n<numPart; n++) {
arsenalist 0:f4e449fa34d7 2152 nextPart = false;
arsenalist 0:f4e449fa34d7 2153 while(!nextPart) {
arsenalist 0:f4e449fa34d7 2154 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2155 s[i]=0; }
arsenalist 0:f4e449fa34d7 2156 sprintf(s,"%s/firmware_Hex_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
arsenalist 0:f4e449fa34d7 2157 rest.get(s);
arsenalist 0:f4e449fa34d7 2158 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2159 s[i]=0; }
arsenalist 0:f4e449fa34d7 2160 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 2161 //DBG.printf("rsp[%d]:%s\r\n",n,s);
arsenalist 0:f4e449fa34d7 2162
arsenalist 0:f4e449fa34d7 2163 str = s;
arsenalist 0:f4e449fa34d7 2164 if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2165 str.erase(str.begin(),str.begin()+str.find("{")+1);
arsenalist 0:f4e449fa34d7 2166 str.erase(str.begin()+str.find("}"),str.end());
arsenalist 0:f4e449fa34d7 2167 //DBG.printf("firmwarePart[%d]:%s\r\n",n,str.c_str());
arsenalist 0:f4e449fa34d7 2168 firmwarePart = str;
arsenalist 0:f4e449fa34d7 2169
arsenalist 0:f4e449fa34d7 2170 //calculated MD5
arsenalist 0:f4e449fa34d7 2171 calcMD5 = calculateMD5(firmwarePart);
arsenalist 0:f4e449fa34d7 2172 //DBG.printf("calcMD5[%d]:%s\r\n",n,calcMD5.c_str());
arsenalist 0:f4e449fa34d7 2173
arsenalist 0:f4e449fa34d7 2174 //MD5 from server
arsenalist 0:f4e449fa34d7 2175 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2176 s[i]=0; }
arsenalist 0:f4e449fa34d7 2177 sprintf(s,"%s/MD5/firmware_MD5_%s_%d",connData.c_str(),firmwareVer.c_str(),n);
arsenalist 0:f4e449fa34d7 2178 rest.get(s);
arsenalist 0:f4e449fa34d7 2179 for(int i=0; i<sizeof(s); i++) {
arsenalist 0:f4e449fa34d7 2180 s[i]=0; }
arsenalist 0:f4e449fa34d7 2181 rest.getResponse(s,sizeof(s));
arsenalist 0:f4e449fa34d7 2182
arsenalist 0:f4e449fa34d7 2183 str = s;
arsenalist 0:f4e449fa34d7 2184 if(str.find("{") != std::string::npos && str.find("}") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2185 str.erase(str.begin(),str.begin()+str.find("{")+1);
arsenalist 0:f4e449fa34d7 2186 str.erase(str.begin()+str.find("}"),str.end());
arsenalist 0:f4e449fa34d7 2187 srvrMD5 = str;
arsenalist 0:f4e449fa34d7 2188 //DBG.printf("srvrMD5[%d]:%s\r\n",n,srvrMD5.c_str());
arsenalist 0:f4e449fa34d7 2189
arsenalist 0:f4e449fa34d7 2190 //compare original MD5 vs MD5 from server
arsenalist 0:f4e449fa34d7 2191 if(strcmp(calcMD5.c_str(),srvrMD5.c_str()) == 0) {
arsenalist 0:f4e449fa34d7 2192 //DBG.printf("MD5 correct\r\n");
arsenalist 0:f4e449fa34d7 2193
arsenalist 0:f4e449fa34d7 2194 //save to sd card
arsenalist 0:f4e449fa34d7 2195 int st = writeFirmwareHexToChar(firmwarePart);
arsenalist 0:f4e449fa34d7 2196 if(st) {
arsenalist 0:f4e449fa34d7 2197 DBG.printf("firmwarePart[%d/%d] written\r\n",n,numPart-1);
arsenalist 0:f4e449fa34d7 2198 nextPart = true;
arsenalist 0:f4e449fa34d7 2199 }
arsenalist 0:f4e449fa34d7 2200
arsenalist 0:f4e449fa34d7 2201 } else {
arsenalist 0:f4e449fa34d7 2202 DBG.printf("MD5 incorrect\r\n");
arsenalist 0:f4e449fa34d7 2203 }
arsenalist 0:f4e449fa34d7 2204 }
arsenalist 0:f4e449fa34d7 2205 } else {
arsenalist 0:f4e449fa34d7 2206 DBG.printf("retry to fetch firmwarePart[%d]\r\n",n);
arsenalist 0:f4e449fa34d7 2207 }
arsenalist 0:f4e449fa34d7 2208 wait(0.5);
arsenalist 0:f4e449fa34d7 2209 }
arsenalist 0:f4e449fa34d7 2210 }
arsenalist 0:f4e449fa34d7 2211 DBG.printf("download finished\r\n");
arsenalist 0:f4e449fa34d7 2212 }
arsenalist 12:96f637ed37f9 2213 /*end emma mode*/
arsenalist 0:f4e449fa34d7 2214
arsenalist 4:76ab12e2f8a3 2215 /*start energy related*/
arsenalist 0:f4e449fa34d7 2216 void energyThread(void const*) {
arsenalist 19:7e3e9332f719 2217 Timer tEnergy;
arsenalist 19:7e3e9332f719 2218
arsenalist 19:7e3e9332f719 2219 while(1) {
arsenalist 19:7e3e9332f719 2220 tEnergy.start();
arsenalist 19:7e3e9332f719 2221 DBG.printf("energyThread-start\r\n");
arsenalist 19:7e3e9332f719 2222
arsenalist 19:7e3e9332f719 2223 AWattHrSum = 0;
arsenalist 19:7e3e9332f719 2224 BWattHrSum = 0;
arsenalist 19:7e3e9332f719 2225 CWattHrSum = 0;
arsenalist 19:7e3e9332f719 2226
arsenalist 20:ea14f175bbb4 2227 while(tEnergy.read() < 1*60.0) {
arsenalist 19:7e3e9332f719 2228 AWattHrValue = ADE.getWattHR(PHASE_A);
arsenalist 19:7e3e9332f719 2229 BWattHrValue = ADE.getWattHR(PHASE_B);
arsenalist 19:7e3e9332f719 2230 CWattHrValue = ADE.getWattHR(PHASE_C);
arsenalist 19:7e3e9332f719 2231
arsenalist 19:7e3e9332f719 2232 AWattHrSum += AWattHrValue;
arsenalist 19:7e3e9332f719 2233 BWattHrSum += BWattHrValue;
arsenalist 20:ea14f175bbb4 2234 CWattHrSum += CWattHrValue;
arsenalist 20:ea14f175bbb4 2235
arsenalist 20:ea14f175bbb4 2236
arsenalist 20:ea14f175bbb4 2237 //start check voltage and power
arsenalist 21:33bd8b82560f 2238 AVrms = ADE.VRMS(PHASE_A) * 0.000124f; //0.000158; //constants are from calculateVRMS function
arsenalist 21:33bd8b82560f 2239 BVrms = ADE.VRMS(PHASE_B) * 0.000123f;
arsenalist 21:33bd8b82560f 2240 CVrms = ADE.VRMS(PHASE_C) * 0.000122f;
arsenalist 20:ea14f175bbb4 2241
arsenalist 21:33bd8b82560f 2242 AIrms = ADE.IRMS(PHASE_A) * 0.00001006f; //0.0000125f; //constants are from calculateIRMS function
arsenalist 21:33bd8b82560f 2243 BIrms = ADE.IRMS(PHASE_B) * 0.00001005f;
arsenalist 21:33bd8b82560f 2244 CIrms = ADE.IRMS(PHASE_C) * 0.00001004f;
arsenalist 20:ea14f175bbb4 2245
arsenalist 20:ea14f175bbb4 2246 AWatt = AVrms * AIrms;
arsenalist 20:ea14f175bbb4 2247 BWatt = BVrms * BIrms;
arsenalist 20:ea14f175bbb4 2248 CWatt = CVrms * CIrms;
arsenalist 20:ea14f175bbb4 2249 //end check voltage and power
arsenalist 20:ea14f175bbb4 2250
arsenalist 19:7e3e9332f719 2251 }
arsenalist 19:7e3e9332f719 2252
arsenalist 25:36c6a5db50ed 2253 AWattHr = AWattHrSum * 0.000044169f; //0.0000198f;
arsenalist 25:36c6a5db50ed 2254 BWattHr = BWattHrSum * 0.000044168f; //0.0000197f;
arsenalist 25:36c6a5db50ed 2255 CWattHr = CWattHrSum * 0.000044167f; //0.0000196f;
arsenalist 19:7e3e9332f719 2256
arsenalist 22:e18d361bf688 2257 newEnergyData = true;
arsenalist 22:e18d361bf688 2258
arsenalist 19:7e3e9332f719 2259 tEnergy.stop();
arsenalist 19:7e3e9332f719 2260 tEnergy.reset();
arsenalist 19:7e3e9332f719 2261 DBG.printf("energyThread-finish\r\n");
arsenalist 19:7e3e9332f719 2262 }
arsenalist 0:f4e449fa34d7 2263 }
arsenalist 21:33bd8b82560f 2264 void checkVoltagePower(void) {
arsenalist 4:76ab12e2f8a3 2265 //check if voltage or power violates threshold
arsenalist 22:e18d361bf688 2266 char p[64];
arsenalist 20:ea14f175bbb4 2267 char q[32];
arsenalist 4:76ab12e2f8a3 2268 char s[256];
arsenalist 22:e18d361bf688 2269 string hmacTime;
arsenalist 4:76ab12e2f8a3 2270 string str;
arsenalist 20:ea14f175bbb4 2271 time_t seconds;
arsenalist 4:76ab12e2f8a3 2272
arsenalist 4:76ab12e2f8a3 2273 //DBG.printf("checkVoltagePower-start\r\n");
arsenalist 4:76ab12e2f8a3 2274
arsenalist 20:ea14f175bbb4 2275 //vrms and irms might be placed inside energy calculation routine
arsenalist 20:ea14f175bbb4 2276 /*
arsenalist 19:7e3e9332f719 2277 AVrms = ADE.VRMS(PHASE_A) * 0.000128f; //0.000158; //constants are from calculateVRMS function
arsenalist 19:7e3e9332f719 2278 BVrms = ADE.VRMS(PHASE_B) * 0.000127f; //0.000157;
arsenalist 19:7e3e9332f719 2279 CVrms = ADE.VRMS(PHASE_C) * 0.000126f; //0.000156;
arsenalist 19:7e3e9332f719 2280
arsenalist 19:7e3e9332f719 2281 AIrms = ADE.IRMS(PHASE_A) * 0.0000125f; //constants are from calculateIRMS function
arsenalist 19:7e3e9332f719 2282 BIrms = ADE.IRMS(PHASE_B) * 0.0000123f;
arsenalist 19:7e3e9332f719 2283 CIrms = ADE.IRMS(PHASE_C) * 0.0000124f;
arsenalist 4:76ab12e2f8a3 2284
arsenalist 4:76ab12e2f8a3 2285 AWatt = AVrms * AIrms;
arsenalist 4:76ab12e2f8a3 2286 BWatt = BVrms * BIrms;
arsenalist 4:76ab12e2f8a3 2287 CWatt = CVrms * CIrms;
arsenalist 20:ea14f175bbb4 2288 */
arsenalist 4:76ab12e2f8a3 2289
arsenalist 21:33bd8b82560f 2290 //DBG.printf("Vrms of each phase:%.2f - %.2f - %.2f\r\n", AVrms, BVrms, CVrms);
arsenalist 21:33bd8b82560f 2291 //DBG.printf("Watt of each phase:%.2f - %.2f - %.2f\r\n", AWatt, BWatt, CWatt);
arsenalist 21:33bd8b82560f 2292 //wait(1);
arsenalist 19:7e3e9332f719 2293
arsenalist 20:ea14f175bbb4 2294 //get time
arsenalist 20:ea14f175bbb4 2295 seconds = time(NULL);
arsenalist 20:ea14f175bbb4 2296 strftime(q, 32, "%Y-%m-%d %H:%M:%S",localtime(&seconds));
arsenalist 20:ea14f175bbb4 2297
arsenalist 22:e18d361bf688 2298 //calculate hmacTime
arsenalist 22:e18d361bf688 2299 for(int j=0; j<sizeof(p); j++) {
arsenalist 22:e18d361bf688 2300 p[j]=0; }
arsenalist 22:e18d361bf688 2301 sprintf(p,"emma-%s-%s",emmaUID.c_str(),q);
arsenalist 22:e18d361bf688 2302 hmacTime = calculateMD5(p);
arsenalist 22:e18d361bf688 2303
arsenalist 4:76ab12e2f8a3 2304 if(AVrms > VRMSTHRESHOLD || AWatt > WATTTHRESHOLD) {
arsenalist 4:76ab12e2f8a3 2305 DBG.printf("alert on ch1\r\n");
arsenalist 20:ea14f175bbb4 2306 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 22:e18d361bf688 2307 emmaUID.c_str(),hmacTime.c_str(),q,AVrms,AWatt);
arsenalist 4:76ab12e2f8a3 2308 rest.post("/emma/api/controller/alert/1",s);
arsenalist 4:76ab12e2f8a3 2309 wait(2);
arsenalist 4:76ab12e2f8a3 2310 str = rxBuf;
arsenalist 4:76ab12e2f8a3 2311 if(str.rfind("/alert/1") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2312 str.erase(str.begin(),str.begin()+str.rfind("/alert/1"));
arsenalist 4:76ab12e2f8a3 2313 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2314 DBG.printf("send alert ch1 success\r\n");
arsenalist 4:76ab12e2f8a3 2315 } else {
arsenalist 4:76ab12e2f8a3 2316 DBG.printf("send alert ch1 failed\r\n");
arsenalist 4:76ab12e2f8a3 2317 }
arsenalist 4:76ab12e2f8a3 2318 }
arsenalist 4:76ab12e2f8a3 2319 }
arsenalist 4:76ab12e2f8a3 2320
arsenalist 4:76ab12e2f8a3 2321 if(BVrms > VRMSTHRESHOLD || BWatt > WATTTHRESHOLD) {
arsenalist 4:76ab12e2f8a3 2322 DBG.printf("alert on ch2\r\n");
arsenalist 20:ea14f175bbb4 2323 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 22:e18d361bf688 2324 emmaUID.c_str(),hmacTime.c_str(),q,BVrms,BWatt);
arsenalist 4:76ab12e2f8a3 2325 rest.post("/emma/api/controller/alert/2",s);
arsenalist 4:76ab12e2f8a3 2326 wait(2);
arsenalist 4:76ab12e2f8a3 2327 str = rxBuf;
arsenalist 4:76ab12e2f8a3 2328 if(str.rfind("/alert/2") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2329 str.erase(str.begin(),str.begin()+str.rfind("/alert/2"));
arsenalist 4:76ab12e2f8a3 2330 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2331 DBG.printf("send alert ch2 success\r\n");
arsenalist 4:76ab12e2f8a3 2332 } else {
arsenalist 4:76ab12e2f8a3 2333 DBG.printf("send alert ch2 failed\r\n");
arsenalist 4:76ab12e2f8a3 2334 }
arsenalist 4:76ab12e2f8a3 2335 }
arsenalist 4:76ab12e2f8a3 2336 }
arsenalist 4:76ab12e2f8a3 2337
arsenalist 4:76ab12e2f8a3 2338 if(CVrms > VRMSTHRESHOLD || CWatt > WATTTHRESHOLD) {
arsenalist 7:7e8c6ad3fd64 2339 DBG.printf("alert on ch3\r\n");
arsenalist 4:76ab12e2f8a3 2340 DBG.printf("alert on ch3\r\n");
arsenalist 20:ea14f175bbb4 2341 sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"voltage\":%.2f,\"power\":%.2f}",
arsenalist 22:e18d361bf688 2342 emmaUID.c_str(),hmacTime.c_str(),q,CVrms,CWatt);
arsenalist 4:76ab12e2f8a3 2343 rest.post("/emma/api/controller/alert/3",s);
arsenalist 4:76ab12e2f8a3 2344 wait(2);
arsenalist 4:76ab12e2f8a3 2345 str = rxBuf;
arsenalist 4:76ab12e2f8a3 2346 if(str.rfind("/alert/3") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2347 str.erase(str.begin(),str.begin()+str.rfind("/alert/3"));
arsenalist 4:76ab12e2f8a3 2348 if(str.find("\"status\":\"success\"") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2349 DBG.printf("send alert ch3 success\r\n");
arsenalist 4:76ab12e2f8a3 2350 } else {
arsenalist 4:76ab12e2f8a3 2351 DBG.printf("send alert ch3 failed\r\n");
arsenalist 4:76ab12e2f8a3 2352 }
arsenalist 4:76ab12e2f8a3 2353 }
arsenalist 4:76ab12e2f8a3 2354 }
arsenalist 4:76ab12e2f8a3 2355 //DBG.printf("checkVoltagePower-finish\r\n");
arsenalist 4:76ab12e2f8a3 2356 }
arsenalist 4:76ab12e2f8a3 2357 /*end energy related*/
arsenalist 0:f4e449fa34d7 2358
arsenalist 0:f4e449fa34d7 2359 /*start wifi mqtt*/
arsenalist 0:f4e449fa34d7 2360 void mqttConnected(void* response) {
arsenalist 1:d314a43ae6f7 2361 DBG.printf("MQTT Connected\r\n");
arsenalist 0:f4e449fa34d7 2362 char mqttTopic[64];
arsenalist 38:e44f35f8129d 2363 sprintf(mqttTopic,"%s/%s/command",mqttDOMAIN.c_str(),emmaUID.c_str());
arsenalist 27:562a95c120cc 2364 //mqtt.subscribe(mqttTopic);
arsenalist 0:f4e449fa34d7 2365 }
arsenalist 0:f4e449fa34d7 2366 void mqttDisconnected(void* response) {
arsenalist 1:d314a43ae6f7 2367 DBG.printf("MQTT Disconnected\r\n");
arsenalist 0:f4e449fa34d7 2368 }
arsenalist 0:f4e449fa34d7 2369 /*end wifi mqtt*/
arsenalist 0:f4e449fa34d7 2370
arsenalist 0:f4e449fa34d7 2371 /*start wifi rest*/
arsenalist 27:562a95c120cc 2372 void wifiCb(void* response) {
arsenalist 27:562a95c120cc 2373 uint32_t status;
arsenalist 27:562a95c120cc 2374 RESPONSE res(response);
arsenalist 27:562a95c120cc 2375
arsenalist 27:562a95c120cc 2376 if(res.getArgc() == 1) {
arsenalist 27:562a95c120cc 2377 res.popArgs((uint8_t*)&status,4);
arsenalist 27:562a95c120cc 2378 if(status == STATION_GOT_IP) {
arsenalist 27:562a95c120cc 2379 DBG.printf("WIFI Connected\r\n");
arsenalist 27:562a95c120cc 2380 //wifiConnected = true;
arsenalist 27:562a95c120cc 2381 }
arsenalist 27:562a95c120cc 2382 else {
arsenalist 27:562a95c120cc 2383 //wifiConnected = false;
arsenalist 27:562a95c120cc 2384 }
arsenalist 27:562a95c120cc 2385 }
arsenalist 27:562a95c120cc 2386 }
arsenalist 27:562a95c120cc 2387
arsenalist 4:76ab12e2f8a3 2388 void rxInterrupt(void) {
arsenalist 4:76ab12e2f8a3 2389 char c;
arsenalist 4:76ab12e2f8a3 2390
arsenalist 4:76ab12e2f8a3 2391 while(_ESP.readable()) {
arsenalist 4:76ab12e2f8a3 2392 c = _ESP.getc();
arsenalist 4:76ab12e2f8a3 2393 if(c != 0) { //char is not null
arsenalist 4:76ab12e2f8a3 2394 rxBuf += c;
arsenalist 4:76ab12e2f8a3 2395 }
arsenalist 4:76ab12e2f8a3 2396 }
arsenalist 4:76ab12e2f8a3 2397 }
arsenalist 4:76ab12e2f8a3 2398 void checkRxBuffer(void) {
arsenalist 4:76ab12e2f8a3 2399 //check new command
arsenalist 4:76ab12e2f8a3 2400 if(rxBuf.rfind("/command") != std::string::npos) {
arsenalist 4:76ab12e2f8a3 2401 rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.rfind("/command"));
arsenalist 41:2d023bd80a9c 2402 if(rxBuf.find("[{") != std::string::npos && rxBuf.rfind("}]") != std::string::npos) {
arsenalist 41:2d023bd80a9c 2403 rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.find("[{"));
arsenalist 41:2d023bd80a9c 2404 rxBuf.erase(rxBuf.begin()+rxBuf.rfind("}]")+2,rxBuf.end());
arsenalist 41:2d023bd80a9c 2405
arsenalist 41:2d023bd80a9c 2406 //start special handler
arsenalist 41:2d023bd80a9c 2407 while(1){
arsenalist 41:2d023bd80a9c 2408 if(rxBuf.find("}],") != std::string::npos) {
arsenalist 41:2d023bd80a9c 2409 rxBuf.erase(rxBuf.begin()+rxBuf.find("}],")+1,rxBuf.begin()+rxBuf.find("}],")+2);
arsenalist 41:2d023bd80a9c 2410 } else {
arsenalist 41:2d023bd80a9c 2411 break;
arsenalist 41:2d023bd80a9c 2412 }
arsenalist 41:2d023bd80a9c 2413 }
arsenalist 41:2d023bd80a9c 2414 //end special handler
arsenalist 41:2d023bd80a9c 2415
arsenalist 4:76ab12e2f8a3 2416 globalCommand = rxBuf;
arsenalist 4:76ab12e2f8a3 2417 newCommand = true;
arsenalist 4:76ab12e2f8a3 2418 }
arsenalist 4:76ab12e2f8a3 2419 }
arsenalist 4:76ab12e2f8a3 2420
arsenalist 24:5d58515ba510 2421 //check free memory -> reinitialize mqtt connection
arsenalist 27:562a95c120cc 2422 if(rxBuf.rfind("Free memory") != std::string::npos) {
arsenalist 27:562a95c120cc 2423 rxLog = "Free memory-" + rxBuf;
arsenalist 24:5d58515ba510 2424 espFreeMemory = true;
arsenalist 24:5d58515ba510 2425 }
arsenalist 24:5d58515ba510 2426
arsenalist 27:562a95c120cc 2427 //check dhcp client start -> initialize all connection
arsenalist 27:562a95c120cc 2428 if(rxBuf.rfind("dhcp client start") != std::string::npos) {
arsenalist 27:562a95c120cc 2429 rxLogA = "dhcp client start-" + rxBuf;
arsenalist 27:562a95c120cc 2430 espDHCPClientStart = true;
arsenalist 27:562a95c120cc 2431 }
arsenalist 27:562a95c120cc 2432
arsenalist 4:76ab12e2f8a3 2433 //clear rxBuf
arsenalist 4:76ab12e2f8a3 2434 rxBuf.clear();
arsenalist 4:76ab12e2f8a3 2435 }
arsenalist 0:f4e449fa34d7 2436 /*end wifi rest*/
arsenalist 0:f4e449fa34d7 2437
arsenalist 36:ed696fd1b44e 2438 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
arsenalist 36:ed696fd1b44e 2439 {
arsenalist 36:ed696fd1b44e 2440 MQTT::Message message;
arsenalist 38:e44f35f8129d 2441 char* topic = new char[strlen(mqttDOMAIN.c_str())+strlen(emmaUID.c_str())+9];
arsenalist 36:ed696fd1b44e 2442 *topic = '\0';
arsenalist 38:e44f35f8129d 2443 strcat(topic, mqttDOMAIN.c_str());
arsenalist 36:ed696fd1b44e 2444 strcat(topic, "/");
arsenalist 36:ed696fd1b44e 2445 strcat(topic, emmaUID.c_str());
arsenalist 36:ed696fd1b44e 2446 strcat(topic, "/dummy");
arsenalist 36:ed696fd1b44e 2447
arsenalist 36:ed696fd1b44e 2448 const char buf[25] = "{\"d\":{\"myName\":\"EMMA\"}}";
arsenalist 36:ed696fd1b44e 2449
arsenalist 36:ed696fd1b44e 2450 message.qos = MQTT::QOS2;
arsenalist 36:ed696fd1b44e 2451 message.retained = false;
arsenalist 36:ed696fd1b44e 2452 message.dup = false;
arsenalist 36:ed696fd1b44e 2453 message.payload = (void*)buf;
arsenalist 36:ed696fd1b44e 2454 message.payloadlen = strlen(buf);
arsenalist 36:ed696fd1b44e 2455
arsenalist 36:ed696fd1b44e 2456 DBG.printf("Publishing %s\r\n", buf);
arsenalist 36:ed696fd1b44e 2457 return client->publish(topic, &message);
arsenalist 36:ed696fd1b44e 2458 }
arsenalist 36:ed696fd1b44e 2459
arsenalist 0:f4e449fa34d7 2460 /*start eth mqtt*/
arsenalist 0:f4e449fa34d7 2461 void ethMQTTMessageArrived(MQTT::MessageData& md) {
arsenalist 0:f4e449fa34d7 2462 MQTT::Message &message = md.message;
arsenalist 36:ed696fd1b44e 2463 char topic[md.topicName.lenstring.len + 1];
arsenalist 36:ed696fd1b44e 2464 string str;
arsenalist 36:ed696fd1b44e 2465 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
arsenalist 36:ed696fd1b44e 2466
arsenalist 36:ed696fd1b44e 2467 DBG.printf("Message arrived on topic %s: %.*s\r\n", topic, message.payloadlen, message.payload);
arsenalist 36:ed696fd1b44e 2468 char *s = (char*)message.payload;
arsenalist 36:ed696fd1b44e 2469 string sp(s);
arsenalist 36:ed696fd1b44e 2470 if(sp.find("[") != std::string::npos && sp.find("]") != std::string::npos) {
arsenalist 36:ed696fd1b44e 2471 sp.erase(sp.begin(),sp.begin()+sp.find("[")+1);
arsenalist 36:ed696fd1b44e 2472 sp.erase(sp.begin()+sp.find("]"),sp.end());
arsenalist 36:ed696fd1b44e 2473 }
arsenalist 36:ed696fd1b44e 2474 else {
arsenalist 36:ed696fd1b44e 2475 DBG.printf("Invalid MQTT command");
arsenalist 36:ed696fd1b44e 2476 return;
arsenalist 36:ed696fd1b44e 2477 }
arsenalist 36:ed696fd1b44e 2478
arsenalist 36:ed696fd1b44e 2479 MbedJSONValue jsonValue;
arsenalist 36:ed696fd1b44e 2480 parse(jsonValue, sp.c_str());
arsenalist 36:ed696fd1b44e 2481 const char *parameter[5] = {"name","nType","nAddr","dType","cmd"};
arsenalist 36:ed696fd1b44e 2482
arsenalist 36:ed696fd1b44e 2483 //check if command is valid
arsenalist 36:ed696fd1b44e 2484 bool validCommand = true;
arsenalist 36:ed696fd1b44e 2485 for(int i=0; i<5; i++) {
arsenalist 36:ed696fd1b44e 2486 validCommand = validCommand && jsonValue.hasMember(parameter[i]);
arsenalist 36:ed696fd1b44e 2487 }
arsenalist 36:ed696fd1b44e 2488 DBG.printf("command validity:%d\r\n",validCommand);
arsenalist 36:ed696fd1b44e 2489
arsenalist 36:ed696fd1b44e 2490 //check for new command
arsenalist 36:ed696fd1b44e 2491 if(validCommand) {
arsenalist 36:ed696fd1b44e 2492 string commandId = jsonValue[parameter[0]].get<std::string>();
arsenalist 36:ed696fd1b44e 2493 string commandNType = jsonValue[parameter[1]].get<std::string>();
arsenalist 36:ed696fd1b44e 2494 string commandNAddr = jsonValue[parameter[2]].get<std::string>();
arsenalist 36:ed696fd1b44e 2495 string commandDType = jsonValue[parameter[3]].get<std::string>();
arsenalist 36:ed696fd1b44e 2496 string commandCmd = jsonValue[parameter[4]].get<std::string>();
arsenalist 36:ed696fd1b44e 2497
arsenalist 36:ed696fd1b44e 2498 if(commandNType == "0") { //switch on panel controller
arsenalist 36:ed696fd1b44e 2499 DBG.printf("command for switch\r\n");
arsenalist 36:ed696fd1b44e 2500 }
arsenalist 36:ed696fd1b44e 2501 else if(commandNType == "1") { //node with mac address
arsenalist 36:ed696fd1b44e 2502 DBG.printf("command for node\r\n");
arsenalist 36:ed696fd1b44e 2503 //get node ip address based on node mac address
arsenalist 36:ed696fd1b44e 2504 string nodeIP = readNodeIP(commandNAddr);
arsenalist 36:ed696fd1b44e 2505 //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
arsenalist 36:ed696fd1b44e 2506
arsenalist 36:ed696fd1b44e 2507 //get cmd string based on device type and command number
arsenalist 36:ed696fd1b44e 2508 string nodeCmd = readNodeCmd(commandDType,commandCmd);
arsenalist 36:ed696fd1b44e 2509 //DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
arsenalist 36:ed696fd1b44e 2510 //execute command
arsenalist 36:ed696fd1b44e 2511 int trial=0;
arsenalist 36:ed696fd1b44e 2512 trial = 0;
arsenalist 36:ed696fd1b44e 2513 bool execStatus=false;
arsenalist 36:ed696fd1b44e 2514
arsenalist 36:ed696fd1b44e 2515 while( !execStatus ) {
arsenalist 36:ed696fd1b44e 2516 sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
arsenalist 36:ed696fd1b44e 2517 str.assign(s);
arsenalist 36:ed696fd1b44e 2518 DBG.printf("str value:%s\r\n",str.c_str());
arsenalist 40:77bd44f57ad3 2519 string rcv = ethREST(nodeIP,REMOTE_TCP_PORT,str); // cause mqtt to stop
arsenalist 36:ed696fd1b44e 2520 DBG.printf("response:%s\r\n",rcv.c_str());
arsenalist 36:ed696fd1b44e 2521 str = rcv;
arsenalist 36:ed696fd1b44e 2522 if(str.find("OK") != std::string::npos) {
arsenalist 36:ed696fd1b44e 2523 DBG.printf("CMD executed successfully\r\n");
arsenalist 36:ed696fd1b44e 2524 execStatus = true;
arsenalist 36:ed696fd1b44e 2525 break;
arsenalist 36:ed696fd1b44e 2526 }
arsenalist 36:ed696fd1b44e 2527 if(trial>5) { //two times trial
arsenalist 36:ed696fd1b44e 2528 DBG.printf("cmd is not executed\r\n");
arsenalist 36:ed696fd1b44e 2529 execStatus = false;
arsenalist 36:ed696fd1b44e 2530 }
arsenalist 36:ed696fd1b44e 2531 DBG.printf("Trial++\r\n");
arsenalist 36:ed696fd1b44e 2532 trial++;
arsenalist 36:ed696fd1b44e 2533 wait(3);
arsenalist 36:ed696fd1b44e 2534 }
arsenalist 36:ed696fd1b44e 2535 DBG.printf("Send execution status\r\n");
arsenalist 36:ed696fd1b44e 2536 //send execution status
arsenalist 36:ed696fd1b44e 2537 }
arsenalist 36:ed696fd1b44e 2538 }
arsenalist 36:ed696fd1b44e 2539 DBG.printf("Finish processing new command\r\n");
arsenalist 36:ed696fd1b44e 2540
arsenalist 36:ed696fd1b44e 2541 /*
arsenalist 36:ed696fd1b44e 2542 MQTT::Message &message = md.message;
arsenalist 0:f4e449fa34d7 2543 DBG.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n",
arsenalist 0:f4e449fa34d7 2544 message.qos, message.retained, message.dup, message.id);
arsenalist 0:f4e449fa34d7 2545 DBG.printf("Payload: %.*s\r\n", message.payloadlen, (char*)message.payload);
arsenalist 0:f4e449fa34d7 2546 //DBG.printf("Payload %s\r\n",message.payload);
arsenalist 0:f4e449fa34d7 2547
arsenalist 0:f4e449fa34d7 2548
arsenalist 0:f4e449fa34d7 2549 //check whether cmd is json
arsenalist 0:f4e449fa34d7 2550 //char r[255];
arsenalist 0:f4e449fa34d7 2551 char *s = (char*)message.payload;
arsenalist 0:f4e449fa34d7 2552 string sp(s);
arsenalist 0:f4e449fa34d7 2553 //sprintf(s,"%.*s",message.payloadlen, (char*)message.payload);
arsenalist 0:f4e449fa34d7 2554 //sprintf(globCmd,"%s",s);
arsenalist 0:f4e449fa34d7 2555 //DBG.printf("Payload: %s\r\n", r);
arsenalist 0:f4e449fa34d7 2556 //string cmd = r;
arsenalist 0:f4e449fa34d7 2557 //globalCommand = cmd;
arsenalist 10:1dc69e0ce7fd 2558
arsenalist 10:1dc69e0ce7fd 2559 //globalCommand = sp;
arsenalist 10:1dc69e0ce7fd 2560
arsenalist 0:f4e449fa34d7 2561 //globCmd = (char*)message.payload;
arsenalist 0:f4e449fa34d7 2562 //string globCmd((char*)message.payload);
arsenalist 0:f4e449fa34d7 2563 //std::string *sp = static_cast<std::char*>(message.payload);
arsenalist 0:f4e449fa34d7 2564 //string sp((char*)message.payload);
arsenalist 10:1dc69e0ce7fd 2565
arsenalist 10:1dc69e0ce7fd 2566 //DBG.printf("String: %s\r\n",sp.c_str());
arsenalist 10:1dc69e0ce7fd 2567
arsenalist 0:f4e449fa34d7 2568 //globCmd = sp;
arsenalist 0:f4e449fa34d7 2569
arsenalist 10:1dc69e0ce7fd 2570 //newCommand = true;
arsenalist 10:1dc69e0ce7fd 2571 if(sp.find("[") != std::string::npos && sp.find("]") != std::string::npos) {
arsenalist 10:1dc69e0ce7fd 2572 sp.erase(sp.begin(),sp.begin()+sp.find("[")+1);
arsenalist 10:1dc69e0ce7fd 2573 sp.erase(sp.begin()+sp.find("]"),sp.end());
arsenalist 10:1dc69e0ce7fd 2574 globalCommand = sp;
arsenalist 0:f4e449fa34d7 2575 newCommand = true;
arsenalist 0:f4e449fa34d7 2576 }
arsenalist 10:1dc69e0ce7fd 2577 sp.clear();
arsenalist 36:ed696fd1b44e 2578 */
arsenalist 0:f4e449fa34d7 2579 }
arsenalist 0:f4e449fa34d7 2580
arsenalist 0:f4e449fa34d7 2581 int ethMQTTConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {
arsenalist 38:e44f35f8129d 2582 char *pMqttServer = &mqttSERVER[0u];
arsenalist 38:e44f35f8129d 2583 int connPort;
arsenalist 38:e44f35f8129d 2584 sscanf(mqttPORT.c_str(),"%d",&connPort);
arsenalist 38:e44f35f8129d 2585 int rc = ipstack->connect(pMqttServer, connPort);
arsenalist 0:f4e449fa34d7 2586
arsenalist 0:f4e449fa34d7 2587 if(rc!=0)
arsenalist 0:f4e449fa34d7 2588 return rc;
arsenalist 0:f4e449fa34d7 2589
arsenalist 0:f4e449fa34d7 2590 //MQTT Connect
arsenalist 36:ed696fd1b44e 2591 char clientId [64];
arsenalist 0:f4e449fa34d7 2592 sprintf(clientId,"emma/%s",emmaUID.c_str());
arsenalist 0:f4e449fa34d7 2593 //DBG.printf("clientId:%s\r\n",clientId);
arsenalist 36:ed696fd1b44e 2594
arsenalist 0:f4e449fa34d7 2595 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
arsenalist 0:f4e449fa34d7 2596 data.MQTTVersion = 3;
arsenalist 0:f4e449fa34d7 2597 data.clientID.cstring = clientId;
arsenalist 0:f4e449fa34d7 2598 data.username.cstring = "761b233e-a49a-4830-a8ae-87cec3dc1086";
arsenalist 0:f4e449fa34d7 2599 data.password.cstring = "ef25cf4567fbc07113252f8d72b7faf2";
arsenalist 0:f4e449fa34d7 2600
arsenalist 0:f4e449fa34d7 2601 if((rc = client->connect(&data)) == 0) {
arsenalist 36:ed696fd1b44e 2602 DBG.printf("ethMQTT connected\r\n");
arsenalist 0:f4e449fa34d7 2603 }
arsenalist 0:f4e449fa34d7 2604
arsenalist 0:f4e449fa34d7 2605 //MQTT Subscribe
arsenalist 36:ed696fd1b44e 2606 //char s[64];
arsenalist 38:e44f35f8129d 2607 //sprintf(s,"%s/%s/command",mqttDOMAIN.c_str(),emmaUID.c_str());
arsenalist 36:ed696fd1b44e 2608 //string topic = s;
arsenalist 38:e44f35f8129d 2609 char* topic = new char[strlen(mqttDOMAIN.c_str())+strlen(emmaUID.c_str())+9];
arsenalist 36:ed696fd1b44e 2610 *topic = '\0';
arsenalist 38:e44f35f8129d 2611 strcat(topic, mqttDOMAIN.c_str());
arsenalist 36:ed696fd1b44e 2612 strcat(topic, "/");
arsenalist 36:ed696fd1b44e 2613 strcat(topic, emmaUID.c_str());
arsenalist 36:ed696fd1b44e 2614 strcat(topic, "/command");
arsenalist 36:ed696fd1b44e 2615 DBG.printf("topic:%s\r\n",topic);
arsenalist 36:ed696fd1b44e 2616
arsenalist 36:ed696fd1b44e 2617 if((rc=client->subscribe(topic,MQTT::QOS0,ethMQTTMessageArrived)) == 0) {
arsenalist 0:f4e449fa34d7 2618 DBG.printf("subscribe success!\r\n");
arsenalist 0:f4e449fa34d7 2619 }
arsenalist 0:f4e449fa34d7 2620 return rc;
arsenalist 0:f4e449fa34d7 2621 }
arsenalist 0:f4e449fa34d7 2622 void ethMQTTAttemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {
arsenalist 0:f4e449fa34d7 2623 int retryAttempt = 0;
arsenalist 0:f4e449fa34d7 2624
arsenalist 0:f4e449fa34d7 2625 while(!ipstack->getEth().linkstatus()) {
arsenalist 0:f4e449fa34d7 2626 DBG.printf("Ethernet link not present. Check cable connection\r\n");
arsenalist 0:f4e449fa34d7 2627 wait(1);
arsenalist 0:f4e449fa34d7 2628 }
arsenalist 0:f4e449fa34d7 2629
arsenalist 0:f4e449fa34d7 2630 while(ethMQTTConnect(client,ipstack) != 0) {
arsenalist 0:f4e449fa34d7 2631 int timeout = 3;
arsenalist 0:f4e449fa34d7 2632 DBG.printf("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout);
arsenalist 0:f4e449fa34d7 2633 wait(timeout);
arsenalist 0:f4e449fa34d7 2634 retryAttempt++;
arsenalist 0:f4e449fa34d7 2635 }
arsenalist 0:f4e449fa34d7 2636 }
arsenalist 0:f4e449fa34d7 2637 /*end eth mqtt*/
arsenalist 0:f4e449fa34d7 2638
arsenalist 0:f4e449fa34d7 2639 /*start emma settings*/
arsenalist 0:f4e449fa34d7 2640 string getUID(void) {
arsenalist 0:f4e449fa34d7 2641 char s[32];
arsenalist 0:f4e449fa34d7 2642 unsigned long *unique = (unsigned long *)BASE_ADDR;
arsenalist 0:f4e449fa34d7 2643 sprintf(s,"%08x%08x%08x",unique[0], unique[1], unique[2]);
arsenalist 0:f4e449fa34d7 2644 return s;
arsenalist 0:f4e449fa34d7 2645 }
arsenalist 0:f4e449fa34d7 2646
arsenalist 0:f4e449fa34d7 2647 string readSetting(string parameter) {
arsenalist 0:f4e449fa34d7 2648 FILE *fp;
arsenalist 0:f4e449fa34d7 2649 signed char c;
arsenalist 0:f4e449fa34d7 2650 int i=0;
arsenalist 0:f4e449fa34d7 2651 char s[64];
arsenalist 0:f4e449fa34d7 2652 string strS;
arsenalist 0:f4e449fa34d7 2653
arsenalist 0:f4e449fa34d7 2654 sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
arsenalist 0:f4e449fa34d7 2655
arsenalist 0:f4e449fa34d7 2656 fp = fopen(s,"r");
arsenalist 0:f4e449fa34d7 2657 memset(s,0,sizeof(s));
arsenalist 0:f4e449fa34d7 2658 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 2659 while(1) {
arsenalist 0:f4e449fa34d7 2660 c = fgetc(fp);
arsenalist 0:f4e449fa34d7 2661 if(c == EOF){
arsenalist 0:f4e449fa34d7 2662 break;
arsenalist 0:f4e449fa34d7 2663 }
arsenalist 0:f4e449fa34d7 2664 s[i] = c;
arsenalist 0:f4e449fa34d7 2665 i++;
arsenalist 0:f4e449fa34d7 2666 }
arsenalist 0:f4e449fa34d7 2667 strS = s;
arsenalist 0:f4e449fa34d7 2668 if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2669 strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
arsenalist 0:f4e449fa34d7 2670 strS.erase(strS.begin()+strS.find(")"),strS.end());
arsenalist 0:f4e449fa34d7 2671 } else {
arsenalist 0:f4e449fa34d7 2672 strS = "";
arsenalist 0:f4e449fa34d7 2673 }
arsenalist 0:f4e449fa34d7 2674 }
arsenalist 0:f4e449fa34d7 2675 fclose(fp);
arsenalist 30:08d290158fa7 2676 free(fp);
arsenalist 0:f4e449fa34d7 2677 return strS;
arsenalist 0:f4e449fa34d7 2678 }
arsenalist 0:f4e449fa34d7 2679
arsenalist 0:f4e449fa34d7 2680 bool writeSetting(string parameter, string value) {
arsenalist 0:f4e449fa34d7 2681 FILE *fp;
arsenalist 0:f4e449fa34d7 2682 char s[255];
arsenalist 0:f4e449fa34d7 2683
arsenalist 0:f4e449fa34d7 2684 sprintf(s,"/sd/settings/%s.txt",parameter.c_str());
arsenalist 0:f4e449fa34d7 2685 fp = fopen(s,"w");
arsenalist 0:f4e449fa34d7 2686 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 2687 fprintf(fp,value.c_str());
arsenalist 0:f4e449fa34d7 2688 fclose(fp);
arsenalist 30:08d290158fa7 2689 free(fp);
arsenalist 0:f4e449fa34d7 2690 return true;
arsenalist 0:f4e449fa34d7 2691 }
arsenalist 30:08d290158fa7 2692 fclose(fp);
arsenalist 30:08d290158fa7 2693 free(fp);
arsenalist 0:f4e449fa34d7 2694 return false;
arsenalist 0:f4e449fa34d7 2695 }
arsenalist 0:f4e449fa34d7 2696 /*end emma settings*/
arsenalist 0:f4e449fa34d7 2697
arsenalist 0:f4e449fa34d7 2698 /*start emma node*/
arsenalist 0:f4e449fa34d7 2699 string readNodeIP(string macAddr) {
arsenalist 0:f4e449fa34d7 2700 FILE *fp;
arsenalist 0:f4e449fa34d7 2701 signed char c;
arsenalist 0:f4e449fa34d7 2702 int i=0;
arsenalist 0:f4e449fa34d7 2703 char s[64];
arsenalist 0:f4e449fa34d7 2704 string strS;
arsenalist 0:f4e449fa34d7 2705
arsenalist 0:f4e449fa34d7 2706 sprintf(s,"/sd/nodeList/%s/nodeIP.txt",macAddr.c_str());
arsenalist 0:f4e449fa34d7 2707
arsenalist 0:f4e449fa34d7 2708 fp = fopen(s,"r");
arsenalist 0:f4e449fa34d7 2709 memset(s,0,sizeof(s));
arsenalist 0:f4e449fa34d7 2710 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 2711 while(1) {
arsenalist 0:f4e449fa34d7 2712 c = fgetc(fp);
arsenalist 0:f4e449fa34d7 2713 if(c == EOF){
arsenalist 0:f4e449fa34d7 2714 break;
arsenalist 0:f4e449fa34d7 2715 }
arsenalist 0:f4e449fa34d7 2716 s[i] = c;
arsenalist 0:f4e449fa34d7 2717 i++;
arsenalist 0:f4e449fa34d7 2718 }
arsenalist 0:f4e449fa34d7 2719 strS = s;
arsenalist 0:f4e449fa34d7 2720 if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2721 strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
arsenalist 0:f4e449fa34d7 2722 strS.erase(strS.begin()+strS.find(")"),strS.end());
arsenalist 0:f4e449fa34d7 2723 } else {
arsenalist 0:f4e449fa34d7 2724 strS = "";
arsenalist 0:f4e449fa34d7 2725 }
arsenalist 0:f4e449fa34d7 2726 }
arsenalist 0:f4e449fa34d7 2727 fclose(fp);
arsenalist 30:08d290158fa7 2728 free(fp);
arsenalist 0:f4e449fa34d7 2729 return strS;
arsenalist 0:f4e449fa34d7 2730 }
arsenalist 0:f4e449fa34d7 2731
arsenalist 0:f4e449fa34d7 2732 string readNodeCmd(string dType, string cmd) {
arsenalist 0:f4e449fa34d7 2733 FILE *fp;
arsenalist 0:f4e449fa34d7 2734 signed char c;
arsenalist 0:f4e449fa34d7 2735 int i=0;
arsenalist 34:8555d471b880 2736 char s[2048];
arsenalist 0:f4e449fa34d7 2737 string strS;
arsenalist 0:f4e449fa34d7 2738
arsenalist 0:f4e449fa34d7 2739 sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmd.c_str());
arsenalist 0:f4e449fa34d7 2740
arsenalist 0:f4e449fa34d7 2741 fp = fopen(s,"r");
arsenalist 0:f4e449fa34d7 2742 memset(s,0,sizeof(s));
arsenalist 0:f4e449fa34d7 2743 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 2744 while(1) {
arsenalist 0:f4e449fa34d7 2745 c = fgetc(fp);
arsenalist 0:f4e449fa34d7 2746 if(c == EOF){
arsenalist 0:f4e449fa34d7 2747 break;
arsenalist 0:f4e449fa34d7 2748 }
arsenalist 0:f4e449fa34d7 2749 s[i] = c;
arsenalist 0:f4e449fa34d7 2750 i++;
arsenalist 0:f4e449fa34d7 2751 }
arsenalist 0:f4e449fa34d7 2752 strS = s;
arsenalist 0:f4e449fa34d7 2753 if(strS.find("(") != std::string::npos && strS.find(")") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2754 strS.erase(strS.begin(),strS.begin()+strS.find("(")+1);
arsenalist 0:f4e449fa34d7 2755 strS.erase(strS.begin()+strS.find(")"),strS.end());
arsenalist 0:f4e449fa34d7 2756 } else {
arsenalist 0:f4e449fa34d7 2757 strS = "";
arsenalist 0:f4e449fa34d7 2758 }
arsenalist 0:f4e449fa34d7 2759 }
arsenalist 0:f4e449fa34d7 2760 fclose(fp);
arsenalist 30:08d290158fa7 2761 free(fp);
arsenalist 0:f4e449fa34d7 2762 return strS;
arsenalist 0:f4e449fa34d7 2763 }
arsenalist 0:f4e449fa34d7 2764
arsenalist 0:f4e449fa34d7 2765 string *readNodeList(void) {
arsenalist 0:f4e449fa34d7 2766 static string nd[10]; //max node
arsenalist 0:f4e449fa34d7 2767 DIR *d;
arsenalist 0:f4e449fa34d7 2768 struct dirent *p;
arsenalist 0:f4e449fa34d7 2769 string q;
arsenalist 0:f4e449fa34d7 2770 int i=0;
arsenalist 0:f4e449fa34d7 2771
arsenalist 0:f4e449fa34d7 2772 d = opendir("/sd/nodeList");
arsenalist 0:f4e449fa34d7 2773 if(d != NULL) {
arsenalist 0:f4e449fa34d7 2774 while((p = readdir(d)) != NULL) {
arsenalist 0:f4e449fa34d7 2775 q = p->d_name;
arsenalist 0:f4e449fa34d7 2776 nd[i] = q;
arsenalist 0:f4e449fa34d7 2777 i++;
arsenalist 0:f4e449fa34d7 2778 }
arsenalist 0:f4e449fa34d7 2779 }
arsenalist 0:f4e449fa34d7 2780 closedir(d);
arsenalist 30:08d290158fa7 2781 free(d);
arsenalist 0:f4e449fa34d7 2782 return nd;
arsenalist 0:f4e449fa34d7 2783 }
arsenalist 0:f4e449fa34d7 2784
arsenalist 0:f4e449fa34d7 2785 string wifiGetNodeTemp(string macAddr) {
arsenalist 0:f4e449fa34d7 2786 int trial=0;
arsenalist 0:f4e449fa34d7 2787 string nodeIP = readNodeIP(macAddr);
arsenalist 0:f4e449fa34d7 2788 string str;
arsenalist 0:f4e449fa34d7 2789 string temp = "0";
arsenalist 0:f4e449fa34d7 2790
arsenalist 0:f4e449fa34d7 2791 if(rest.begin(nodeIP.c_str(),REMOTE_TCP_PORT,false)) {
arsenalist 0:f4e449fa34d7 2792 while(1) {
arsenalist 0:f4e449fa34d7 2793 char rcv[256] = {};
arsenalist 0:f4e449fa34d7 2794 rest.get("/","<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"2\"/>\r\n");
arsenalist 0:f4e449fa34d7 2795 rest.getResponse(rcv,sizeof(rcv));
arsenalist 0:f4e449fa34d7 2796 str = rcv;
arsenalist 0:f4e449fa34d7 2797 if(str.find("temp=") != std::string::npos) {
arsenalist 0:f4e449fa34d7 2798 str.erase(str.begin(),str.begin()+str.find("temp=")+6);
arsenalist 0:f4e449fa34d7 2799 str.erase(str.begin()+str.find("\""),str.end());
arsenalist 0:f4e449fa34d7 2800 temp = str;
arsenalist 0:f4e449fa34d7 2801 break;
arsenalist 0:f4e449fa34d7 2802 }
arsenalist 0:f4e449fa34d7 2803 if(trial>1) { //three times trial
arsenalist 0:f4e449fa34d7 2804 break;
arsenalist 0:f4e449fa34d7 2805 }
arsenalist 0:f4e449fa34d7 2806 trial++;
arsenalist 0:f4e449fa34d7 2807 wait(2);
arsenalist 0:f4e449fa34d7 2808 }
arsenalist 0:f4e449fa34d7 2809 }
arsenalist 0:f4e449fa34d7 2810 return temp;
arsenalist 0:f4e449fa34d7 2811 }
arsenalist 0:f4e449fa34d7 2812 /*end emma node*/
arsenalist 0:f4e449fa34d7 2813
arsenalist 0:f4e449fa34d7 2814 /*start emma connection function*/
arsenalist 40:77bd44f57ad3 2815 string ethREST(string host, int port, string data) {
arsenalist 0:f4e449fa34d7 2816 char buf[1024];
arsenalist 0:f4e449fa34d7 2817 char s[256];
arsenalist 0:f4e449fa34d7 2818 int ret;
arsenalist 0:f4e449fa34d7 2819 TCPSocketConnection sock;
arsenalist 0:f4e449fa34d7 2820 Timer t;
arsenalist 0:f4e449fa34d7 2821
arsenalist 40:77bd44f57ad3 2822 sprintf(s,"%s",data.c_str());
arsenalist 0:f4e449fa34d7 2823 sock.connect(host.c_str(),port);
arsenalist 0:f4e449fa34d7 2824 sock.send_all(s,sizeof(s)-1);
arsenalist 0:f4e449fa34d7 2825 wait(2);
arsenalist 0:f4e449fa34d7 2826
arsenalist 0:f4e449fa34d7 2827 //receive return
arsenalist 0:f4e449fa34d7 2828 t.start();
arsenalist 0:f4e449fa34d7 2829 while(1) {
arsenalist 0:f4e449fa34d7 2830 ret = sock.receive(buf, sizeof(buf));
arsenalist 0:f4e449fa34d7 2831 if(ret<=0 || t.read_ms() > 10000) {
arsenalist 0:f4e449fa34d7 2832 t.stop();
arsenalist 0:f4e449fa34d7 2833 break;
arsenalist 0:f4e449fa34d7 2834 }
arsenalist 0:f4e449fa34d7 2835 }
arsenalist 0:f4e449fa34d7 2836 sock.close();
arsenalist 0:f4e449fa34d7 2837 return buf;
arsenalist 0:f4e449fa34d7 2838 }
arsenalist 0:f4e449fa34d7 2839 /*end emma connection function*/
arsenalist 0:f4e449fa34d7 2840
arsenalist 0:f4e449fa34d7 2841 /*start emma private function*/
arsenalist 8:51a0ca9079ca 2842 void isEthAvailable(void) {
arsenalist 8:51a0ca9079ca 2843 if(ipstack.getEth().linkstatus()) {
arsenalist 8:51a0ca9079ca 2844 ethAvailable = true;
arsenalist 8:51a0ca9079ca 2845 } else {
arsenalist 8:51a0ca9079ca 2846 ethAvailable = false;
arsenalist 8:51a0ca9079ca 2847 }
arsenalist 8:51a0ca9079ca 2848 }
arsenalist 8:51a0ca9079ca 2849
arsenalist 8:51a0ca9079ca 2850 void isEthConnected(void) {
arsenalist 8:51a0ca9079ca 2851 char s[512];
arsenalist 8:51a0ca9079ca 2852 int connPort;
arsenalist 8:51a0ca9079ca 2853 string connHost;
arsenalist 8:51a0ca9079ca 2854 string str;
arsenalist 8:51a0ca9079ca 2855 Timer t;
arsenalist 8:51a0ca9079ca 2856
arsenalist 8:51a0ca9079ca 2857 if(ethAvailable) {
arsenalist 8:51a0ca9079ca 2858 if(useProxy) {
arsenalist 8:51a0ca9079ca 2859 connHost = proxySERVER;
arsenalist 8:51a0ca9079ca 2860 sscanf(proxyPORT.c_str(),"%d",&connPort);
arsenalist 8:51a0ca9079ca 2861 for(int i=0; i<sizeof(s); i++) {
arsenalist 8:51a0ca9079ca 2862 s[i]=0; }
arsenalist 38:e44f35f8129d 2863 sprintf(s,"GET http://%s:%s/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str());
arsenalist 8:51a0ca9079ca 2864 } else {
arsenalist 38:e44f35f8129d 2865 connHost = restSERVER;
arsenalist 38:e44f35f8129d 2866 //connPort = restPORT;
arsenalist 38:e44f35f8129d 2867 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 8:51a0ca9079ca 2868 for(int i=0; i<sizeof(s); i++) {
arsenalist 8:51a0ca9079ca 2869 s[i]=0; }
arsenalist 8:51a0ca9079ca 2870 strcpy(s,"GET /emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n");
arsenalist 8:51a0ca9079ca 2871 }
arsenalist 8:51a0ca9079ca 2872
arsenalist 8:51a0ca9079ca 2873 t.start();
arsenalist 8:51a0ca9079ca 2874 while(1) {
arsenalist 40:77bd44f57ad3 2875 str = ethREST(connHost,connPort,s);
arsenalist 8:51a0ca9079ca 2876 if(str.find("OK") != std::string::npos) {
arsenalist 8:51a0ca9079ca 2877 t.stop();
arsenalist 8:51a0ca9079ca 2878 ethConnected = true;
arsenalist 8:51a0ca9079ca 2879 break;
arsenalist 8:51a0ca9079ca 2880 }
arsenalist 8:51a0ca9079ca 2881 if(t.read_ms() > 5000) {
arsenalist 8:51a0ca9079ca 2882 t.stop();
arsenalist 8:51a0ca9079ca 2883 ethConnected = false;
arsenalist 8:51a0ca9079ca 2884 break;
arsenalist 8:51a0ca9079ca 2885 }
arsenalist 8:51a0ca9079ca 2886 }
arsenalist 8:51a0ca9079ca 2887 } else {
arsenalist 8:51a0ca9079ca 2888 ethConnected = false;
arsenalist 8:51a0ca9079ca 2889 }
arsenalist 8:51a0ca9079ca 2890 }
arsenalist 8:51a0ca9079ca 2891
arsenalist 8:51a0ca9079ca 2892 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 2893 char s[512];
arsenalist 8:51a0ca9079ca 2894 int connPort;
arsenalist 8:51a0ca9079ca 2895 string connHost;
arsenalist 8:51a0ca9079ca 2896 string str;
arsenalist 8:51a0ca9079ca 2897 Timer t;
arsenalist 8:51a0ca9079ca 2898
arsenalist 29:4d94a03d38e0 2899 /*
arsenalist 27:562a95c120cc 2900 esp.enable();
arsenalist 27:562a95c120cc 2901 wait(1);
arsenalist 27:562a95c120cc 2902 esp.reset();
arsenalist 27:562a95c120cc 2903 wait(1);
arsenalist 27:562a95c120cc 2904 while(!esp.ready());
arsenalist 27:562a95c120cc 2905
arsenalist 27:562a95c120cc 2906 //rest begin
arsenalist 38:e44f35f8129d 2907 if(!rest.begin(restSERVER,restPORT,false)) {
arsenalist 27:562a95c120cc 2908 DBG.printf("EMMA: fail to setup rest\r\n");
arsenalist 27:562a95c120cc 2909 TFT.locate(0,20);
arsenalist 27:562a95c120cc 2910 TFT.printf("EMMA: fail to setup rest");
arsenalist 27:562a95c120cc 2911 while(1);
arsenalist 27:562a95c120cc 2912 }
arsenalist 27:562a95c120cc 2913
arsenalist 27:562a95c120cc 2914 //setup wifi
arsenalist 27:562a95c120cc 2915 DBG.printf("EMMA: setup wifi\r\n");
arsenalist 27:562a95c120cc 2916 TFT.locate(0,20);
arsenalist 27:562a95c120cc 2917 TFT.printf("EMMA: setup wifi");
arsenalist 27:562a95c120cc 2918 wait(1);
arsenalist 27:562a95c120cc 2919 esp.wifiCb.attach(&wifiCb);
arsenalist 28:7561035e3df5 2920 //esp.wifiConnect("Tritronik Mobile","Tri12@11");
arsenalist 28:7561035e3df5 2921 esp.wifiConnect(wifiSSID.c_str(),wifiPASS.c_str());
arsenalist 27:562a95c120cc 2922 DBG.printf("EMMA: system started\r\n");
arsenalist 27:562a95c120cc 2923 TFT.locate(0,20);
arsenalist 27:562a95c120cc 2924 TFT.printf("EMMA: system started");
arsenalist 27:562a95c120cc 2925 t.start();
arsenalist 28:7561035e3df5 2926 while(t.read() < 20);
arsenalist 27:562a95c120cc 2927 t.stop();
arsenalist 27:562a95c120cc 2928 t.reset();
arsenalist 28:7561035e3df5 2929 TFT.locate(0,20);
arsenalist 28:7561035e3df5 2930 TFT.printf(" ");
arsenalist 29:4d94a03d38e0 2931 */
arsenalist 27:562a95c120cc 2932
arsenalist 8:51a0ca9079ca 2933 if(wifiAvailable) {
arsenalist 8:51a0ca9079ca 2934 _ESP.printf("MODE=B");
arsenalist 8:51a0ca9079ca 2935 if(useProxy) {
arsenalist 8:51a0ca9079ca 2936 connHost = proxySERVER;
arsenalist 8:51a0ca9079ca 2937 sscanf(proxyPORT.c_str(),"%d",&connPort);
arsenalist 8:51a0ca9079ca 2938 for(int i=0; i<sizeof(s); i++) {
arsenalist 8:51a0ca9079ca 2939 s[i]=0; }
arsenalist 38:e44f35f8129d 2940 sprintf(s,"http://%s:%s/emma/api/controller/test HTTP/1.0\nHost: %s\r\n\r\n",restSERVER.c_str(),restPORT.c_str(),restSERVER.c_str());
arsenalist 8:51a0ca9079ca 2941 } else {
arsenalist 38:e44f35f8129d 2942 connHost = restSERVER;
arsenalist 38:e44f35f8129d 2943 //connPort = restPORT;
arsenalist 38:e44f35f8129d 2944 sscanf(restPORT.c_str(),"%d",&connPort);
arsenalist 8:51a0ca9079ca 2945 for(int i=0; i<sizeof(s); i++) {
arsenalist 8:51a0ca9079ca 2946 s[i]=0; }
arsenalist 8:51a0ca9079ca 2947 sprintf(s,"/emma/api/controller/test");
arsenalist 8:51a0ca9079ca 2948 }
arsenalist 27:562a95c120cc 2949 wait(2);
arsenalist 8:51a0ca9079ca 2950 t.start();
arsenalist 28:7561035e3df5 2951 while(!esp.ready() && t.read() < 20);
arsenalist 8:51a0ca9079ca 2952 t.stop();
arsenalist 27:562a95c120cc 2953 t.reset();
arsenalist 8:51a0ca9079ca 2954 if(rest.begin(connHost.c_str(),connPort,false)) {
arsenalist 27:562a95c120cc 2955 DBG.printf("rest begin\r\n");
arsenalist 8:51a0ca9079ca 2956 esp.process();
arsenalist 8:51a0ca9079ca 2957 rest.get(s);
arsenalist 8:51a0ca9079ca 2958 for(int i=0; i<sizeof(s); i++) {
arsenalist 8:51a0ca9079ca 2959 s[i]=0; }
arsenalist 8:51a0ca9079ca 2960 rest.getResponse(s,sizeof(s));
arsenalist 8:51a0ca9079ca 2961 str = s;
arsenalist 27:562a95c120cc 2962 DBG.printf("response:%s\r\n",s);
arsenalist 8:51a0ca9079ca 2963 if(str.find("OK") != std::string::npos) {
arsenalist 8:51a0ca9079ca 2964 wifiConnected = true;
arsenalist 8:51a0ca9079ca 2965 }
arsenalist 8:51a0ca9079ca 2966 } else {
arsenalist 8:51a0ca9079ca 2967 wifiConnected = false;
arsenalist 8:51a0ca9079ca 2968 }
arsenalist 8:51a0ca9079ca 2969 } else {
arsenalist 8:51a0ca9079ca 2970 wifiConnected = false;
arsenalist 8:51a0ca9079ca 2971 }
arsenalist 8:51a0ca9079ca 2972 }
arsenalist 8:51a0ca9079ca 2973
arsenalist 8:51a0ca9079ca 2974 void isGprsConnected(void) {
arsenalist 8:51a0ca9079ca 2975
arsenalist 8:51a0ca9079ca 2976 }
arsenalist 0:f4e449fa34d7 2977
arsenalist 0:f4e449fa34d7 2978 void addChar(char *s, char c) {
arsenalist 0:f4e449fa34d7 2979 uint16_t k; //customized for EMS
arsenalist 0:f4e449fa34d7 2980 k = strlen(s);
arsenalist 0:f4e449fa34d7 2981 s[k] = c;
arsenalist 0:f4e449fa34d7 2982 s[k + 1] = 0;
arsenalist 0:f4e449fa34d7 2983 }
arsenalist 0:f4e449fa34d7 2984
arsenalist 0:f4e449fa34d7 2985 void rcvReply(char *r, int to) {
arsenalist 0:f4e449fa34d7 2986 Timer t;
arsenalist 0:f4e449fa34d7 2987 bool ended = false;
arsenalist 0:f4e449fa34d7 2988 char c;
arsenalist 0:f4e449fa34d7 2989
arsenalist 0:f4e449fa34d7 2990 strcpy(r,"");
arsenalist 0:f4e449fa34d7 2991 t.start();
arsenalist 0:f4e449fa34d7 2992 while(!ended) {
arsenalist 0:f4e449fa34d7 2993 if(_ESP.readable()) {
arsenalist 0:f4e449fa34d7 2994 c = _ESP.getc();
arsenalist 0:f4e449fa34d7 2995 addChar(r,c);
arsenalist 0:f4e449fa34d7 2996 t.start();
arsenalist 0:f4e449fa34d7 2997 }
arsenalist 0:f4e449fa34d7 2998 if(t.read_ms() > to) {
arsenalist 0:f4e449fa34d7 2999 ended = true;
arsenalist 0:f4e449fa34d7 3000 }
arsenalist 0:f4e449fa34d7 3001 }
arsenalist 0:f4e449fa34d7 3002 addChar(r, 0x00);
arsenalist 0:f4e449fa34d7 3003 }
arsenalist 0:f4e449fa34d7 3004
arsenalist 0:f4e449fa34d7 3005 string calculateMD5(string text) {
arsenalist 0:f4e449fa34d7 3006 char s[64];
arsenalist 0:f4e449fa34d7 3007 memset(s,0,sizeof(s)); //for unknown reason, after reading UID, the 's' will contaion UID data
arsenalist 0:f4e449fa34d7 3008 uint8_t hash[16];
arsenalist 0:f4e449fa34d7 3009 MD5::computeHash(hash, (uint8_t*)text.c_str(), strlen(text.c_str()));
arsenalist 0:f4e449fa34d7 3010 for(int i=0; i<16; ++i) {
arsenalist 0:f4e449fa34d7 3011 sprintf(s,"%s%02x",s,hash[i]);
arsenalist 0:f4e449fa34d7 3012 }
arsenalist 0:f4e449fa34d7 3013 return s;
arsenalist 0:f4e449fa34d7 3014 }
arsenalist 0:f4e449fa34d7 3015
arsenalist 0:f4e449fa34d7 3016 bool writeFirmwareHexToChar(string value) {
arsenalist 0:f4e449fa34d7 3017 FILE *fp;
arsenalist 0:f4e449fa34d7 3018 char s[32];
arsenalist 0:f4e449fa34d7 3019 int number;
arsenalist 0:f4e449fa34d7 3020 string chunk;
arsenalist 0:f4e449fa34d7 3021
arsenalist 0:f4e449fa34d7 3022 sprintf(s,"/sd/newFirmware/firmware.bin");
arsenalist 0:f4e449fa34d7 3023 fp = fopen(s,"a");
arsenalist 0:f4e449fa34d7 3024 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 3025 for(int ch=0; ch<value.size(); ch+=2) {
arsenalist 0:f4e449fa34d7 3026 chunk = value.substr(ch,2);
arsenalist 0:f4e449fa34d7 3027 sscanf(chunk.c_str(),"%x",&number);
arsenalist 0:f4e449fa34d7 3028 fprintf(fp,"%c",number);
arsenalist 0:f4e449fa34d7 3029 }
arsenalist 0:f4e449fa34d7 3030 fclose(fp);
arsenalist 30:08d290158fa7 3031 free(fp);
arsenalist 0:f4e449fa34d7 3032 return true;
arsenalist 0:f4e449fa34d7 3033 }
arsenalist 30:08d290158fa7 3034 fclose(fp);
arsenalist 30:08d290158fa7 3035 free(fp);
arsenalist 0:f4e449fa34d7 3036 return false;
arsenalist 0:f4e449fa34d7 3037 }
arsenalist 0:f4e449fa34d7 3038
arsenalist 0:f4e449fa34d7 3039 bool clearFirmware(void) {
arsenalist 0:f4e449fa34d7 3040 FILE *fp;
arsenalist 0:f4e449fa34d7 3041 char s[32];
arsenalist 0:f4e449fa34d7 3042
arsenalist 0:f4e449fa34d7 3043 sprintf(s,"/sd/newFirmware/firmware.bin");
arsenalist 0:f4e449fa34d7 3044 fp = fopen(s,"w");
arsenalist 0:f4e449fa34d7 3045 if(fp != NULL) {
arsenalist 0:f4e449fa34d7 3046 fprintf(fp,"");
arsenalist 0:f4e449fa34d7 3047 fclose(fp);
arsenalist 30:08d290158fa7 3048 free(fp);
arsenalist 0:f4e449fa34d7 3049 return true;
arsenalist 0:f4e449fa34d7 3050 }
arsenalist 30:08d290158fa7 3051 fclose(fp);
arsenalist 30:08d290158fa7 3052 free(fp);
arsenalist 0:f4e449fa34d7 3053 return false;
arsenalist 0:f4e449fa34d7 3054 }
arsenalist 27:562a95c120cc 3055
arsenalist 27:562a95c120cc 3056 bool writeLog(string logTime, string logData) {
arsenalist 27:562a95c120cc 3057 FILE *fp;
arsenalist 27:562a95c120cc 3058 char s[255];
arsenalist 27:562a95c120cc 3059 string logAll;
arsenalist 27:562a95c120cc 3060 logAll = logTime + "\t" + logData + "\r\n";
arsenalist 27:562a95c120cc 3061
arsenalist 27:562a95c120cc 3062 sprintf(s,"/sd/log.txt");
arsenalist 27:562a95c120cc 3063 fp = fopen(s,"a");
arsenalist 27:562a95c120cc 3064 if(fp != NULL) {
arsenalist 27:562a95c120cc 3065 fprintf(fp,logAll.c_str());
arsenalist 27:562a95c120cc 3066 fclose(fp);
arsenalist 30:08d290158fa7 3067 free(fp);
arsenalist 27:562a95c120cc 3068 return true;
arsenalist 27:562a95c120cc 3069 }
arsenalist 30:08d290158fa7 3070 fclose(fp);
arsenalist 30:08d290158fa7 3071 free(fp);
arsenalist 30:08d290158fa7 3072 return false;
arsenalist 30:08d290158fa7 3073 }
arsenalist 30:08d290158fa7 3074
arsenalist 30:08d290158fa7 3075 bool writeDbg(string dbgTime, string dbgData) {
arsenalist 30:08d290158fa7 3076 FILE *fp;
arsenalist 30:08d290158fa7 3077 char s[255];
arsenalist 30:08d290158fa7 3078 string dbgAll;
arsenalist 30:08d290158fa7 3079 dbgAll = dbgTime + "\t" + dbgData + "\r\n";
arsenalist 30:08d290158fa7 3080
arsenalist 30:08d290158fa7 3081 sprintf(s,"/sd/dbg.txt");
arsenalist 30:08d290158fa7 3082 fp = fopen(s,"a");
arsenalist 30:08d290158fa7 3083 if(fp != NULL) {
arsenalist 30:08d290158fa7 3084 fprintf(fp,dbgAll.c_str());
arsenalist 30:08d290158fa7 3085 fclose(fp);
arsenalist 30:08d290158fa7 3086 free(fp);
arsenalist 30:08d290158fa7 3087 return true;
arsenalist 30:08d290158fa7 3088 }
arsenalist 30:08d290158fa7 3089 fclose(fp);
arsenalist 30:08d290158fa7 3090 free(fp);
arsenalist 27:562a95c120cc 3091 return false;
arsenalist 27:562a95c120cc 3092 }
arsenalist 0:f4e449fa34d7 3093 /*end emma private function*/