Arduino nano mqtt

Floats and MQTT can be something of an issue. I had an issue where I would send float values but not all of them were received. One thing I did that helped was to add a delay between each publish of 10ms.

What I ended up doing was to combine all my float values into a comma delimited string, rounding the float values to either 2 or 3 or 4 decimal places. On the receive side I parse out the string.

 if ( count >= 30 )
    {
      MQTTinfo.concat( String(kph, 2) );
      MQTTinfo.concat( ",");
      MQTTinfo.concat( windDirection );
      MQTTinfo.concat( ",");
      MQTTinfo.concat( String(rain, 2) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
      MQTTclient.publish( topicWSWDRF, MQTTinfo.c_str() );
      xSemaphoreGive( sema_MQTT_KeepAlive );
      count = 0;
    }

Example of rounding and sending float values as a comma delimited payload.