@charrold303 The C-String used to confuse me also but after playing around a bit I now see how simple they are to work with to format data before sending it in a Particle Publish.
Here is how I setup a C-String (Char array) in my code to send GPS Data & Battery Voltage to Losant.
First define your Char Array and change publishStateString
to what ever you want it to be:
The 256 is the max byte size of the array which is the max size of a Particle Publish payload +1
Now I use the snprintf
function to format the data that will be put into the char array before sending it out as the Particle Publish data payload.
snprintf(publishStateString, sizeof(publishStateString), "%.6f, %.6f:%.2f:%u:%.2f:%.2f:%.u:%.2f:",gps.location.lat(), gps.location.lng(), gps.altitude.feet(), gps.satellites.value(), (gps.hdop.value()/100.0), fuel.getSoC(), fuel.getVCell() );
Particle.publish("GPS", publishStateString);
Pay attention to this first line of code below because it contains the commands that actually format the data:
"%.6f, %.6f:%.2f:%u:%.2f:%.2f:%.u:%.2f:"
,gps.location.lat(), gps.location.lng(), gps.altitude.feet(), gps.satellites.value(), (gps.hdop.value()/100.0), fuel.getSoC(), fuel.getVCell() );
Now the %.6f
is a snprintf
formatting function for turning the data returned from gps.location.lat()
into a float data point with 6 numbers after the decimial point.
So you can see we use the same %.6f
for the gps.location.lng()
data point also because it’s also a float data point with 6 digits after the decimal point.
For the next data point we use %.2f
because gps.altitude.feet()
the accuracy of the altitude only goes to 2 points past the decimal point and it’s a floating point number.
Now for the next data point we use %u
because it’s not a float but a nubmer from 0-99 showing how many satellites are in view: gps.satellites.value()
And we just keep doing this as we go down the list of data variables we want to send.
Here is a reference for the different types of data formatting you can do using the snprintf
function.
http://www.cplusplus.com/reference/cstdio/printf/
When I send a Particle Publish the snprintf
function formats the data payload like this:
{"data"39.123456 , -85.123456:856.45:14:1.12:85:3.88:":","ttl":60,"published_at":"2017-07-10T20:28:13.934Z","coreid":"460042001051353338363333","name":"GPS"}
Then on the back end of Losant I can just pull data out between the colons and define what that data is so it can be databased and then made available to view in custom dashboards.
That may or may not make sense and I’m a newbie but after playing around with this and looking at what actually get’s Published will help you figure out this formatting function rather quickly.
The help is here if you need it.