How place 15 Float values in one MQTT payload Pub sub Client

I need to send 3 energy sensor readings with one single payload via MQTT.

So what I have done so far.

The 15 float values is in an array ph[0]- ph[14]

I then convert the values to String with dtostrf

   char  v1[10], c1[10], p1[10], e1[10], pf1[5];
   char  v2[10], c2[10], p2[10], e2[10], pf2[5];
   char  v3[10], c3[10], p3[10], e3[10], pf3[5];
   
// Phase 1
   dtostrf(ph[0],2,2,v1);
   dtostrf(ph[1],2,2,c1);
   dtostrf(ph[2],2,2,p1);
   dtostrf(ph[3],2,2,e1);
   dtostrf(ph[4],2,2,pf1);
// Phase 2
   dtostrf(ph[5],2,2,v2);
   dtostrf(ph[6],2,2,c2);
   dtostrf(ph[7],2,2,p2);
   dtostrf(ph[8],2,2,e2);
   dtostrf(ph[9],2,2,pf2);
// Phase 3
   dtostrf(ph[10],2,2,v3);
   dtostrf(ph[11],2,2,c3);
   dtostrf(ph[12],2,2,p3);
   dtostrf(ph[13],2,2,e3);
   dtostrf(ph[14],2,2,pf3);

Then create a comma separated payload with sprintf

      char data[160];
      sprintf(data, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", v1, c1, p1, e1, pf1, v2, c2, p2, e2, pf2, v3, c3, p3, e3, pf3);
      mqtt.publish("energy/data", data);

It works for a couple of times and then the program crashes. Is there a better way of sending the info via MQTT , I have a limited amount of band width so I need to send the data with one payload.

could you be overrunning you data[] array?
try printing the value returned by sprintf() which is the number of characters written to the array (excluding null)
also consider using snprintf() when the array length is specified

What does crashes mean?

Limit the size of the float to 3 or 4 or 6 decimal points.

why do you convert it to String?
Send it as array of float.

I will need to have a look at the array of float.

I am also playing with the idea to convert the values to INT by multiplying with 100 (only need 2x decimals).
Should that also be a solution?

What happened when you tried?

So far the program seems to be running stable. But will run it for the day and see if it holds up.

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