From a97afbf36c23385684a6ea6852d2c5c4460a6e07 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Fri, 12 Jul 2019 21:17:11 +0200 Subject: [PATCH] refactor read sensors --- mini-beieli-node.ino | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index bdc1086..5079310 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -135,6 +136,7 @@ static const byte MAX_VALUES_TO_SEND = 8; static const uint8_t LORA_DATA_VERSION = 1; static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 128; static const uint32_t PRESSURE_OFFSET = 825; +static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 10; // when weight value drops by 50g, then send data /****************************************************************************\ | @@ -467,6 +469,15 @@ void ClearLoraData(void) lora_data_first.temperature = 0; my_position = 0; + + // We initialize last_sensor_reading + last_sensor_reading.vbat = 0; + last_sensor_reading.weight1 = 0; + last_sensor_reading.weight2 = 0; + last_sensor_reading.weight = 0; + last_sensor_reading.temperature = 0; + last_sensor_reading.humidity = 0; + last_sensor_reading.pressure = 0; } void ShowLORAData(bool firstTime) @@ -577,11 +588,9 @@ void ReadSensors(bool firstTime, bool readOnly) uint8_t pressure_current; int32_t weight1_current; int32_t weight2_current; - int16_t temp_last; int16_t temp_change; int32_t weight_current32; uint16_t weight_current; - uint16_t weight_last; if (fBme) { /* warm up the BME280 by discarding a measurement */ @@ -660,17 +669,6 @@ void ReadSensors(bool firstTime, bool readOnly) weight_current = (uint16_t)weight_current32; if (not(readOnly)) { - // calculate last value - weight_last = 0; - temp_last = lora_data.temperature; - - for (int i = 0; i < my_position; i++) { - temp_last = temp_last + lora_data.temperature_change[i]; - } - - if (my_position > 0) { - weight_last = lora_data.weight[my_position - 1]; - } if (firstTime) { lora_data_first.vbat = GetVBatValue((int)(vBat * 1000.0f)); @@ -686,7 +684,7 @@ void ReadSensors(bool firstTime, bool readOnly) if (my_position == 0) { lora_data.temperature = temp_current; } else { - temp_change = temp_current - temp_last; + temp_change = temp_current - last_sensor_reading.temperature; if (temp_change > 127) { temp_change = 127; } @@ -709,9 +707,9 @@ void ReadSensors(bool firstTime, bool readOnly) // Should we send the Data? // we send data the first time the system is started, when the array is full - // or when the weight has fallen more than 100g or the first measurement is + // or when the weight has fallen more than threshold or the first measurement is // more than one hour old (which should not happen :-) ) - if (firstTime || (my_position >= MAX_VALUES_TO_SEND) || ((weight_last - weight_current) > 20) || ((millis() - timer_pos0) > 3600000)) { + if (firstTime || (my_position >= MAX_VALUES_TO_SEND) || ((last_sensor_reading.weight - weight_current) > SEND_DIFF_THRESHOLD_5GRAMS) || ((millis() - timer_pos0) > 3600000)) { lora_data.offset_last_reading = (uint8_t)((millis() - timer_pos0) / 1000 / 60); gCatena.SafePrintf("startSendingUplink()\n"); startSendingUplink(firstTime);