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.