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);

Hi @paul41 regarding to this post I collected the data from your GPS module and accelerometer and then put all together to nice JSON :slight_smile:

the code compile for Argon without any errors and is here:

Please note that I don’t have either GPS Module either MPU6050 I’m 100% sure that this code still will need some modyfications !!! to fit your needs but will give you some Idea how to deall with this.

to test this code I used online GDB compiller and then I make some “fake” data pretending yours accelerometer and GPS module

#include <iostream>
#include <stdint.h>

using namespace std;

struct Pgtop{

  int commandId = 22;
  int reference = 3;

}pgtop;

struct Gga{

  const char* utcTime = "\"10/7/2020 19:56\"";
  const char* latitude = "56.8999";
  const char* northSouthIndicator = "\"n_ind\"";
  const char* longitude = "36.987";
  const char* eastWestIndicator = "\"e/w_ind\"";
  int positionFixIndicator = 22;
  int satellitesUsed = 7;
  float hdop = 77.87;
  float altitude = 998.27;
  float geoidalSeparation = 45.22;
  const char* ageOfDiffCorr = "\"really_old\"";

} gga;

struct Rmc{

  const char* utcTime = "\"10/7/2020 19:56\"";
  const char* latitude = "56.8999";
  const char* northSouthIndicator = "\"n_ind\"";
  const char* longitude = "36.987";
  const char* eastWestIndicator = "\"e/w_ind\"";
  float speedOverGround = 22.66;
  float courseOverGround = 7.23;
  const char* date = "\"today hey\"";
  float magneticVariation = 98.27;
  const char* magneticVariationDirection = "\"left\"";
  const char* mode = "\"Auto\"";

} rmc;

float ax = 77.87;
float ay = 7.89;
float az = 22.87;
float gx = 477.87;
float gz = 75.87;
float gy = 453.87;

char pgtop_ch[] = "\"PTOP\":{\"ComID:\":%d,\"AntSts\":%d},";
char Temp_pgtop[sizeof(pgtop_ch) + 16];

char gpgca_ch[] = "\"GPGGA\":{\"UTTime\":%s ,\"Lati\":%s,\"N_SInd\":%s,\"Longi\":%s,\"E_WInd\":%s,\"Pos_Fix_Ind\":%d,\"S_Used\":%d,\"H_D_of_Prec\":%.02f,\"Alti\":%.02f,\"Geo_Sep\":%.02f,\"Age_Dif_Corr\":%s},";
char Temp_gpgca[sizeof(gpgca_ch) + 128];

char gprmc_ch[] =  "\"GRMC\":{\"UTTime\":%s,\"Lati\":%s,\"N_SInd\":%s,\"Longi\":%s,\"E_WInd\":%s,\"Spd_Ov_Grd\":%.02f,\"Cour_Ov_Grd\":%.02f,\"Dt\":%s,\"Mag_Var\":%.02f,\"Mag_Var_dir\":%s,\"Mode\":%s},";
char Temp_gprmc[sizeof(gprmc_ch) + 128];

char accgyro[] = "\"ag\":{\"ax\":%.02f,\"ay\":%.02f,\"az\":%.02f,\"gx\":%.02f,\"gy\":%.02f,\"gz\":%.02f}";
char Temp_accgyro[sizeof(accgyro) + 64];

char finall[] = "{%s%s%s%s}";
char msg[sizeof(pgtop_ch) + sizeof(gpgca_ch) + sizeof(gprmc_ch) +sizeof(accgyro) + sizeof(finall) + 160];


int main(){
    snprintf(Temp_pgtop, sizeof(Temp_pgtop), pgtop_ch, pgtop.commandId, pgtop.reference);

    snprintf(Temp_gpgca, sizeof(Temp_gpgca), gpgca_ch, gga.utcTime, gga.latitude, gga.northSouthIndicator, gga.longitude, gga.eastWestIndicator,
                             gga.positionFixIndicator, gga.satellitesUsed, gga.hdop, gga.altitude, gga.geoidalSeparation, gga.ageOfDiffCorr);

    snprintf(Temp_gprmc, sizeof(Temp_gprmc), gprmc_ch, rmc.utcTime, rmc.latitude, rmc.northSouthIndicator, rmc.longitude, rmc.eastWestIndicator,
                       rmc.speedOverGround, rmc.courseOverGround, rmc.date, rmc.magneticVariation, rmc.magneticVariationDirection, rmc.mode);

    snprintf(Temp_accgyro, sizeof(Temp_accgyro), accgyro, ax, ay, az, gx, gy, gz);

    snprintf(msg, sizeof(msg), finall, Temp_pgtop, Temp_gpgca, Temp_gprmc, Temp_accgyro);
    
   int arrSize = sizeof(msg)/sizeof(msg[0]);
    
    cout<<arrSize;
    cout<<endl;
    cout<<msg;
    return 0;
}

I hope this will help :slight_smile: :+1:

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