From 67546aa92529e124e7f4068ce8e4ed8a7cff0410 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Fri, 5 Feb 2021 20:24:09 +0100 Subject: [PATCH] not using supercaps but lipo --- mini-beieli-node-cubecell.ino | 118 +++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 22 deletions(-) diff --git a/mini-beieli-node-cubecell.ino b/mini-beieli-node-cubecell.ino index 2a664fe..51cbb0b 100644 --- a/mini-beieli-node-cubecell.ino +++ b/mini-beieli-node-cubecell.ino @@ -7,14 +7,24 @@ NAU7802 nau7802; BME280 bme280; +//#define Vext GPIO6 + +/******************************************************************************/ +/* Firmware Version */ +/******************************************************************************/ +static const int32_t fwVersion = 20210116; + /******************************************************************************/ /* LoraWAN Settings */ /******************************************************************************/ /* OTAA para*/ -uint8_t devEui[] = { 0x00, 0x72, 0x4A, 0x61, 0xB5, 0xB6, 0x20, 0xBB }; -uint8_t appEui[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0xE5, 0xAB }; -uint8_t appKey[] = { 0x55, 0x11, 0xF4, 0x55, 0x71, 0x81, 0xC0, 0xBC, 0x04, 0xA9, 0x16, 0x7F, 0x75, 0x8A, 0xEA, 0xB4 }; +//uint8_t devEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 }; +//uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +//uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01 }; +uint8_t devEui[] = { 0x00, 0x00, 0xd3, 0xa6, 0x59, 0x83, 0x3b, 0x25 }; +uint8_t appEui[] = { 0x70, 0xb3, 0xd5, 0x7e, 0xd0, 0x02, 0xe5, 0xab }; +uint8_t appKey[] = { 0x00, 0x00, 0xd3, 0xa6, 0x59, 0x83, 0x3b, 0x25, 0x00, 0x00, 0xd3, 0xa6, 0x59, 0x83, 0x3b, 0x25 }; /* ABP para*/ uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda, 0x85 }; @@ -140,7 +150,6 @@ float stddev(long samples[], int m) //calculate the stdandard deviation /******************************************************************************/ /* Global Data Structures */ /******************************************************************************/ -static const int32_t fwVersion = 20201020; // send an init package every 100 packages; static const byte INIT_PACKAGE_INTERVAL = 100; @@ -165,7 +174,7 @@ typedef struct { typedef struct { uint8_t version; // Version of Packet Format (must be increased every time format changes...) - uint8_t vsupercap; // Spannung Supercap in mV / 20 + uint8_t vbat; // Batteriespannung (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV]) int16_t temperature; // Temperatur (Startwert) in 1/10 Grad Celsius uint8_t humidity; // Luftfeuchtigkeit in Prozent uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa) @@ -179,7 +188,7 @@ typedef struct { typedef struct { uint8_t version; // Version of Packet Format (must be increased every time format changes...) int32_t fw_version; // Version of Firmware, Nummer entspricht YYYYMMDD - uint8_t vsupercap; // Spannung Supercap in mV / 20 + uint8_t vbat; // Batteriespannung (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV]) int16_t temperature; // Temperatur in 1/10 Grad Celsius uint8_t humidity; // Luftfeuchtigkeit in Prozent uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa) @@ -192,7 +201,7 @@ typedef struct { } __attribute__((packed)) LORA_data_first; typedef struct { - uint8_t vsupercap; // Spannung Supercap in mV / 20 + uint8_t vbat; // Batteriespannung (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV]) int16_t temperature; // Temperatur in 1/10 Grad Celsius uint8_t humidity; // Luftfeuchtigkeit in Prozent uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa) @@ -224,7 +233,7 @@ uint32_t gRebootMs; void ClearLoraData(bool clearLastValues) { lora_data.version = LORA_DATA_VERSION; - lora_data.vsupercap = 0; + lora_data.vbat = 0; lora_data.temperature = 0; lora_data.humidity = 0; lora_data.pressure = 0; @@ -241,7 +250,7 @@ void ClearLoraData(bool clearLastValues) lora_data_first.version = LORA_DATA_VERSION_FIRST_PACKAGE; lora_data_first.fw_version = fwVersion; - lora_data_first.vsupercap = 0; + lora_data_first.vbat = 0; lora_data_first.weight_a = 0; lora_data_first.weight_b = 0; lora_data_first.cal_a_0 = config_data.cal_a_0; @@ -256,7 +265,7 @@ void ClearLoraData(bool clearLastValues) // We initialize last_sensor_reading if (clearLastValues) { - last_sensor_reading.vsupercap = 0; + last_sensor_reading.vbat = 0; last_sensor_reading.weight_a = 0; last_sensor_reading.weight_b = 0; last_sensor_reading.weight = 0; @@ -275,7 +284,7 @@ void ShowLORAData(bool firstTime) Serial.printf("{\n"); Serial.printf(" \"version\": \"%d\",\n", lora_data_first.version); Serial.printf(" \"fw_version\": \"%d\",\n", lora_data_first.fw_version); - Serial.printf(" \"vsupercap:\": \"%u\",\n", lora_data_first.vsupercap); + Serial.printf(" \"vbat:\": \"%u\",\n", lora_data_first.vbat); Serial.printf(" \"humidity\": \"%u\",\n", lora_data_first.humidity); Serial.printf(" \"pressure\": \"%u\",\n", lora_data_first.pressure); Serial.printf(" \"weight_a\": \"%ld\",\n", lora_data_first.weight_a); @@ -291,7 +300,7 @@ void ShowLORAData(bool firstTime) Serial.printf("{\n"); Serial.printf(" \"version\": \"%d\",\n", lora_data.version); - Serial.printf(" \"vsupercap\": \"%u\",\n", lora_data.vsupercap); + Serial.printf(" \"vbat\": \"%u\",\n", lora_data.vbat); Serial.printf(" \"temperature\": \"%u\",\n", lora_data.temperature); Serial.printf(" \"humidity\": \"%u\",\n", lora_data.humidity); Serial.printf(" \"pressure\": \"%u\",\n", lora_data.pressure); @@ -534,12 +543,58 @@ void PowerupScale() } } +void CalculateWeight() +{ + int32_t weight_current32; + int32_t weight_current32_a; + int32_t weight_current32_b; + + bool plausible; + bool plausible_a; + bool plausible_b; + + // Gewicht berechnen + weight_current32_a = (int32_t)((sensor_data.weight_a - config_data.cal_a_0) / config_data.cal_a_factor); + weight_current32_b = (int32_t)((sensor_data.weight_b - config_data.cal_b_0) / config_data.cal_b_factor); + weight_current32 = (int32_t)((weight_current32_a + weight_current32_b) / 5.0); + + // we check if weights are plausible + plausible_a = (weight_current32_a > -10000) && (weight_current32_a < 150000); + plausible_b = (weight_current32_b > -10000) && (weight_current32_b < 150000); + plausible = (plausible_a && plausible_b); + + if (weight_current32 < 0) { + if (plausible) { + 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 (!plausible) { + weight_current32 = NOT_PLAUSIBLE_16; + //if (!plausible_a) { + // sensor_data.weight_a = NOT_PLAUSIBLE_32; + //} + //if (!plausible_b) { + // sensor_data.weight_b = NOT_PLAUSIBLE_32; + //} + } + sensor_data.weight = (uint16_t)weight_current32; +} + void ReadScales() { PowerupScale(); sensor_data.weight_a = ReadScale('A'); sensor_data.weight_b = ReadScale('B'); + if (config_data.debug_level > 0) { + Serial.printf("Reading A, B: %d, %d\n", sensor_data.weight_a, sensor_data.weight_b); + + } PowerdownScale(); + CalculateWeight(); } /******************************************************************************/ @@ -612,19 +667,19 @@ bool checkUserAt(char *cmd, char *content) Serial.printf(" \"temperature\": \"%d\",\n", sensor_data.temperature); Serial.printf(" \"humidity\": \"%d\",\n", sensor_data.humidity); Serial.printf(" \"pressure\": \"%d\",\n", sensor_data.pressure); - Serial.printf(" \"vsupercap\": \"%d\",\n", sensor_data.vsupercap); + Serial.printf(" \"vbat\": \"%d\",\n", sensor_data.vbat); Serial.printf("}\n"); } return true; } - if (strcmp(cmd, "SUPERCAP") == 0) + if (strcmp(cmd, "VBAT") == 0) { if (content[0] == '?') { - uint16_t BatteryVoltage = getBatteryVoltage(); - Serial.print("+SUPERCAP="); - Serial.print(BatteryVoltage); + uint16_t VBatVoltage = getBatteryVoltage(); + Serial.print("+VBAT="); + Serial.print(VBatVoltage); Serial.println(); } return true; @@ -729,7 +784,7 @@ void AddSensorDataToLoraData() iteration++; next_package_is_init_package = ((iteration <= INIT_PACKETS) || ((package_counter % INIT_PACKAGE_INTERVAL) == 0)); if (next_package_is_init_package) { - lora_data_first.vsupercap = sensor_data.vsupercap; + lora_data_first.vbat = sensor_data.vbat; lora_data_first.weight_a = sensor_data.weight_a; lora_data_first.weight_b = sensor_data.weight_b; lora_data_first.temperature = sensor_data.temperature; @@ -739,7 +794,7 @@ void AddSensorDataToLoraData() ShowLORAData(true); } } else { - lora_data.vsupercap = sensor_data.vsupercap; + lora_data.vbat = sensor_data.vbat; if (my_position == 0) { lora_data.temperature = sensor_data.temperature; lora_data.weight_first = sensor_data.weight; @@ -796,6 +851,22 @@ bool TooBigWeightChange() return big_difference; } +uint8_t GetVBatValue(int millivolts) +{ + uint8_t res; + if (millivolts <= 2510) { + res = 0; + + } else if (millivolts >= 4295) { + res = 255; + + } else { + res = (millivolts - 2510) / 7; + } + + return res; +} + // returns true if data should be sent; read_only: when true, do not use as lora_data bool ReadSensors(bool read_only) { @@ -811,7 +882,7 @@ bool ReadSensors(bool read_only) disableVext(); uint16_t voltage = getBatteryVoltage(); - sensor_data.vsupercap = (uint8_t)(voltage / 20); + sensor_data.vbat = GetVBatValue(voltage); if (config_data.debug_level > 0) { Serial.printf("Read ADC, %d Millivolts...\n", voltage); } @@ -857,9 +928,12 @@ void loop() { case DEVICE_STATE_INIT: { -#if(AT_SUPPORT) - getDevParam(); +#if(LORAWAN_DEVEUI_AUTO) + LoRaWAN.generateDeveuiByChipID(); #endif +#if(AT_SUPPORT) + getDevParam(); +#endif printDevParam(); LoRaWAN.init(loraWanClass, loraWanRegion); deviceState = DEVICE_STATE_JOIN;