Hello together...
In the following sketch i am reading two 4- 20 mA industrial sensor. One for range, one for force.
I want to send the values over wifi to a App with a Json file using the AduinoJson Library. The problem that i have is, how i can write to the Json the floating values only with 1 or 2 decimal places. Right now it writes until 9 decimal points.
I tried as follow to reduce the decimal points but it does not work.
range = (int)(unroude_range * 100 + 0.5) / 100.0;
force = (int)(unroude_force * 100 + 0.5) / 100.0;
Follows the complete funktion where i am reading the sensors writing and sending the Json.
Thanks in advance for some help.
void Read_Scale() {
String jsonString = "";
StaticJsonDocument<1000> doc;
static int scale_counter = 0;
if (millis() - scale_previousMillis >= scale_interval) { // read analog value every 500ms
scale_previousMillis = millis(); // reset the timer
rawADCValue_range = 0;
rawADCValue_force = 0;
for (int i = 0; i < 100; ) {
if (millis() - range_t >= 1) {
rawADCValue_range += analogRead(ADC_Range_Pin);
rawADCValue_force += analogRead(ADC_Force_Pin);
i++;
range_t = millis();
}
}
ADCVoltage_range = (float)(rawADCValue_range / 100.0) * (3.3 / 4095.0);
Current_range = mapfloat(ADCVoltage_range, 0.8, 3.3, 4, 20);
unroude_range = mapfloat(Current_range, 4, 20, 0, 50);
range = (int)(unroude_range * 100 + 0.5) / 100.0;
ADCVoltage_force = (float)(rawADCValue_force / 100.0) * (3.3 / 4095.0);
Current_force = mapfloat(ADCVoltage_force, 0.8, 3.3, 4, 20);
unroude_force = mapfloat(Current_force, 4, 20, 0, 20);
force = (int)(unroude_force * 100 + 0.5) / 100.0;
buffer_range[scale_counter] = range ; // store the range value in the buffer
buffer_force[scale_counter] = force ; // store the force value in the buffer
scale_counter ++;
doc["count_json"] = daten.zyklus;
JsonArray rangeValues = doc.createNestedArray("Range");
JsonArray forceValues = doc.createNestedArray("Force");
if (scale_counter >= NUM_SAMPLES)
{
for (int i = 0; i < NUM_SAMPLES; i++){
rangeValues.add(buffer_range[i]);
forceValues.add(buffer_force[i]);
}
scale_counter = 0;
serializeJson(doc, jsonString);
webSocket.broadcastTXT(jsonString);
Serial.println(jsonString);
}
}
}