The code below updates an IoT shadow with a fluid volume level. The code is working fine as long as the volume level is 1000 or higher. If it is a three digit value, the float_buf is storing the number with a trailing decimal point (ex. 879. ). This is invalid JSON structure and the IoT update is then failing.
My question is how can I trim the trailing decimal point so that if the value in the character array is four places it is stored properly (ex. 4234) and if it is 3 places it also is stored properly (ex. 879)?
char JSON_buf[100];
char float_buf[5];
float volume = resistanceToVolume(resistance, ZERO_VOLUME_RESISTANCE, CALIBRATION_RESISTANCE, CALIBRATION_VOLUME);
Serial.print("Calculated volume: ");
Serial.println(volume, 5);
if (volume < 1000){
level = "Low";
Serial.print("Level: ");
Serial.println(level);
}
dtostrf(volume, 4, 1, float_buf);
float_buf[4] = '\0';
sprintf(JSON_buf, "{\"state\":{\"reported\":{\"Volume\":%s}}}", float_buf);
print_log("shadow update", myClient.shadow_update(AWS_IOT_MY_THING_NAME, JSON_buf, strlen(JSON_buf), NULL, 5));
Serial.println(JSON_buf);
Here is an example of the invalid JSON caused by the character array storing three digits and a decimal for float_buf:
Resistance: 1740.72 ohms
Calculated volume: 879.70220
Level: Low
[ERR] command: shadow update code: -31
{"state":{"reported":{"Volume":879.}}}
This is an example of valid JSON:
Resistance: 1677.81 ohms
Calculated volume: 1062.18237
[LOG] command: shadow update completed.
{"state":{"reported":{"Volume":1062}}}