Better way construct CSV file with Float values

I am reading values from a BMS and sending it to MQTT broker as csv file.

in constructing the payload I find my self repeating the the same commands.
Here is what the working function looks like:

void SendData ( ){

   char  v1[10], v2[10], v3[10],  v4[10],  v5[10];
   char  v6[10], v8[10], v10[10], v12[10], v14[10];
   char  v7[10], v9[10], v11[10], v13[10], v15[10] ,v16[10];
   

        dtostrf(cells[1],3,3,v1);
        dtostrf(cells[2],3,3,v2);
        dtostrf(cells[3],3,3,v3);
        dtostrf(cells[4],3,3,v4);
        dtostrf(cells[5],3,3,v5);
        dtostrf(cells[6],3,3,v6);
        dtostrf(cells[7],3,3,v7);
        dtostrf(cells[8],3,3,v8);
        dtostrf(cells[9],3,3,v9);
        dtostrf(cells[10],3,3,v10);
        dtostrf(cells[11],3,3,v11);
        dtostrf(cells[12],3,3,v12);
        dtostrf(cells[13],3,3,v13);
        dtostrf(cells[14],3,3,v14);
        dtostrf(cells[15],3,3,v15);
        dtostrf(cells[16],3,3,v16);

   // displayData ();
      char data[320];
      sprintf(data, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16);
      client.publish("Volts", data);
      Serial.println("Run Event: Done"); 
 
}

What I attempted was to run char v* data trought a loop to assign the values to each char like this

 for (int i = 1; i <= 16; i++)
  {
char dat[5];
sprintf(dat, "%s%i","v",i);
  dtostrf(cells[i],3,3,dat);
  }

But the data does not show correctly , is there a better way to handle the data or is that as good as I will get it?

The dtostrf() function returns the pointer to the converted string s.

Bummer. It would have been handy if it returned the tail instead of the head. In any case, give this a try. It's not been tested.


bool append_float(char* buffer, size_t buffer_size, size_t& index, float value)
{
  // It is assumed the maximum bytes stored by dtostrf will be 12.
  if (index >= buffer_size - 12) {
    return false;
  }
  else {
    char* start = &buffer[index];
    dtostrf(value, 3, 3, start);
    char* stop = strchr(start, 0);
    index += (stop - start);
    return true;
  }
}

void setup() {
  char data[320];
  size_t rover = 0;
  float cells[17];

  for (unsigned i=1; i <= 16; ++i) {
    if (!append_float(data, sizeof(data), rover, cells[i])) {
      // Yikes! The buffer is too small!
    }
  }
  // client.publish("Volts", data);
  // Serial.println("Run Event: Done"); 
}

void loop() {}

I will need to go through the code to understand what it is doing but just testing the code it seems like it does not work.

1.9750.0000.0000.0000.000-0.0001.9752.3921.9750.0002.3921.9750.0001.9920.0000.000

What I am expecting is

3.269,3.270,3.268,3.267,3.268,3.269,3.269,3.269,3.270,3.270,3.268,3.269,3.270,3.268,3.268,3.270

Oh yeah. Forgot the comma. A small adjustment after determining stop and using 13 instead of 12 should get that fixed.

The other way around would be better, If index is greater than zero first append a comma then write the value.

NodeRED accepts a JSON'ish format (slightly modified CSV) and easily implemented. Does MQTT not accept JSON?

Serial output directly into NodeRED - Arduino for STM32 (stm32duino.com)

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