I have been trying to retrieve the values after parsing the following Json array:
[{"Motor1":"OFF"},{"Motor2":"OFF"},{"Motor3":"OFF},{"Motor4":"OFF"},{"valve1":"OFF"},{"valve2":"OFF"},{"valve3":"OFF"},{"valve4":"OFF"}]
for this I have written following code:
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
const char* ssid = "hotspot";
const char* password = "12345678";
const char* host = "techaddictates.16mb.com";
void setup()
{
Serial.begin(9600);
delay(100); // We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop()
{
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort))
{
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/motors/people.json";
Serial.print("Requesting URL: ");
Serial.println(url); // This will send the request to the server
client.print(String("GET ") + url +" HTTP/1.1\r\n" + "Host: "
+ host + "\r\n" + "Connection: close\r\n\r\n");
delay(500);
String section="header";
while(client.available())
{
String line = client.readStringUntil('\r');
// we’ll parse the HTML body here
if(section=="header")
{ //headers..
//Serial.print(".");
if (line=="\n")
{ //skips the empty space at the beginning
section="json";
}
}
else if (section=="json")
{ // print the good stuff
section="ignore";
String result = line.substring(1);
Serial.print(result);
// Parse JSON
int size = result.length() + 1;
char json[size];
result.toCharArray(json, size);
StaticJsonBuffer<1000> jsonBuffer;
JsonArray& root = jsonBuffer.parseArray(json);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
const char* Motor1 = root['JSON'][0];
const char* Motor2 = root['JSON'][1];
const char* Motor3 = root['JSON'][2];
const char* Motor4 = root['JSON'][3];
const char* Valve1 = root['JSON'][4];
const char* Valve2 = root['JSON'][5];
const char* Valve3 = root['JSON'][6];
const char* Valve4 = root['JSON'][7];
Serial.println(Motor1);
Serial.println(Motor2);
Serial.println(Motor3);
Serial.println(Motor4);
Serial.println(Valve1);
Serial.println(Valve2);
Serial.println(Valve3);
Serial.println(Valve4);
}
}
Serial.println();
Serial.println("closing connection");
}
After burning it to ESP, in Serial monitor I am able to see the line received. I am expecting to see the retrieved values after parsing. But after the print the "result" what I get is only a blank space followed by directly with slogan "closing connection. I am expecting to get the retrieved values after parsing on the Serial monitor.
I have attached Snapshot image of the Serial monitor of what I get when I start Serial monitor
I am unable to figure out if my code is right or what changes should I make to get it work, any help please.....

