Particle publishing --- how to?

Hello…
I am testing out some stuff on the Argon and would like to know how to format a string with multiple variables to publish.
I understand the following:

Particle.publish("Vbias",String::format("%0.2f", SensorDataPerCyclePublish.Vbias[current]));

Just not sure how to add (or even if I can) a second variable to this line WITHOUT calling publish a second time. I ask bcz at the moment I am trying to publish 24 events in a 20 second window and it appears (via reading the documentation) that there is a limitation of 4 publishes per second which would mean I would have to get crafty with a SoftwareTimer to break up the publishing into 4’s every second. Not impossible BUT the blocks of data come in 6’s not 4’s. Eventually I am going to use a web-hook to send them to a google spreadsheet.

Thanks

With one publish statement (event & message), you can send multiple data pairs within the message.
Here is a post you can review for a useful tool:
https://community.particle.io/t/json-parser-and-generator-in-device-os/56147

Here is an example:

char msg128[128];
double averageTempF;
double averageHumidity;
char usbPoweredSzInfo[16] = "no data...";

void formatVariableDataPairs() {
  JSONBufferWriter writer(msg128, sizeof(msg128) - 1);
  writer.beginObject();
    writer.name("tempF").value(averageTempF);
    writer.name("rh").value(averageHumidity);
    writer.name("mains power").value(usbPoweredSzInfo);
  writer.endObject();
  writer.buffer()[std::min(writer.bufferSize(), writer.dataSize())] = 0;
}

loop() {
  //fillVarsWithData
  formatVariableDataPairs();
  if (Particle.connected()) { //will block if NOT connected
    Particle.publish("stats", msg128);
    delay(1000);
  }
  delay(10000); // demo only
}
1 Like

Maybe looking into the reference docs can help

Particularly the supplemental link directing you to printf()

While at it you may also want to look into snprintf() to avoid using String and rather go with old-fashioned C-strings.

1 Like

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