refactor read sensors

This commit is contained in:
Joerg Lehmann 2019-07-12 21:17:11 +02:00
parent 59f6a1c0b9
commit a97afbf36c
1 changed files with 14 additions and 16 deletions

View File

@ -15,6 +15,7 @@
#include <Catena_Mx25v8035f.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Arduino_LoRaWAN.h>
#include <lmic.h>
@ -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);