Version 20201020

This commit is contained in:
Joerg Lehmann 2020-10-20 17:23:08 +02:00
parent d6b0b23e8e
commit 86539f43fc
2 changed files with 178 additions and 62 deletions

View File

@ -22,8 +22,8 @@ Das sind die verwendeten Libraries [1]:
| URL | Commit | Commit Date |
| --- | ----- | ----------- |
| https://github.com/HelTecAutomation/ASR650x-Arduino.git | 96e9b9e | Fri, 25 Sep 2020 14:53:02 +0800 |
| https://github.com/HelTecAutomation/ASR650x-Arduino.git | f468b40 | Sat, 10 Oct 2020 16:45:30 +0800 |
| https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git | 688f255 | Fri, 3 Jan 2020 12:35:22 -0700 |
| https://github.com/mcci-catena/Adafruit_BME280_Library.git | 3dafbe1 | Wed, 13 Dec 2017 13:56:30 -0500 |
[1]: Commit String: git log --pretty=format:'%h | %cD |' -n 1

View File

@ -1,6 +1,6 @@
#include "LoRaWan_APP.h"
#include "Arduino.h"
#include "Seeed_BME280.h"
#include "SparkFunBME280.h"
#include <Wire.h>
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
@ -59,7 +59,7 @@ void enableVext()
{
pinMode(Vext, OUTPUT);
digitalWrite(Vext, LOW);
delay(500);
delay(50);
Wire.begin();
}
@ -140,46 +140,48 @@ float stddev(long samples[], int m) //calculate the stdandard deviation
/******************************************************************************/
/* Global Data Structures */
/******************************************************************************/
static const int32_t fwVersion = 20200925;
static const int32_t fwVersion = 20201020;
// send an init package every 100 packages;
static const byte INIT_PACKAGE_INTERVAL = 100;
static const byte MAX_VALUES_TO_SEND = 8;
// static const byte MAX_VALUES_TO_SEND = 1; // Testing
static const uint8_t LORA_DATA_VERSION = 1;
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 128;
static const uint8_t LORA_DATA_VERSION = 2;
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 130;
static const uint32_t PRESSURE_OFFSET = 825;
// when weight changes by 100g, then send data
static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 20;
static const long NOT_PLAUSIBLE_16 = 65535;
static const long NOT_PLAUSIBLE_32 = 2147483647;
static const byte INIT_PACKETS = 5;
static const byte INIT_PACKETS = 2;
typedef struct {
long cal_a_0; // 4 Bytes, Wert Waegezelle 1 ohne Gewicht, LONG_MIN when not connected
long cal_b_0; // 4 Bytes, Wert Waegezelle 2 ohne Gewicht, LONG_MIN when not connected
float cal_a_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
float cal_b_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
byte debug_level; // 0 => no debugging, no led, 1 => infos, no led, 2 => infos, 3 => error, 4 => highest level
byte debug_level; // 0 => no debugging, no led, 1 => infos, no led, 2 => infos, 3 => error, 4 => highest level
} __attribute__((packed)) CONFIG_data;
typedef struct {
uint8_t version; // Version of Packet Format (must be increased every time format changes...)
uint8_t vsupercap; // Spannung Supercap (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
uint8_t humidity[MAX_VALUES_TO_SEND]; // Luftfeuchtigkeit in Prozent
uint8_t vsupercap; // Spannung Supercap in mV / 20
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)
int8_t temperature_change[MAX_VALUES_TO_SEND - 1]; // Unterschied Temperatur seit letztem Messwert in 1/10 Grad Celsius
uint8_t pressure[MAX_VALUES_TO_SEND]; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
uint16_t weight[MAX_VALUES_TO_SEND]; // Waegezelle Gesamtgewicht, in 5g
uint16_t weight_first; // Waegezelle Gesamtgewicht, in 5g, Erste Messung
int8_t weight_change[MAX_VALUES_TO_SEND - 2]; // Waegezelle Gesamtgewicht, in 5g
uint16_t weight_last; // Waegezelle Gesamtgewicht, in 5g, Letzte Messung
uint8_t offset_last_reading; // Zeitunterschied letzte zu erste Messung (in Minuten)
} __attribute__((packed)) LORA_data;
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 (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
uint8_t humidity; // Luftfeuchtigkeit in Prozent
uint8_t vsupercap; // Spannung Supercap in mV / 20
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)
int32_t weight_a; // Waegezelle A, Raw Value
int32_t weight_b; // Waegezelle B, Raw Value
@ -190,9 +192,9 @@ typedef struct {
} __attribute__((packed)) LORA_data_first;
typedef struct {
uint8_t vsupercap; // Spannung Supercap (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
uint8_t humidity; // Luftfeuchtigkeit in Prozent
uint8_t vsupercap; // Spannung Supercap in mV / 20
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)
int32_t weight_a; // Waegezelle A, Raw Value
int32_t weight_b; // Waegezelle B, Raw Value
@ -224,20 +226,22 @@ void ClearLoraData(bool clearLastValues)
lora_data.version = LORA_DATA_VERSION;
lora_data.vsupercap = 0;
lora_data.temperature = 0;
lora_data.humidity = 0;
lora_data.pressure = 0;
lora_data.weight_first = 0;
lora_data.weight_last = 0;
for (int i = 0; i < MAX_VALUES_TO_SEND; i++) {
lora_data.humidity[i] = 0;
lora_data.pressure[i] = 0;
lora_data.weight[i] = 0;
if (i < (MAX_VALUES_TO_SEND - 2)) {
lora_data.weight_change[i] = 0;
}
if (i < (MAX_VALUES_TO_SEND - 1)) {
lora_data.temperature_change[i] = 0;
lora_data.temperature_change[i] = 127;
}
}
lora_data_first.version = LORA_DATA_VERSION_FIRST_PACKAGE;
lora_data_first.fw_version = fwVersion;
lora_data_first.vsupercap = 0;
lora_data_first.humidity = 0;
lora_data_first.pressure = 0;
lora_data_first.weight_a = 0;
lora_data_first.weight_b = 0;
lora_data_first.cal_a_0 = config_data.cal_a_0;
@ -245,6 +249,8 @@ void ClearLoraData(bool clearLastValues)
lora_data_first.cal_a_factor = config_data.cal_a_factor;
lora_data_first.cal_b_factor = config_data.cal_b_factor;
lora_data_first.temperature = 0;
lora_data_first.humidity = 0;
lora_data_first.pressure = 0;
my_position = 0;
@ -260,6 +266,58 @@ void ClearLoraData(bool clearLastValues)
}
}
void ShowLORAData(bool firstTime)
{
Serial.printf("ShowLORAData\n");
if (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(" \"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);
Serial.printf(" \"weight_b\": \"%ld\",\n", lora_data_first.weight_b);
Serial.printf(" \"cal_a_0\": \"%ld\",\n", lora_data_first.cal_a_0);
Serial.printf(" \"cal_b_0\": \"%ld\",\n", lora_data_first.cal_b_0);
Serial.printf(" \"cal_a_factor\": \"%d.%03d\",\n", (int)lora_data_first.cal_a_factor, (int)abs(lora_data_first.cal_a_factor * 1000) % 1000);
Serial.printf(" \"cal_b_factor\": \"%d.%03d\",\n", (int)lora_data_first.cal_b_factor, (int)abs(lora_data_first.cal_b_factor * 1000) % 1000);
Serial.printf(" \"temperature\": \"%u\",\n", lora_data_first.temperature);
Serial.printf("}\n");
} else {
Serial.printf("{\n");
Serial.printf(" \"version\": \"%d\",\n", lora_data.version);
Serial.printf(" \"vsupercap\": \"%u\",\n", lora_data.vsupercap);
Serial.printf(" \"temperature\": \"%u\",\n", lora_data.temperature);
Serial.printf(" \"humidity\": \"%u\",\n", lora_data.humidity);
Serial.printf(" \"pressure\": \"%u\",\n", lora_data.pressure);
Serial.printf(" \"weight_first\": \"%u\",\n", lora_data.weight_first);
Serial.printf(" \"weight\": [");
for (int i = 0; i < MAX_VALUES_TO_SEND - 2; i++) {
Serial.printf("%ld", lora_data.weight_change[i]);
if (i < (MAX_VALUES_TO_SEND - 3)) {
Serial.printf(",");
}
}
Serial.printf("],\n");
Serial.printf(" \"weight_last\": \"%u\",\n", lora_data.weight_last);
Serial.printf(" \"temperature_change\": [");
for (int i = 0; i < MAX_VALUES_TO_SEND - 1; i++) {
Serial.printf("%d", lora_data.temperature_change[i]);
if (i < (MAX_VALUES_TO_SEND - 2)) {
Serial.printf(",");
}
}
Serial.printf("]\n");
Serial.printf("}\n");
}
}
/******************************************************************************/
/* Read/Write Configuration */
/******************************************************************************/
@ -286,30 +344,43 @@ void WriteConfigDataToFlash()
void SetupBME280()
{
if (!bme280.init()) {
Serial.println("Error: cannot initialize BME280");
}
Wire.begin();
Wire.setClock(400000); //Increase to fast I2C speed!
bme280.beginI2C();
bme280.setMode(MODE_SLEEP); //Sleep for now
}
void ReadBME280()
void ReadBME280(bool read_humidity_and_pressure)
{
//get and print temperatures
/* warm up the BME280 by discarding a measurement */
bme280.getTemperature();
// get and print temperatures
bme280.setMode(MODE_FORCED); //Wake up sensor and take reading
long startTime = millis();
while (bme280.isMeasuring() == false) ; //Wait for sensor to start measurment
while (bme280.isMeasuring() == true) ; //Hang out while sensor completes the reading
long endTime = millis();
// Sensor is now back asleep but we get get the data
sensor_data.temperature = (int16_t)(bme280.readTempC() * 10);
if (read_humidity_and_pressure) {
sensor_data.humidity = (uint8_t)bme280.readFloatHumidity();
sensor_data.pressure = (uint8_t)((bme280.readFloatPressure() / 100) - PRESSURE_OFFSET);
}
if (config_data.debug_level > 0) {
Serial.printf("Temperature: %d\n", sensor_data.temperature);
Serial.printf("Humidity: %d\n", sensor_data.humidity);
Serial.printf("Pressure: %d\n", sensor_data.pressure);
}
sensor_data.temperature = (int16_t)(bme280.getTemperature() * 10);
sensor_data.humidity = (uint8_t)bme280.getHumidity();
sensor_data.pressure = (uint8_t)((bme280.getPressure() / 100) - PRESSURE_OFFSET);
// if (config_data.debug_level > 0) {
Serial.printf("Temperature: %d, Humidity: %d, Pressure: %d\n", sensor_data.temperature, sensor_data.humidity, sensor_data.pressure);
// }
}
/******************************************************************************/
/* Functions to interface with Load Cells */
/******************************************************************************/
#define SAMPLES 5
#define SAMPLES 3
//byte interruptPin = A0;
@ -324,8 +395,6 @@ bool InitializeScales()
result &= nau7802.setSampleRate(NAU7802_SPS_40); //Set samples per second to 40
result &= nau7802.setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
result &= nau7802.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
return result;
}
@ -341,7 +410,9 @@ bool SetupScales()
Serial.printf("Scale not detected. Please check wiring. Freezing...\n");
return false;
}
Serial.printf("Scale detected!\n");
if (config_data.debug_level > 0) {
Serial.printf("Scale detected!\n");
}
bool result = InitializeScales();
if (config_data.debug_level > 0) {
@ -397,7 +468,6 @@ long ReadScale(char channel)
Serial.printf("Timeout while reading scale...\n");
}
}
delay(50);
}
long reading;
if (nau7802.available()) {
@ -407,7 +477,6 @@ long ReadScale(char channel)
if (config_data.debug_level > 0) {
Serial.printf("Reading: %d\n", reading);
}
delay(50);
}
unsigned long duration = millis() - startTime;
@ -483,9 +552,15 @@ static void prepareTxFrame( uint8_t port )
if (next_package_is_init_package) {
appDataSize = sizeof(lora_data_first);
memcpy(&appData, &lora_data_first, sizeof(lora_data_first));
if (config_data.debug_level > 0) {
Serial.printf("Prepare TX Frame Init Packet, Size: %d\n", sizeof(lora_data_first));
}
} else {
appDataSize = sizeof(lora_data);
memcpy(&appData, &lora_data, sizeof(lora_data));
if (config_data.debug_level > 0) {
Serial.printf("Prepare TX Frame Normal Packet, Size: %d\n", sizeof(lora_data));
}
}
}
@ -640,19 +715,19 @@ void setup_platform(void)
Serial.printf("cal_a_factor: %d.%03d\n", (int)config_data.cal_a_factor, (int)abs(config_data.cal_a_factor * 1000) % 1000);
Serial.printf("cal_b_factor: %d.%03d\n", (int)config_data.cal_b_factor, (int)abs(config_data.cal_b_factor * 1000) % 1000);
Serial.printf("debug_level: %d\n", (int)config_data.debug_level);
Serial.printf("\n");
Serial.printf("-------------------------------------------------------------------------------\n");
Serial.printf("mini-beieli.ch - BeieliScale Version %d.\n", fwVersion);
}
Serial.printf("\n");
Serial.printf("-------------------------------------------------------------------------------\n");
Serial.printf("mini-beieli.ch - BeieliScale Version %d.\n", fwVersion);
}
void AddSensorDataToLoraData()
{
int16_t temp_change;
int16_t weight_change;
iteration++;
next_package_is_init_package = ((iteration < INIT_PACKETS) || ((package_counter % INIT_PACKAGE_INTERVAL) == 0));
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.weight_a = sensor_data.weight_a;
@ -660,58 +735,98 @@ void AddSensorDataToLoraData()
lora_data_first.temperature = sensor_data.temperature;
lora_data_first.humidity = sensor_data.humidity;
lora_data_first.pressure = sensor_data.pressure;
if (config_data.debug_level > 0) {
ShowLORAData(true);
}
} else {
lora_data.vsupercap = sensor_data.vsupercap;
lora_data.weight[my_position] = sensor_data.weight;
if (my_position == 0) {
lora_data.temperature = sensor_data.temperature;
lora_data.weight_first = sensor_data.weight;
lora_data.humidity = sensor_data.humidity;
lora_data.pressure = sensor_data.pressure;
} else {
temp_change = sensor_data.temperature - last_sensor_reading.temperature;
if (temp_change > 127) {
temp_change = 127;
if (temp_change > 126) {
temp_change = 126;
}
if (temp_change < -128) {
temp_change = -128;
}
lora_data.temperature_change[my_position - 1] = (uint8_t)temp_change;
weight_change = sensor_data.weight - last_sensor_reading.weight;
if (weight_change > 127) {
weight_change = 127;
}
if (weight_change < -128) {
weight_change = -128;
}
if (my_position == MAX_VALUES_TO_SEND - 1) {
lora_data.weight_last = sensor_data.weight;
} else {
lora_data.weight_change[my_position - 1] = (uint8_t)weight_change;
}
}
lora_data.humidity[my_position] = sensor_data.humidity;
lora_data.pressure[my_position] = sensor_data.pressure;
lora_data.offset_last_reading = (uint8_t)((millis() - timer_pos0) / 1000 / 60);
if (config_data.debug_level > 0) {
ShowLORAData(false);
}
}
if (my_position == 0) {
timer_pos0 = millis();
}
my_position++;
last_sensor_reading = sensor_data;
}
bool TooBigWeightChange()
{
bool big_difference = (abs(last_sensor_reading.weight - sensor_data.weight) > SEND_DIFF_THRESHOLD_5GRAMS);
if (big_difference) {
lora_data.weight_last = sensor_data.weight;
}
if (config_data.debug_level > 0) {
Serial.printf("TooBigWeightChange (my_position: %d): %d...\n", my_position, big_difference);
}
return big_difference;
}
// returns true if data should be sent; read_only: when true, do not use as lora_data
bool ReadSensors(bool read_only)
{
bool send_data;
//config_data.debug_level=2;
enableVext();
SetupBME280();
SetupScales();
ReadBME280();
if (config_data.debug_level > 0) {
Serial.printf("Read BME280, my_position: %d...\n", my_position);
}
ReadBME280(my_position == 0);
ReadScales();
disableVext();
uint16_t voltage = getBatteryVoltage();
sensor_data.vsupercap = voltage;
sensor_data.vsupercap = (uint8_t)(voltage / 20);
if (config_data.debug_level > 0) {
Serial.printf("Read ADC, %d Millivolts...\n", voltage);
}
if (!read_only) {
AddSensorDataToLoraData();
send_data = (iteration % MAX_VALUES_TO_SEND == 0) || (iteration <= INIT_PACKETS) || (iteration % INIT_PACKAGE_INTERVAL == 0);
send_data = (TooBigWeightChange()) || (my_position >= MAX_VALUES_TO_SEND) || (iteration <= INIT_PACKETS) || (iteration % INIT_PACKAGE_INTERVAL == 0);
if (send_data) {
Serial.printf("Iteration: %d, we send the data...\n", iteration);
} else {
Serial.printf("Iteration: %d, only measurement, not sending...\n", iteration);
if (config_data.debug_level > 0) {
if (send_data) {
Serial.printf("Iteration: %d, we send the data...\n", iteration);
} else {
Serial.printf("Iteration: %d, only measurement, not sending...\n", iteration);
}
}
} else {
send_data = false;
}
@ -760,6 +875,7 @@ void loop()
if (ReadSensors(false)) {
prepareTxFrame(appPort);
LoRaWAN.send();
package_counter++;
ClearLoraData(false);
}
deviceState = DEVICE_STATE_CYCLE;