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?