String within payload always comes as null on Arduino side

Hi

I have been using Losant with ESP8266 and ESP32 for a while now, always exchanging numbers and bolleans between the dashboard and the microcontroller. But now I need to send a string from the Dashboard to the microcontrollers (some 40 chars).
So, I create a String attribute -> create an input text box on Dashboard with that attribute set on the ‘Attribute’ field -> give a name to thee template id in thatinput box -> Add a button trigger and set its payload as follows:

{
   "Some_text":{{Template_id}}
}

And that’s it on the Losant side, on the Arduino IDE side, I have the hanles as follows:

void handleCommand(LosantCommand *command) {
  JsonObject& payload = *command->payload;
  serializeJson(payload, Serial);
 
  if (strcmp(command->name, "Template_id") == 0) {
    msgs[1] = payload["Some_text"].as<String>();
    Serial.println(msgs[1]);
   }
}

All works fine with numbers, for example, if I send 45 on the input box, I get the print on Arduino IDE:
{"Some_text":45}

But if I send letters…all I get is null.

On the Device Log, on Losant, I can see, for example if I send an Hello:

 "name":"Send_switch"
 "payload":"{\r\n \"Some_text\":Hello\r\n}\r\n"
 "time":"2020-08-17T22:49:22.450Z"

But on Arduino, always a null =
Any ideia?
I have been reading all I can find about Strings ectraction on payloads, theres a few posts but none of the solutions solved my problem =\

Thank you.
Cheers, Luís Pereira.

I’m adding some pictures for clarification:
Input_box

Hi @Luis_Pereira,

It sounds as though your entire object is being stringified prior to reaching the workflow, likely as the values are not recognized as JSON. I would recommend formatting your block like so to format the string:

{
   "Some_text":"{{Template_id}}"
}

Please let me know if this addition does not resolve the issue!

Thanks,
Julia

Hi Julia

Your suggestion worked!
With the payload block as you suggested:
{
“Some_text”:"{{Template_id}}"
}

If I send on the input box and Hello, causes the Log on Losant:

“name”:“Send_switch”
“payload”:{} 1 key

  • “Some_text”:“Hello”
    “time”:“2020-08-19T14:02:37.226Z”

And I get on Arduino IDE with the previous print mentioned before ( serializeJson(payload, json_string):wink: the following:

{"Some_text":"Hello"}

No longer null! Now I can start parsing the string for my information :slight_smile:
Thank you so much!
Cheers, Luís Pereira.

2 Likes