I am trying to retrieve JSON mqtt message I received in ESP32.
void mqttMsgCallback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
String _message = String((char*)payload);
String _topic = String(topic);
if (_topic.equals("Sys.GetInfo/"+getMacAddr()) == 1)
{
Serial.println("Message arrived for sys.GetInfo");
StaticJsonDocument <256> msg1;
Serial.println(_message);
deserializeJson(msg1,_message);
String callbackTopic = msg1["callbackTopic"];
Serial.println(callbackTopic);
}
}
The issue is when I publish mosquitto_pub -t "Sys.GetInfo/2462ABFC2CD0" -m "{"callbackTopic" : "1234"}"
it works well like callbackTopic as 1234.
but when I publish mosquitto_pub -t "Sys.GetInfo/2462ABFC2CD0" -m "{"callbackTopic" : "xyz"}"
it prints callbackTopic value as null.
Please point me if I an missing something.
Arduino version 1.8.15 ArduinoJson : 6.18.2 Borad : ESP32
Thanks
deserializeJson()
to see if it can even parse it as valid JSON. Maybe you need'{"callbackTopic" : "xyz"}'
(single quotes round the outside) to preserve the quotes on the inside.'{"callbackTopic" : "xyz"}'
. Thanks why the message with "" works whencallbackTopic
value is numericxyz
as a valid "thing".payload[length] = '\0';
how do you know there is a length + 1 position available?