Particle - GPS Device - Publish - String or StringList to google cloud

I am looking to send some GPS co-ordinates and save these into google cloud database.

Previously on the particle device i have done this successfully sending only one value using;

data = analogValue
Particle.publish(“my_event”), data, PRIVATE);

However, for my GPS i want to send both latitude and longitude values.

What is the best method of publishing two values as opposed to just one?

It depends on how you would like to parse the string on the receiving end. JSON and CSV are both popular formats for sending a series of values in a single publish.

Essentially you would need a char buffer (c-string) to format the data into, and then publish that buffer. There are plenty of examples of using snprintf() here on the forum.

// Buffer to store the generated message
char message[255];

// Example using two doubles and an int
double data1 = 123.4;
double data2 = 56.78;
int data3 = 42;

// CSV formatting
snprintf(message, sizeof(message), "%.2f,%.2f,%d", data1, data2, data3);

// JSON formatting
snprintf(message, sizeof(message), "{\"data1\":%.2f, \"data2\":%.2f, \"data3\":%d}", data1, data2, data3);

// Publish the buffer
Particle.publish("my_event", message, PRIVATE);

Thank you very much!!! That is so much appreciated, i will update on how i get on :slight_smile:

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