0

I'm struggling with this piece of code and I can't get my head around it. So I manage to get a return from an api in JSON. I receive the reply in the String Client. But I can't get any data out this string. I've checked my code for hours but didn't get any closer. What is going wrong here?

My JSON code is:

{
    "devices": {
        "thermostats": {
            "e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU": {
                "humidity": 40,
                "locale": "nl-NL",
                "temperature_scale": "C",
                "is_using_emergency_heat": false,
                "has_fan": false,
                "software_version": "5.6.6-4",
                "has_leaf": false,
                "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA",
                "device_id": "e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU",
                "name": "Downstairs",
                "can_heat": true,
                "can_cool": false,
                "target_temperature_c": 23.5,
                "target_temperature_f": 75,
                "target_temperature_high_c": 24,
                "target_temperature_high_f": 75,
                "target_temperature_low_c": 20,
                "target_temperature_low_f": 68,
                "ambient_temperature_c": 15,
                "ambient_temperature_f": 60,
                "away_temperature_high_c": 24,
                "away_temperature_high_f": 76,
                "away_temperature_low_c": 8.5,
                "away_temperature_low_f": 48,
                "eco_temperature_high_c": 24,
                "eco_temperature_high_f": 76,
                "eco_temperature_low_c": 8.5,
                "eco_temperature_low_f": 48,
                "is_locked": false,
                "locked_temp_min_c": 20,
                "locked_temp_min_f": 68,
                "locked_temp_max_c": 22,
                "locked_temp_max_f": 72,
                "sunlight_correction_active": false,
                "sunlight_correction_enabled": true,
                "structure_id": "vEJb634MNif-xVxcn3hIdIAAdT2p5L7beo2amUiTuBVoEPy3C0PAFw",
                "fan_timer_active": false,
                "fan_timer_timeout": "1970-01-01T00:00:00.000Z",
                "fan_timer_duration": 15,
                "previous_hvac_mode": "",
                "hvac_mode": "heat",
                "time_to_target": "~0",
                "time_to_target_training": "ready",
                "where_name": "Downstairs",
                "label": "",
                "name_long": "Downstairs Thermostat",
                "is_online": true,
                "last_connection": "2018-02-17T08:56:41.512Z",
                "hvac_state": "heating"
            }
        }
    },
    "structures": {
        "NzsPBklZNQBC4FVqmRUaVLqMhPVJII3IzQ0QfhReeGv_UCagNdtMhg": {
            "name": "Structure 1",
            "country_code": "US",
            "time_zone": "America/Los_Angeles",
            "away": "home",
            "structure_id": "NzsPBklZNQBC4FVqmRUaVLqMhPVJII3IzQ0QfhReeGv_UCagNdtMhg",
            "wheres": {
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6FUQ2RVTLC5w": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6FUQ2RVTLC5w",
                    "name": "Backyard"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4POqHyZw5GHQ": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4POqHyZw5GHQ",
                    "name": "Basement"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5QjU1FA_slkA": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5QjU1FA_slkA",
                    "name": "Bedroom"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5kuu8P9K1TFQ": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5kuu8P9K1TFQ",
                    "name": "Den"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4ncylMQyZn-g": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4ncylMQyZn-g",
                    "name": "Dining Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA",
                    "name": "Downstairs"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5rtDphaQytaQ": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5rtDphaQytaQ",
                    "name": "Driveway"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5Z5XxmZr9Aeg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5Z5XxmZr9Aeg",
                    "name": "Entryway"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7r62EA7u7DWw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7r62EA7u7DWw",
                    "name": "Family Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6o_Kr6tv-CSA": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6o_Kr6tv-CSA",
                    "name": "Front Yard"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5OO0kfgPRADw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5OO0kfgPRADw",
                    "name": "Guest House"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5AMihmAgobTw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5AMihmAgobTw",
                    "name": "Guest Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6MLDlLEu4-2A": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6MLDlLEu4-2A",
                    "name": "Hallway"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5t08bO8j0Mzg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5t08bO8j0Mzg",
                    "name": "Kids Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7p1ASlDNUnHg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7p1ASlDNUnHg",
                    "name": "Kitchen"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7uLdRQIIIvww": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7uLdRQIIIvww",
                    "name": "Living Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5W7d3KtKxzfg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5W7d3KtKxzfg",
                    "name": "Master Bedroom"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6arlkBKu_K8Q": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6arlkBKu_K8Q",
                    "name": "Office"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7gc73riRgkvw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7gc73riRgkvw",
                    "name": "Outside"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6c07Ca7PZt2A": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6c07Ca7PZt2A",
                    "name": "Upstairs"
                }
            }
        },
        "vEJb634MNif-xVxcn3hIdIAAdT2p5L7beo2amUiTuBVoEPy3C0PAFw": {
            "name": "CNT/FAI",
            "country_code": "BE",
            "time_zone": "Europe/Brussels",
            "away": "home",
            "thermostats": [
                "e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU"
            ],
            "structure_id": "vEJb634MNif-xVxcn3hIdIAAdT2p5L7beo2amUiTuBVoEPy3C0PAFw",
            "wheres": {
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6FUQ2RVTLC5w": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6FUQ2RVTLC5w",
                    "name": "Backyard"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4POqHyZw5GHQ": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4POqHyZw5GHQ",
                    "name": "Basement"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5QjU1FA_slkA": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5QjU1FA_slkA",
                    "name": "Bedroom"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5kuu8P9K1TFQ": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5kuu8P9K1TFQ",
                    "name": "Den"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4ncylMQyZn-g": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG4ncylMQyZn-g",
                    "name": "Dining Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA",
                    "name": "Downstairs"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5rtDphaQytaQ": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5rtDphaQytaQ",
                    "name": "Driveway"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5Z5XxmZr9Aeg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5Z5XxmZr9Aeg",
                    "name": "Entryway"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7r62EA7u7DWw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7r62EA7u7DWw",
                    "name": "Family Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6o_Kr6tv-CSA": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6o_Kr6tv-CSA",
                    "name": "Front Yard"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5OO0kfgPRADw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5OO0kfgPRADw",
                    "name": "Guest House"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5AMihmAgobTw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5AMihmAgobTw",
                    "name": "Guest Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6MLDlLEu4-2A": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6MLDlLEu4-2A",
                    "name": "Hallway"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5t08bO8j0Mzg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5t08bO8j0Mzg",
                    "name": "Kids Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7p1ASlDNUnHg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7p1ASlDNUnHg",
                    "name": "Kitchen"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7uLdRQIIIvww": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7uLdRQIIIvww",
                    "name": "Living Room"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5W7d3KtKxzfg": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG5W7d3KtKxzfg",
                    "name": "Master Bedroom"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6arlkBKu_K8Q": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6arlkBKu_K8Q",
                    "name": "Office"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7gc73riRgkvw": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7gc73riRgkvw",
                    "name": "Outside"
                },
                "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6c07Ca7PZt2A": {
                    "where_id": "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG6c07Ca7PZt2A",
                    "name": "Upstairs"
                }
            }
        }
    },
    "metadata": {
        "access_token": "SECURITY",
        "client_version": 2,
        "user_id": "SECURITY"
    }
}

My ESP8266 code is:

String json = ""; 
while (client.available()) {
     String line = client.readStringUntil('\r');

    Serial.println("reply was:");
    Serial.println("==========");
    Serial.println(line);
    Serial.println("==========");
    Serial.println("closing connection");
    digitalWrite(gpio13Led, LOW);
    json = json + line;
    Serial.print("json");
    Serial.println(json);
 }

// Reading JSON
   DynamicJsonBuffer jsonBuffer(10833);
      JsonObject& root = jsonBuffer.parseObject(json);
   Serial.print("JsonObject: ");
   JsonObject&  thermoStat = root["devices"]["thermostats"]["e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU"];


const char* hvacState = thermoStat["hvac_state"]; // "heating"

Serial.println("hvac");
Serial.print(hvacState);

//compare hvacState
if (hvacState == "off") {
  Serial.print("Off");
  }
if   (hvacState == "heating"){
Serial.print("On");
}


}

My serial output is:

reply was:
==========
{"devices":{"thermostats":{"e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU":{"humidity...
==========
closing connection

json
JsonObject: hvac

Similar code mentioned here did work. But there i work with const char* while now i get the response from the api in a string. I've no idea how to do this differently.

After some suggestions in the answers I've tried the following code for the same JSON code:

#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200);
  Serial.println("TEST START");


//Serial.println("Buffer" + bufferSize);
const char* json = "{\blanked oUT}}";

Serial.println(json);

DynamicJsonBuffer jsonBuffer(10833);
JsonObject& root = jsonBuffer.parseObject(json);
//Serial.print("JsonObject: ");
//Serial.println((int)root["devices"]["thermostats"]["e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU"]);
JsonObject& thermostat = root["devices"]["thermostats"]["e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU"];

const char* hvacState = thermostat["hvac_state"]; // "heating"


Serial.println("hvac:");
Serial.println(hvacState);

if (hvacState == "off") {
  Serial.print("Uit");
  }
if   (hvacState == "heating"){
Serial.print("Aan");
}
}
void loop() {
  // put your main code here, to run repeatedly:

}

The response is then:

TEST START
{"devices":{all blanked out}}
hvac:
off

As you can see, besides the IF() everything else works. But if I change the const char* to String The code doesn't work. Unfortunately I need to parse a string in order to let my full code work.

1
  • Thx for your response. I did what you suggested. And I'm getting the idea that the string(line) never gets parsed because the library only parse const char*. But then I'm getting confused when i see other examples where they do use strings as well. I'll alter my code to show you what goes wrong. Maybe you can help me further then. Commented Feb 17, 2018 at 8:20

2 Answers 2

0

Might be worth doing separate test with a hardcoded JSON string you pass to the JSON object to make sure parsing works as expected.

ArduinoJSON Assistant should help stub out JSON parsing.

Based on your comment and the JSON content on your other question it looks like you're either getting incomplete or jumbled up.

Here's a manually formatted version of the JSON response. Bare in mind the last part is made up just ensure a valid JSON object is used:

{
    "devices":
    {
        "thermostats":
        {
            "exxx":
                {
                    "humidity":40,
                    "locale":"nl-NL",
                    "temperature_scale":"C",
                    "is_using_emergency_heat":false,
                    "has_fan":false,
                    "software_version":"5.6.6-4",
                    "has_leaf":true,
                    "where_id":"YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA",
                    "device_id":"e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU",
                    "name":"Downstairs",
                    "can_heat":true,
                    "can_cool":false,
                    "target_temperature_c":15.0,
                    "target_temperature_f":59,
                    "target_temperature_high_c":24.0,
                    "target_temperature_high_f":75,
                    "target_temperature_low_c":20.0,
                    "target_temperature_low_f":68,
                    "ambient_temperature_c":15.5,
                    "ambient_temperature_f":61,
                    "away_temperature_high_c":24.0,
                    "away_temperature_high_f":76,
                    "away_temperature_low_c":8.5,
                    "away_temperature_low_f":48,
                    "eco_temperature_high_c":24.0,
                    "eco_temperature_high_f":76,
                    "eco_temperature_low_c":8.5,
                    "eco_temperature_low_f":48,
                    "is_locked":false,
                    "locked_temp_min_c":20.0,
                    "locked_temp_min_f":68,
                    "locked_temp_max_c":22.0,
                    "locked_temp_max_f":72,
                    "sunlight_correction_active":false,
                    "sunlight_correction_enabled":true,
                    "structure_id":"vEJb634MNif-xxx",
                    "something_else_?":
                        {
                            "access_token":"xxxx",
                            "client_version":2,
                            "user_id":"xxxx"
                        }
                }
        }
    }
}

With this, the ArduinoJson Assistant can generate parsing code:

const size_t bufferSize = 3*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(37) + 1100;
DynamicJsonBuffer jsonBuffer(bufferSize);

const char* json = "{\"devices\":{\"thermostats\":{\"exxx\":{\"humidity\":40,\"locale\":\"nl-NL\",\"temperature_scale\":\"C\",\"is_using_emergency_heat\":false,\"has_fan\":false,\"software_version\":\"5.6.6-4\",\"has_leaf\":true,\"where_id\":\"YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA\",\"device_id\":\"e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU\",\"name\":\"Downstairs\",\"can_heat\":true,\"can_cool\":false,\"target_temperature_c\":15,\"target_temperature_f\":59,\"target_temperature_high_c\":24,\"target_temperature_high_f\":75,\"target_temperature_low_c\":20,\"target_temperature_low_f\":68,\"ambient_temperature_c\":15.5,\"ambient_temperature_f\":61,\"away_temperature_high_c\":24,\"away_temperature_high_f\":76,\"away_temperature_low_c\":8.5,\"away_temperature_low_f\":48,\"eco_temperature_high_c\":24,\"eco_temperature_high_f\":76,\"eco_temperature_low_c\":8.5,\"eco_temperature_low_f\":48,\"is_locked\":false,\"locked_temp_min_c\":20,\"locked_temp_min_f\":68,\"locked_temp_max_c\":22,\"locked_temp_max_f\":72,\"sunlight_correction_active\":false,\"sunlight_correction_enabled\":true,\"structure_id\":\"vEJb634MNif-xxx\",\"something_else_?\":{\"access_token\":\"xxxx\",\"client_version\":2,\"user_id\":\"xxxx\"}}}}}";

JsonObject& root = jsonBuffer.parseObject(json);

JsonObject& devices_thermostats_exxx = root["devices"]["thermostats"]["exxx"];
int devices_thermostats_exxx_humidity = devices_thermostats_exxx["humidity"]; // 40
const char* devices_thermostats_exxx_locale = devices_thermostats_exxx["locale"]; // "nl-NL"
const char* devices_thermostats_exxx_temperature_scale = devices_thermostats_exxx["temperature_scale"]; // "C"
bool devices_thermostats_exxx_is_using_emergency_heat = devices_thermostats_exxx["is_using_emergency_heat"]; // false
bool devices_thermostats_exxx_has_fan = devices_thermostats_exxx["has_fan"]; // false
const char* devices_thermostats_exxx_software_version = devices_thermostats_exxx["software_version"]; // "5.6.6-4"
bool devices_thermostats_exxx_has_leaf = devices_thermostats_exxx["has_leaf"]; // true
const char* devices_thermostats_exxx_where_id = devices_thermostats_exxx["where_id"]; // "YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA"
const char* devices_thermostats_exxx_device_id = devices_thermostats_exxx["device_id"]; // "e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU"
const char* devices_thermostats_exxx_name = devices_thermostats_exxx["name"]; // "Downstairs"
bool devices_thermostats_exxx_can_heat = devices_thermostats_exxx["can_heat"]; // true
bool devices_thermostats_exxx_can_cool = devices_thermostats_exxx["can_cool"]; // false
int devices_thermostats_exxx_target_temperature_c = devices_thermostats_exxx["target_temperature_c"]; // 15
int devices_thermostats_exxx_target_temperature_f = devices_thermostats_exxx["target_temperature_f"]; // 59
int devices_thermostats_exxx_target_temperature_high_c = devices_thermostats_exxx["target_temperature_high_c"]; // 24
int devices_thermostats_exxx_target_temperature_high_f = devices_thermostats_exxx["target_temperature_high_f"]; // 75
int devices_thermostats_exxx_target_temperature_low_c = devices_thermostats_exxx["target_temperature_low_c"]; // 20
int devices_thermostats_exxx_target_temperature_low_f = devices_thermostats_exxx["target_temperature_low_f"]; // 68
float devices_thermostats_exxx_ambient_temperature_c = devices_thermostats_exxx["ambient_temperature_c"]; // 15.5
int devices_thermostats_exxx_ambient_temperature_f = devices_thermostats_exxx["ambient_temperature_f"]; // 61
int devices_thermostats_exxx_away_temperature_high_c = devices_thermostats_exxx["away_temperature_high_c"]; // 24
int devices_thermostats_exxx_away_temperature_high_f = devices_thermostats_exxx["away_temperature_high_f"]; // 76
float devices_thermostats_exxx_away_temperature_low_c = devices_thermostats_exxx["away_temperature_low_c"]; // 8.5
int devices_thermostats_exxx_away_temperature_low_f = devices_thermostats_exxx["away_temperature_low_f"]; // 48
int devices_thermostats_exxx_eco_temperature_high_c = devices_thermostats_exxx["eco_temperature_high_c"]; // 24
int devices_thermostats_exxx_eco_temperature_high_f = devices_thermostats_exxx["eco_temperature_high_f"]; // 76
float devices_thermostats_exxx_eco_temperature_low_c = devices_thermostats_exxx["eco_temperature_low_c"]; // 8.5
int devices_thermostats_exxx_eco_temperature_low_f = devices_thermostats_exxx["eco_temperature_low_f"]; // 48
bool devices_thermostats_exxx_is_locked = devices_thermostats_exxx["is_locked"]; // false
int devices_thermostats_exxx_locked_temp_min_c = devices_thermostats_exxx["locked_temp_min_c"]; // 20
int devices_thermostats_exxx_locked_temp_min_f = devices_thermostats_exxx["locked_temp_min_f"]; // 68
int devices_thermostats_exxx_locked_temp_max_c = devices_thermostats_exxx["locked_temp_max_c"]; // 22
int devices_thermostats_exxx_locked_temp_max_f = devices_thermostats_exxx["locked_temp_max_f"]; // 72
bool devices_thermostats_exxx_sunlight_correction_active = devices_thermostats_exxx["sunlight_correction_active"]; // false
bool devices_thermostats_exxx_sunlight_correction_enabled = devices_thermostats_exxx["sunlight_correction_enabled"]; // true
const char* devices_thermostats_exxx_structure_id = devices_thermostats_exxx["structure_id"]; // "vEJb634MNif-xxx"

JsonObject& devices_thermostats_exxx_something_else_ = devices_thermostats_exxx["something_else_?"];
const char* devices_thermostats_exxx_something_else__access_token = devices_thermostats_exxx_something_else_["access_token"]; // "xxxx"
int devices_thermostats_exxx_something_else__client_version = devices_thermostats_exxx_something_else_["client_version"]; // 2
const char* devices_thermostats_exxx_something_else__user_id = devices_thermostats_exxx_something_else_["user_id"]; // "xxxx"

Back to your question: "hvac_state" doesn't exist in this data, so this might be one reason your attempt didn't work.

Additionally, as mentioned in the comments, test the api response outside the ESP (plain request on a computer) to make sure that works as expected. If it does, debug how the string that gets passed to the JSON object in Arduino/ESP code gets concatenated (if all data makes it in, in the right order, etc.)

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried with a String but didn't really work. With a Constant char* it did, sort of. But then the json = json +line doesn't work.
is the serial output above full or have you manually truncated it ? It looks like it doesn't have a full JSON object there
I manually made it smaller. It's a huge response. In my previous question you can see the full one and my first testcode. But there i was not using strings like now.
I went to your previous question, found the test code and it's invalid JSON: ERROR: Unexpected token a in JSON at position 1034 (try running it through the Assistant or JSONLint). This section looks dodgy: "structure_id":"vEJb634MNif-xxx{"access_token":"xxxx","client_version":2,"user_id":"xxxx"}}..."structure_id":"vEJb634MNif-xxx looks incomplete, or jumbled up). debug the response without the ESP first to make sure what comes in is legit and only if it is look into how the reponse is buffered.
0

Ok, I found the answer to my problem. Sort of.. In the end I noticed that the response from the api was never complete. So the parsing could have never worked.

1 Comment

Glad you have spotted that. I have mentioned you're either getting incomplete or jumbled up JSON data in my answer bellow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.