Hi .. i am trying to control some switches through my webserver and Nodemcu. i am managing to connect to my Database and read data.
I want to send data back to the server but i am not how to construct a JSON from an array.
This is my code :
if(currentMillis - previousMillis >= interval) {
if ((WiFiMulti.run() == WL_CONNECTED)) {
outputsState = httpGETRequest(serverName);
Serial.println("OutputState ( {ID Key:[Switch ID, State, Mode, AlarmON, AlarmOFF,JustChanged]}");
Serial.println(outputsState);
Serial.println(" ");
JSONVar myObject = JSON.parse(outputsState);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++) {
JSONVar value = myObject[keys[i]];
Switch_ID[i] = atoi(value[0]);
Switch_Mode[i] = atoi(value[1]);
Switch_State[i] = atoi(value[2]);
Switch_AlarmOn[i] = atoi(value[3]);
Switch_AlarmOFF[i] = atoi(value[4]);
Switch_JustChanged[i] = atoi(value[5]);
if (Switch_Mode[i] == 0 && Switch_State[i] == 0 && Switch_JustChanged[i] == 0) {
mySwitch315.send(Switch_ID[i], 24);
New_State[i] = 1;
New_JustChanged[i] = 1;
}
if (Switch_Mode[i] == 0 && Switch_State[i] == 1 && Switch_JustChanged[i] == 1) {
mySwitch315.send(Switch_ID[i], 24);
New_State[i] = 0;
New_JustChanged[i] = 0;
}
if (Switch_Mode[i] == 1 && Switch_AlarmOn[i] == 1 && Switch_State[i] == 0 && Switch_JustChanged[i] == 0) {
mySwitch315.send(Switch_ID[i], 24);
New_State[i] = 1;
New_JustChanged[i] = 1;
}
if (Switch_Mode[i] == 1 && Switch_AlarmOFF[i] == 1 && Switch_State[i] == 1 && Switch_JustChanged[i] == 1) {
mySwitch315.send(Switch_ID[i], 24);
New_State[i] = 0;
New_JustChanged[i] = 0;
}
}
Serial.println(Switch_ID[0]);
Serial.println(Switch_Mode[0]);
Serial.println(Switch_State[0]);
Serial.println(Switch_AlarmOn[0]);
Serial.println(Switch_AlarmOFF[0]);
Serial.println(Switch_JustChanged[0]);
Serial.println(New_JustChanged[0]);
Serial.println(New_State[0]);
previousMillis = currentMillis;
//How to send back ? ....
//Post_Response_Data(Switch_ID[], Switch_AlarmON[], Switch_AlarmOFF[], New_JustChanged[], New_State[]);
this my my "outputState" in the monitor :
OutputState ( {ID Key:[Switch ID, State, Mode, AlarmON, AlarmOFF,JustChanged]}
{"3038947":["3038947","0","0","0","0","0"],"3038948":["3038948","0","0","0","0","0"]}
How can i construct a JSON from the arrays : Switch_ID[], ... so i can send it back to the server.
Thanks.