Using String::format() with float and string

I am attempting to send a JSON with multiple payload datapoints. My event name is dataup. When I replace "String::format("{"tcdata":"%f","towerid":%s}", tcdata, towerid)" with String(tcdata) it works, but then I can't parse the payload on the other end of my Webhook.

This is the template I am using. I tried room.c_str() but this doesn't translate for a floating point. I'm really struggling to understand this, any advice is appreciated.

FIRMWARE
String room = "kitchen";
int temp = 20;
String data = String::format(
  "{ \"room\": \"%s\", \"temp\": %d }",
  room.c_str(), temp);
client.publish("test", data, PRIVATE);

WEBHOOK
{
  "eventName": "test",
  "url": "http://example.com/log/{{{room}}}",
  "json": {
    "location": "{{{room}}}",
    "temperature": "{{{temp}}}"
  }
}

Here's my code.

String towerid = "ancillary_1";
float tcdata = maxthermo.readThermocoupleTemperature();
client.publish("dataup", String::format("{\"tcdata\":\"%f\",\"towerid\":%s}", tcdata, towerid), PRIVATE, NO_ACK);

It looks like you are missing the trailing (escaped) quote following your %s....
It may be easier to do something like this:

String towerid = "ancillary_1";
float tcdata = maxthermo.readThermocoupleTemperature();
String payload = "{\"tcdata\":\" + String(tcdata) + "\",\"towerid\":" + String(towerid) + "\"}";
client.publish("dataup", payload), PRIVATE, NO_ACK);

I'm getting an error on the stray backslash that is not red in that string.

This goes beyond the scope of my original question.
If I run this code it prints the way I want it to.

  char towerid[15] = "main1";
  float tcdata = maxthermo.readThermocoupleTemperature();
  
  char jsondata[69];
  sprintf(jsondata, "{\"towerid\":%s,\"tcdata\":%.5f}",
  towerid, tcdata);

  client.publish("dataup2", jsondata, PRIVATE, NO_ACK);

Idk why it works, but if I use %s after the towerid char it comes out how I want. If towerid is a String and not char I get different characters. That code prints something like this.

{"towerid":main1,"tcdata":1000.12500}

This is a JSON payload string. When I send it to a webhook, all I get are null values. If I strike out the char and only use the float number, the webhook posts the numbers correctly.

  char towerid[15] = "main1";
  float tcdata = maxthermo.readThermocoupleTemperature();
  
  char jsondata[69];
  sprintf(jsondata, "{\"tcdata\":%.5f}",
  tcdata);

  client.publish("dataup2", jsondata, PRIVATE, NO_ACK);

Is there a problem with my formatting that the program listening to my webhook event can't recognize variables? Here's my JSON setup. Thanks for your help.

{
  "event": "{{{eventname}}}",
  "tcdata": "{{{tcdata}}}",
  "irdata": "{{{irdata}}}",
  "towerid": "{{{towerid}}}",
  "coreid": "{{{deviceID}}}",
  "published_at": "{{{time}}}"
}

Those are different things. If towerid is a String then you need to pass the underlying c string using the towerid.c_str() function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.