Skip to main content
Bumped by Community user
added 1180 characters in body
Source Link

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.
  4. If I make a test and just make the response a String, char[] or char*, it parses fine.

Parsing logic [works]:

StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
float raw = root[String("main")][String("temp")].as<float>();

Parsing logic: [does not work]

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Request logic:

String RequestAPI::performRequest(String url, char* host){
WiFiClient client;
if (!client.connect(host, port)) {
    lcd -> clearScreen();
    lcd -> drawText("Failed to connect to API", 0, 0, this -> randomUint16());
    delay(5000);
    return "";
}

char endOfHeaders[] = "\r\n\r\n";
client.print(
String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + 
"Connection: close\r\n\r\n"
);
// Skiping HTTP headers from the response
client.find(endOfHeaders);

unsigned long startTime = millis();
while(client.available() == 0){
    if(millis() - startTime > this -> timeout){
        lcd -> clearScreen();
        lcd -> drawText("API request failed", 0, 0, this -> randomUint16());
        delay(5000);
        return "";
    }
}

String response = "";
while(client.available()){
    response += client.readStringUntil('\r');
}
Serial.println(response);

return response;
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.

Parsing logic [works]:

StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
float raw = root[String("main")][String("temp")].as<float>();

Parsing logic: [does not work]

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.
  4. If I make a test and just make the response a String, char[] or char*, it parses fine.

Parsing logic [works]:

StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
float raw = root[String("main")][String("temp")].as<float>();

Parsing logic: [does not work]

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Request logic:

String RequestAPI::performRequest(String url, char* host){
WiFiClient client;
if (!client.connect(host, port)) {
    lcd -> clearScreen();
    lcd -> drawText("Failed to connect to API", 0, 0, this -> randomUint16());
    delay(5000);
    return "";
}

char endOfHeaders[] = "\r\n\r\n";
client.print(
String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + 
"Connection: close\r\n\r\n"
);
// Skiping HTTP headers from the response
client.find(endOfHeaders);

unsigned long startTime = millis();
while(client.available() == 0){
    if(millis() - startTime > this -> timeout){
        lcd -> clearScreen();
        lcd -> drawText("API request failed", 0, 0, this -> randomUint16());
        delay(5000);
        return "";
    }
}

String response = "";
while(client.available()){
    response += client.readStringUntil('\r');
}
Serial.println(response);

return response;
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?

added 209 characters in body
Source Link

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.

Parsing logic [works]:

StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
float raw = root[String("main")][String("temp")].as<float>();

Parsing logic: [does not work]

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.

Parsing logic:

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.

Parsing logic [works]:

StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
float raw = root[String("main")][String("temp")].as<float>();

Parsing logic: [does not work]

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?

Source Link

Possible ArduinoJson bug on ESP8266?

I'm making a small IOT project using the ESP8266 in which I make 2 API calls:

  1. api.openweathermap.org for weather data
  2. api.timezonedb.com

The request logic is the same for both calls, the only difference being how I parse the response string. The first response is parsed fine, while the other isn't.

  1. I print the String responses to Serial, they look fine.
  2. Tried both Dynamic and Static buffers to no avail.
  3. Making the request only to the second API, still not being parsed.

Parsing logic:

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);

String data = root[String("formatted")].as<String>();

The response object looks like this:

{
  "status": "OK",
  "message": "",
  "formatted": "2017-06-30 11:34:13"
}

Now I'm thinking, could the empty message be causing a bug? Or am I doing something wrong?