I am sending a string as payload with device command from Losant to nodemcu. Serial monitor shows nothing.
Where it shows curly braces, it should be displaying a string (“Just about anything!”).
I have verified the following:
The device is connected
It receives the command
The device log shows message sent with the string.
Code compiles but doesn’t work as intended.
// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
Serial.print("Command received: ");
Serial.println(command->name);
if(strcmp(command->name, "toggle") == 0) {
toggle();
}
// TRY 1: Print payload using printTo
JsonObject& payload = *command->payload;
Serial.println("Received payload: ");
payload.printTo(Serial); // Gives {}
Serial.println();
// TRY 2: Serialize payload and print
char json[200];
StaticJsonBuffer<200> jsonBuffer;
payload.printTo(json, sizeof(json));
Serial.print(json); // Gives {}
}
Apologies for the confusion here. Our Arduino client is expecting JSON data within commands. Since that string is not valid JSON, the client is failing to parse the command payload, so nothing is returned.
The fastest workaround would be to make your payload JSON. Something like { "value" : "Just about any thing!" }
. You can then access that field using the following:
JsonObject& payload = *command->payload;
Serial.println(payload["value"]);
I created an issue on the repo to add a new field to the command object for the raw string for cases like this.
Also, make sure to increase the max mqtt packet size if you’re sending commands. Instructions here:
If you’re using a Losant Builder Kit or the losant-sdk-arduino on another board, you’ll likely hit the default mqtt packet size limit defined in the underlying pubsubclient library. Unfortunately the packet is blocked before reaching any of your code, so it’s hard to debug. It simply looks like the command was never received. For example:
{ "foo" : "bar" } works, whereas { "somethingLarger" : "with a longer value" } doesn’t work. This is because the default packet size is 128, which provides en…