Hi,
I'm using Arduinojson v6.19.4 to program an ESP32 to water my house plants.
The ESP32 sends soil moisture readings via MQTT to Home Assistant which sends MQTT messages to turn water pumps on and off.
Each ESP32 will have multiple pumps attached and info for the pumps will be stored in an array. Home Assistant sends messages similar to the following turn on pumps on and off. The first value ("1" in the example below) will be used to access the pump array.
{ "1" : { "Power" : "ON" } }
My question is how best to deserialize this message: I need to get that first value - the pump number - into a variable. I've developed the following code, but its not particularly elegant. Is there a way to get the pump number into a variable without the for loop?
if (mqttTopic == mqttClientID + "/Irrigation/Set_Pump") {
Serial.println ("MQTT Topic / Action " + mqttTopic + " / " + mqttAction);
DeserializationError dsErr = deserializeJson (jsonDoc, mqttAction);
if (dsErr) {
Serial.print ("Set_Pump: Error parsing mqttAction: ");
Serial.println (dsErr.f_str());
}
else {
JsonObject jsonObject = jsonDoc.as<JsonObject>();
for (JsonPair kv : jsonObject) {
JsonVariant jv = kv.value().as<JsonVariant>();
Serial.println ("Pump: " + String(kv.key().c_str()) );
Serial.println ("Power: " + jv ["Power"].as<String>());
}
}
}
Here's the output
19:12:51.057 -> MQTT Topic / Action ESP32.EE.18/Irrigation/Set_Pump / { "1" : { "Power" : "ON" } }
19:12:51.057 -> Pump: 1
19:12:51.057 -> Power: ON
BTW, I could change the json to something like the following, but I'm using this sketch as a learning experience as well, so wondering if there is an elegant way to use the json above.
{ "Pump" : "1", "Power" : "ON" }
I appreciate any help I can get!
Thanks