In the “Esp32 MQTT – Publish an subscribe with Arduino IDE” project the float BME sensorreadings are converted to a char aray so they can be published as esp/reading topics.
For this conversion dtostrf(reading,1,2 readingString) is used.
Can you please explain the parameter 1 in this expression?
Thank you very much.
i am an absolute ESP32 – MQTT – beginner.
Hervé Tavernier Belgium
Hi Herve.
Here is the syntax of the stostrf() function
dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
where
- floatvar – float variable
- StringLengthIncDecimalPoint – this is the minimum length of the string that will be created including the point
- numVarsAfterDecimal: the number of digits after the decimal point
- charbuf: the array to store the results
I think you may be confused because the length is 1, which is smaller than the number of decimal points.
1 is the minimal length, that means that it will work even if the string is bigger than that.
For example, if you define a minimum length of 8, you’ll get 8 characters, even if your string is smaller than that. It will add white spaces to make it 8 characters long. So, if you set it to 1, you guarantee that you won’t get white spaces.
I hope this is clear.
Regards,
Sara
Hello Sara
I am very happy with your quick and clear answer to my question.
The parameter 1 now has no more secrets for me!
Thank you very much!
Hervé
P.S. Apologize for my poor English. My native language is Dutch