Sprintf creating bad formatting for data

Following advise from @peekay123 I used sprintf to bring order to my output.

However, now the data is coming out in a weird format?

Code:

      //Particle.publish("environment/temperature", String(bmp.readTemperature()));
      //Particle.publish("environment/temperature", String(temp);
      
      //Particle.publish("environment/pressure", String(pToHg(bmp.readPressure())));
      //Particle.publish("environment/altitude", String(bmp.readAltitude(seaLevelhPa)));
      
      sprintf(publishString,"{\"Temp\": %u, \"Press\": %u, \"Alt\": %u}",(bmp.readTemperature()),(pToHg(bmp.readPressure())),(bmp.readAltitude(seaLevelhPa)));
        Spark.publish("V",publishString);

Data:

{\"Temp\": 1610612736, \"Press\": 1077454438, \"Alt\": 1610612736}

Temperature is 1610612736??

Have you tried options other than %u?

What kind of numbers are those? Integers, doubles, long, etc?

Also, some of those parentheses can be eliminated I think.

sprintf(publishString, "{\"Temp\": %u, \"Press\": %u, \"Alt\": %u}", bmp.readTemperature(), pToHg(bmp.readPressure()), bmp.readAltitude(seaLevelhPa));
1 Like

if your BMP bmp.readTemperature() function actually returns a floating point number (float or double) it will be misinterpreted (as you observed).

In your case you are interpreting them as unsigned integers (the u format specifier).

You can cast the returns from your function, but unless the temperatures are in Kelvin, you may want to accommodate a signed number.

1 Like

@r0b0tn1k, I think you want:

sprintf(publishString,"{\"Temp\": %f, \"Press\": %f, \"Alt\": %f}", bmp.readTemperature(), pToHg(bmp.readPressure(), bmp.readAltitude(seaLevelhPa);

If you want more/less zeroes, you could use: %.1f, %.2f, etc.

1 Like

I think you misplaced a parenthesis or two.

1 Like

you are correct!

sprintf(publishString,"{\"Temp\": %f, \"Press\": %f, \"Alt\": %f}", bmp.readTemperature(), pToHg(bmp.readPressure()), bmp.readAltitude(seaLevelhPa));

that should do it.

1 Like