diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index 6e1cf42..7264340 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -527,12 +527,18 @@ void DoDeepSleep(uint32_t sleep_time) } } -void ReadSensors(SENSOR_data &sensor_data) { +// Returns true if measurements are plausible, otherwise false +bool ReadSensors(SENSOR_data &sensor_data) { + bool plausible; SENSOR_data res; int32_t weight_current32; + int32_t weight_current32_a; + int32_t weight_current32_b; long w1_0_real; long w2_0_real; + plausible = true; + // vBat int vbat_mv = (int)(gCatena.ReadVbat() * 1000.0f); res.vbat = GetVBatValue(vbat_mv); @@ -582,24 +588,29 @@ void ReadSensors(SENSOR_data &sensor_data) { PowerdownScale(); // Gewicht berechnen - weight_current32 = (int32_t)((((res.weight1 - w1_0_real) / config_data.cal_w1_factor) + ((res.weight2 - w2_0_real) / config_data.cal_w2_factor)) / 5.0); + weight_current32_a = (int32_t)((res.weight1 - w1_0_real) / config_data.cal_w1_factor); + weight_current32_b = (int32_t)((res.weight2 - w2_0_real) / config_data.cal_w2_factor); + weight_current32 = (int32_t)((weight_current32_a + weight_current32_b) / 5.0); + + // we check if weights are plausible + plausible = (weight_current32_a > -10000) && (weight_current32_b > -10000) && (weight_current32_a < -150000) && (weight_current32_b < -150000); if (weight_current32 < 0) { - weight_current32 = 0; - } else if (weight_current32 > UINT16_MAX) { - //weight_current32 = UINT16_MAX; - // we set the weight to 0, as such high values are not realistic and probably a sign for bad calibration... - weight_current32 = 0; - } - if (config_data.cal_w1_0 == NOT_ATTACHED || config_data.cal_w2_0 == NOT_ATTACHED) { - // when at least one load cell is disabled, we multiply the measured weight by 2 - weight_current32 = weight_current32 * 2; - } - res.weight = (uint16_t)weight_current32; + weight_current32 = 0; +} else if (weight_current32 > UINT16_MAX) { + //weight_current32 = UINT16_MAX; + // we set the weight to 0, as such high values are not realistic and probably a sign for bad calibration... + weight_current32 = 0; +} +if (config_data.cal_w1_0 == NOT_ATTACHED || config_data.cal_w2_0 == NOT_ATTACHED) { + // when at least one load cell is disabled, we multiply the measured weight by 2 + weight_current32 = weight_current32 * 2; +} +res.weight = (uint16_t)weight_current32; - if (fBme) { - /* warm up the BME280 by discarding a measurement */ - (void)gBME280.readTemperature(); +if (fBme) { + /* warm up the BME280 by discarding a measurement */ + (void)gBME280.readTemperature(); Adafruit_BME280::Measurements m = gBME280.readTemperaturePressureHumidity(); // temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C @@ -621,6 +632,7 @@ void ReadSensors(SENSOR_data &sensor_data) { } sensor_data = res; + return plausible; } void StartNewIteration() { @@ -632,7 +644,18 @@ void StartNewIteration() { iteration++; SENSOR_data current_sensor_reading; - ReadSensors(current_sensor_reading); + if (!ReadSensors(current_sensor_reading)) { + // we try a second time if Readings do not seem plausible + if (config_data.debug_level > 0) { + gCatena.SafePrintf("Readings do not seem plausible, try a second time\n"); + } + delay(500); + if (!ReadSensors(current_sensor_reading)) { + if (config_data.debug_level > 0) { + gCatena.SafePrintf("Readings do not seem plausible for a second time, we give up!\n"); + } + } + } int16_t temp_change; // vBus @@ -733,7 +756,7 @@ void StartNewIteration() { last_sensor_reading = current_sensor_reading; } else { // we only copy the last weight - last_sensor_reading.weight = current_sensor_reading.weight; + last_sensor_reading.weight = current_sensor_reading.weight; } uint32_t sleep_time_sec; diff --git a/mini_beieli_node.h b/mini_beieli_node.h index 8d09c0b..ce2dc98 100644 --- a/mini_beieli_node.h +++ b/mini_beieli_node.h @@ -56,7 +56,7 @@ enum { | \****************************************************************************/ -static const int32_t fwVersion = 20200630; +static const int32_t fwVersion = 20200701; static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages; static const byte MAX_VALUES_TO_SEND = 8;