Publishing several fields in one event

Hi,

I have 2 sensors that reads and after the data is published. After that we use webhook to send the info to our server.

Every reading does a push, but the problem is we make a lot of pushes from the same machine. Is it possible to publish a string of data? for example

Sensor1: 25.6 Sensor2: 22.6 in one event?

Thank’s

Eduard

Yes it is and the prefered way is via snprintf() and in JSON format.

Just for the records

It's not only possible but publishing strings is the only thing that works even with single values :wink:

Hi ScruffR

I did what you suggest but the result (json with event Ulatracker) comes with values completely wrong:

You can see the data with vlues without sense:

See the code below:

Thank’s for your eternal patience

Eduard

sensor.read();
double tempeatureFresh = sensor.celsius();
    Particle.publish("tempeatureFresh", String(tempeatureFresh), PRIVATE);
    digitalWrite(D4, LOW);
    

delay(5000);

digitalWrite(D5, HIGH);
//Reading Temperature Frozen
        sensor2.read();
double temperatureFrozen = sensor2.celsius();
    Particle.publish("temperatureFrozen", String(temperatureFrozen), PRIVATE);
    //Particle.publish("temperatureFrozen", String(sensor2.celsius()), PRIVATE);
    digitalWrite(D5, LOW);

//Reading battery level
FuelGauge fuel;
//float batt;
float batt = fuel.getVCell();

sprintf(publishString,"{\"tempeatureFresh\": %u, \"temperatureFrozen\": %u, \"batt\": %u}",tempeatureFresh,temperatureFrozen,batt);
Particle.publish("UlaTracker",publishString);

For float and double input variables snprintf() would require you to use something like "%.1f" instead of "%u".

snprintf() uses the same formatting pattern as printf()
%u stands for unsigned decimal integer - meaning an integer input type not to be confused with the output format.

Hi

I did your changes but I did received the same results.... you know why? thank'ssssss
sprintf(publishString,"{"tempeatureFresh": %1f, "temperatureFrozen": %.1f, "batt": %1f}",tempeatureFresh,temperatureFrozen,batt);

You left out the dot (.) in two cases to start with, but for more I’d have to look a bit closer - stay tuned.


Update:
How about this?

FuelGauge fuel;  // have this declarec globally only once 

void loop() {
  char msg[256]; // max length for publish, shorten to what's required

  sensor.read();
  double temperatureFresh = sensor.celsius();
  Particle.publish("temperatureFresh", String(temperatureFresh), PRIVATE);
  digitalWrite(D4, LOW);
    
  delay(5000);

  digitalWrite(D5, HIGH);
  //Reading Temperature Frozen
  sensor2.read();
  double temperatureFrozen = sensor2.celsius();
  Particle.publish("temperatureFrozen", String(temperatureFrozen), PRIVATE);
  //Particle.publish("temperatureFrozen", String(sensor2.celsius()), PRIVATE);
  digitalWrite(D5, LOW);

  //Reading battery level
  //FuelGauge fuel;
  //float batt;
  float batt = fuel.getVCell();

  snprintf(msg, sizeof(msg), "{\"temperatureFresh\":%.1f,\"temperatureFrozen\":%.1f,\"batt\":%.1f}", temperatureFresh, temperatureFrozen, batt);
  Particle.publish("UlaTracker_test", msg, PRIVATE);
}

Might want to change tempeatureFresh to temperatureFresh. Missing an “r” in the middle. :wink:

2 Likes

Done, although that’s only optics as it was written that way at all instances :wink:

1 Like