998 lines
32 KiB
C++
998 lines
32 KiB
C++
#include "LoRaWan_APP.h"
|
|
#include "Arduino.h"
|
|
#include "SparkFunBME280.h"
|
|
#include <Wire.h>
|
|
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
|
|
|
|
NAU7802 nau7802;
|
|
BME280 bme280;
|
|
|
|
//#define Vext GPIO6
|
|
|
|
/******************************************************************************/
|
|
/* Firmware Version */
|
|
/******************************************************************************/
|
|
static const int32_t fwVersion = 20211228;
|
|
|
|
/******************************************************************************/
|
|
/* LoraWAN Settings */
|
|
/******************************************************************************/
|
|
|
|
/* OTAA para*/
|
|
uint8_t devEui[8];
|
|
uint8_t appEui[8];
|
|
uint8_t appKey[16];
|
|
|
|
/* ABP para*/
|
|
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda, 0x85 };
|
|
uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef, 0x67 };
|
|
uint32_t devAddr = ( uint32_t )0x007e6ae1;
|
|
|
|
/*LoraWan channelsmask, default channels 0-7*/
|
|
uint16_t userChannelsMask[6] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
|
|
|
|
/*LoraWan region, select in arduino IDE tools*/
|
|
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
|
|
|
|
/*LoraWan Class, Class A and Class C are supported*/
|
|
DeviceClass_t loraWanClass = LORAWAN_CLASS;
|
|
|
|
/*the application data transmission duty cycle. value in [ms].*/
|
|
//uint32_t appTxDutyCycle = 1800000;
|
|
uint32_t appTxDutyCycle = 300000;
|
|
|
|
/*OTAA or ABP*/
|
|
bool overTheAirActivation = LORAWAN_NETMODE;
|
|
|
|
/*ADR enable*/
|
|
bool loraWanAdr = LORAWAN_ADR;
|
|
|
|
/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
|
|
bool keepNet = LORAWAN_NET_RESERVE;
|
|
|
|
/* Indicates if the node is sending confirmed or unconfirmed messages */
|
|
bool isTxConfirmed = LORAWAN_UPLINKMODE;
|
|
|
|
/* Application port */
|
|
uint8_t appPort = 2;
|
|
|
|
uint8_t confirmedNbTrials = 4;
|
|
|
|
/******************************************************************************/
|
|
/* Essential Device Functions */
|
|
/******************************************************************************/
|
|
|
|
void enableVext()
|
|
{
|
|
pinMode(Vext, OUTPUT);
|
|
digitalWrite(Vext, LOW);
|
|
delay(50);
|
|
Wire.begin();
|
|
}
|
|
|
|
void disableVext()
|
|
{
|
|
Wire.end();
|
|
pinMode(Vext, OUTPUT);
|
|
digitalWrite(Vext, HIGH);
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Helper Functions */
|
|
/******************************************************************************/
|
|
|
|
//Following functions are based on "https://github.com/dndubins/QuickStats", by David Dubins
|
|
|
|
void bubbleSort(long A[], int len) {
|
|
unsigned long newn;
|
|
unsigned long n = len;
|
|
long temp = 0;
|
|
do {
|
|
newn = 1;
|
|
for (int p = 1; p < len; p++) {
|
|
if (A[p - 1] > A[p]) {
|
|
temp = A[p]; //swap places in array
|
|
A[p] = A[p - 1];
|
|
A[p - 1] = temp;
|
|
newn = p;
|
|
} //end if
|
|
} //end for
|
|
n = newn;
|
|
} while (n > 1);
|
|
}
|
|
|
|
long median(long samples[], int m) //calculate the median
|
|
{
|
|
//First bubble sort the values: https://en.wikipedia.org/wiki/Bubble_sort
|
|
long sorted[m]; // Define and initialize sorted array.
|
|
long temp = 0; // Temporary float for swapping elements
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
sorted[i] = samples[i];
|
|
}
|
|
bubbleSort(sorted, m); // Sort the values
|
|
|
|
if (bitRead(m, 0) == 1) { //If the last bit of a number is 1, it's odd. This is equivalent to "TRUE". Also use if m%2!=0.
|
|
return sorted[m / 2]; //If the number of data points is odd, return middle number.
|
|
} else {
|
|
return (sorted[(m / 2) - 1] + sorted[m / 2]) / 2; //If the number of data points is even, return avg of the middle two numbers.
|
|
}
|
|
}
|
|
|
|
// Joergs STDDEV
|
|
float stddev(long samples[], int m) //calculate the stdandard deviation
|
|
{
|
|
float sum_x;
|
|
float sum_x2;
|
|
float mean;
|
|
|
|
float stdev;
|
|
sum_x = 0;
|
|
sum_x2 = 0;
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
sum_x = sum_x + samples[i];
|
|
}
|
|
mean = sum_x / m;
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
sum_x2 = sum_x2 + ((samples[i] - mean) * (samples[i] - mean));
|
|
}
|
|
|
|
stdev = sqrt(sum_x2 / m);
|
|
|
|
return stdev;
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Global Data Structures */
|
|
/******************************************************************************/
|
|
|
|
// 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 = 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 = 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
|
|
uint8_t devEui[8]; // keep OTAA settings so that new fw-updates does not overwrite it
|
|
uint8_t appEui[8]; // dito
|
|
uint8_t appKey[16]; // dito
|
|
} __attribute__((packed)) CONFIG_data;
|
|
|
|
typedef struct {
|
|
uint8_t version; // Version of Packet Format (must be increased every time format changes...)
|
|
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)
|
|
int8_t temperature_change[MAX_VALUES_TO_SEND - 1]; // Unterschied Temperatur seit letztem Messwert in 1/10 Grad Celsius
|
|
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 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)
|
|
int32_t weight_a; // Waegezelle A, Raw Value
|
|
int32_t weight_b; // Waegezelle B, Raw Value
|
|
long cal_a_0; // 4 Bytes, Wert Waegezelle A ohne Gewicht
|
|
long cal_b_0; // 4 Bytes, Wert Waegezelle B ohne Gewicht
|
|
float cal_a_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle A
|
|
float cal_b_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle B
|
|
} __attribute__((packed)) LORA_data_first;
|
|
|
|
typedef struct {
|
|
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)
|
|
int32_t weight_a; // Waegezelle A, Raw Value
|
|
int32_t weight_b; // Waegezelle B, Raw Value
|
|
uint16_t weight; // Waegezelle Gesamtgewicht, in 5g
|
|
} SENSOR_data;
|
|
|
|
byte my_position = 0; // what is our actual measurement, starts with 0
|
|
unsigned long timer_pos0;
|
|
|
|
LORA_data lora_data;
|
|
LORA_data_first lora_data_first;
|
|
CONFIG_data config_data;
|
|
SENSOR_data sensor_data;
|
|
SENSOR_data last_sensor_reading;
|
|
long iteration = 0; // what iteration number do we have, starts with 0
|
|
long package_counter = 0; // sent package counter
|
|
bool send_in_progress = false;
|
|
bool stop_iterations = false;
|
|
bool start_new_iteration = false;
|
|
bool next_package_is_init_package = true;
|
|
uint32_t gRebootMs;
|
|
|
|
/******************************************************************************/
|
|
/* Functions for Global Data Structures */
|
|
/******************************************************************************/
|
|
|
|
void ClearLoraData(bool clearLastValues)
|
|
{
|
|
lora_data.version = LORA_DATA_VERSION;
|
|
lora_data.vbat = 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++) {
|
|
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] = 127;
|
|
}
|
|
}
|
|
|
|
lora_data_first.version = LORA_DATA_VERSION_FIRST_PACKAGE;
|
|
lora_data_first.fw_version = fwVersion;
|
|
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;
|
|
lora_data_first.cal_b_0 = config_data.cal_b_0;
|
|
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;
|
|
|
|
// We initialize last_sensor_reading
|
|
if (clearLastValues) {
|
|
last_sensor_reading.vbat = 0;
|
|
last_sensor_reading.weight_a = 0;
|
|
last_sensor_reading.weight_b = 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)
|
|
{
|
|
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(" \"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);
|
|
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(" \"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);
|
|
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 */
|
|
/******************************************************************************/
|
|
|
|
#define ROW 0
|
|
#define ROW_OFFSET 100
|
|
|
|
//CY_FLASH_SIZEOF_ROW is 256 , CY_SFLASH_USERBASE is 0x0ffff400
|
|
#define addr CY_SFLASH_USERBASE+CY_FLASH_SIZEOF_ROW*ROW + ROW_OFFSET
|
|
|
|
void ReadConfigDataFromFlash()
|
|
{
|
|
FLASH_read_at(addr, reinterpret_cast<uint8_t*>(&config_data), sizeof(config_data));
|
|
}
|
|
|
|
void WriteConfigDataToFlash()
|
|
{
|
|
FLASH_update(addr, reinterpret_cast<uint8_t*>(&config_data), sizeof(config_data));
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Functions to interface with BME280 */
|
|
/******************************************************************************/
|
|
|
|
void SetupBME280()
|
|
{
|
|
Wire.begin();
|
|
Wire.setClock(400000); //Increase to fast I2C speed!
|
|
|
|
bme280.beginI2C();
|
|
|
|
bme280.setMode(MODE_SLEEP); //Sleep for now
|
|
}
|
|
|
|
void ReadBME280(bool read_humidity_and_pressure)
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Functions to interface with Load Cells */
|
|
/******************************************************************************/
|
|
|
|
#define SAMPLES 3
|
|
|
|
//byte interruptPin = A0;
|
|
|
|
bool InitializeScales()
|
|
{
|
|
bool result;
|
|
result = nau7802.reset(); //Reset all registers
|
|
result &= nau7802.powerUp(); //Power on analog and digital sections of the scale
|
|
|
|
result &= nau7802.setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
|
|
result &= nau7802.setGain(NAU7802_GAIN_128); //Set gain to 128
|
|
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.
|
|
|
|
return result;
|
|
}
|
|
|
|
bool SetupScales()
|
|
{
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("SetupScales start\n");
|
|
}
|
|
// pinMode(interruptPin, INPUT);
|
|
|
|
if (!nau7802.begin(Wire, false))
|
|
{
|
|
Serial.printf("Scale not detected. Please check wiring. Freezing...\n");
|
|
return false;
|
|
}
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Scale detected!\n");
|
|
}
|
|
|
|
bool result = InitializeScales();
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("SetupScales done, result: %d\n", result);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
long ReadScale(char channel)
|
|
{
|
|
long res;
|
|
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("ReadScale Start, Channel %c\n", channel);
|
|
}
|
|
|
|
uint8_t channelNumber;
|
|
if (channel == 'B') {
|
|
channelNumber = NAU7802_CHANNEL_1;
|
|
} else {
|
|
channelNumber = NAU7802_CHANNEL_2;
|
|
}
|
|
unsigned long startTime = millis();
|
|
nau7802.setChannel(channelNumber);
|
|
bool calibrate_success = nau7802.calibrateAFE();
|
|
if (! calibrate_success) {
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Error: Calibration not successful!\n");
|
|
}
|
|
}
|
|
|
|
if (nau7802.available()) {
|
|
long dummy = nau7802.getReading();
|
|
}
|
|
int const num_scale_readings = SAMPLES; // number of instantaneous scale readings to calculate the median
|
|
|
|
// we use the median, not the average, see https://community.particle.io/t/boron-gpio-provides-less-current-than-electrons-gpio/46647/13
|
|
long readings[num_scale_readings]; // create array to hold readings
|
|
for (int i = 0; i < num_scale_readings; i++) {
|
|
//while (digitalRead(interruptPin) == LOW) {
|
|
unsigned long mytimer = millis();
|
|
int timeouts = 0;
|
|
while (! nau7802.available() && (timeouts < 3)) {
|
|
// we set a timeout of 10 seconds for the measurement...
|
|
if ((millis() - mytimer) > 10000) {
|
|
timeouts = timeouts + 1;
|
|
// Timeout reading scale...
|
|
Wire.endTransmission(true);
|
|
delay(50);
|
|
InitializeScales();
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Timeout while reading scale...\n");
|
|
}
|
|
}
|
|
}
|
|
long reading;
|
|
if (nau7802.available()) {
|
|
reading = nau7802.getReading();
|
|
readings[i] = reading;
|
|
}
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Reading: %d\n", reading);
|
|
}
|
|
}
|
|
|
|
unsigned long duration = millis() - startTime;
|
|
res = median(readings, num_scale_readings); // calculate median
|
|
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Median of %d samples: %d\n", num_scale_readings, res);
|
|
float sdev;
|
|
sdev = stddev(readings, num_scale_readings);
|
|
float sdev_proc;
|
|
sdev_proc = 100 * (sdev / float(res));
|
|
Serial.printf("Measurements: [");
|
|
for (int i = 0; i < num_scale_readings; i++) {
|
|
Serial.printf("%d", readings[i]);
|
|
if (i < (SAMPLES - 1)) {
|
|
Serial.printf(",");
|
|
}
|
|
|
|
}
|
|
Serial.printf("]\n");
|
|
|
|
Serial.printf("Standard Deviation: %d.%03d\n", (int)sdev, (int)abs(sdev * 1000) % 1000);
|
|
Serial.printf("Standard Deviation / Median (Percent): %d.%03d\n", (int)sdev_proc, (int)abs(sdev_proc * 1000) % 1000);
|
|
Serial.printf("Duration (ms): %d\n", duration);
|
|
}
|
|
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("ReadScale Done\n");
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void PowerdownScale()
|
|
{
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("PowerdownScale Start\n");
|
|
}
|
|
nau7802.powerDown();
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("PowerdownScale Done\n");
|
|
}
|
|
}
|
|
|
|
void PowerupScale()
|
|
{
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("PowerupScale Start\n");
|
|
}
|
|
|
|
InitializeScales();
|
|
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("PowerupScale Done\n");
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Functions to interface with LoraWAN */
|
|
/******************************************************************************/
|
|
|
|
/* Prepares the payload of the frame */
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* User defined functions, to be used with AT... */
|
|
/******************************************************************************/
|
|
|
|
bool checkUserAt(char *cmd, char *content)
|
|
{
|
|
if (strcmp(cmd, "DEBUG") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
Serial.print("+DEBUG=");
|
|
Serial.print(config_data.debug_level);
|
|
Serial.println();
|
|
}
|
|
else
|
|
{
|
|
config_data.debug_level = atol(content);
|
|
WriteConfigDataToFlash();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "READ_CONFIG") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
Serial.printf("{\n");
|
|
Serial.printf(" \"cal_a_0\": \"%d\",\n", config_data.cal_a_0);
|
|
Serial.printf(" \"cal_b_0\": \"%d\",\n", config_data.cal_b_0);
|
|
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("}\n");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "SAVE_OTAA_CONFIG") == 0)
|
|
{
|
|
if (content[0] == '1')
|
|
{
|
|
memcpy(config_data.devEui, devEui, sizeof(devEui));
|
|
memcpy(config_data.appEui, appEui, sizeof(appEui));
|
|
memcpy(config_data.appKey, appKey, sizeof(appKey));
|
|
WriteConfigDataToFlash();
|
|
Serial.printf("saved OTAA configuration to flash\n");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "READ_SENSORS") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
ReadSensors(true);
|
|
Serial.printf("{\n");
|
|
Serial.printf(" \"weight\": \"%d\",\n", sensor_data.weight);
|
|
Serial.printf(" \"weight_a_raw\": \"%d\",\n", sensor_data.weight_a);
|
|
Serial.printf(" \"weight_b_raw\": \"%d\",\n", sensor_data.weight_b);
|
|
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(" \"vbat\": \"%d\"\n", sensor_data.vbat);
|
|
Serial.printf("}\n");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "VBAT") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
uint16_t VBatVoltage = getBatteryVoltage();
|
|
Serial.print("+VBAT=");
|
|
Serial.print(VBatVoltage);
|
|
Serial.println();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "CAL_A_0") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
Serial.print("+CAL_A_0=");
|
|
Serial.print(config_data.cal_a_0);
|
|
Serial.println();
|
|
}
|
|
else
|
|
{
|
|
config_data.cal_a_0 = atol(content);
|
|
WriteConfigDataToFlash();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "CAL_B_0") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
Serial.print("+CAL_B_0=");
|
|
Serial.print(config_data.cal_b_0);
|
|
Serial.println();
|
|
}
|
|
else
|
|
{
|
|
config_data.cal_b_0 = atol(content);
|
|
WriteConfigDataToFlash();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "CAL_A_FACTOR") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
Serial.print("+CAL_A_FACTOR=");
|
|
Serial.print(config_data.cal_a_factor);
|
|
Serial.println();
|
|
}
|
|
else
|
|
{
|
|
config_data.cal_a_factor = atof(content);
|
|
WriteConfigDataToFlash();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (strcmp(cmd, "CAL_B_FACTOR") == 0)
|
|
{
|
|
if (content[0] == '?')
|
|
{
|
|
Serial.print("+CAL_B_FACTOR=");
|
|
Serial.print(config_data.cal_b_factor);
|
|
Serial.println();
|
|
}
|
|
else
|
|
{
|
|
config_data.cal_b_factor = atof(content);
|
|
WriteConfigDataToFlash();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Other Functions */
|
|
/******************************************************************************/
|
|
|
|
void setup_platform(void)
|
|
{
|
|
boardInitMcu();
|
|
Serial.begin(115200);
|
|
|
|
// read config_data from flash...
|
|
ReadConfigDataFromFlash();
|
|
memcpy(devEui, config_data.devEui, sizeof(config_data.devEui));
|
|
memcpy(appEui, config_data.appEui, sizeof(config_data.appEui));
|
|
memcpy(appKey, config_data.appKey, sizeof(config_data.appKey));
|
|
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Setup_platform, this is the configuration\n");
|
|
Serial.printf("cal_a_0: %d\n", config_data.cal_a_0);
|
|
Serial.printf("cal_b_0: %d\n", config_data.cal_b_0);
|
|
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);
|
|
}
|
|
}
|
|
|
|
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));
|
|
if (next_package_is_init_package) {
|
|
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;
|
|
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.vbat = sensor_data.vbat;
|
|
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 > 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.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++;
|
|
}
|
|
|
|
bool TooBigWeightChange()
|
|
{
|
|
// on first position there cannot be a difference
|
|
if (my_position == 1) {
|
|
return false;
|
|
}
|
|
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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
bool send_data;
|
|
enableVext();
|
|
SetupBME280();
|
|
SetupScales();
|
|
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.vbat = GetVBatValue(voltage);
|
|
if (config_data.debug_level > 0) {
|
|
Serial.printf("Read ADC, %d Millivolts...\n", voltage);
|
|
}
|
|
|
|
if (!read_only) {
|
|
AddSensorDataToLoraData();
|
|
send_data = (TooBigWeightChange()) || (my_position >= MAX_VALUES_TO_SEND) || (iteration <= INIT_PACKETS) || (iteration % INIT_PACKAGE_INTERVAL == 0);
|
|
last_sensor_reading = sensor_data;
|
|
|
|
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;
|
|
}
|
|
return send_data;
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Arduino setup function */
|
|
/******************************************************************************/
|
|
|
|
void setup() {
|
|
setup_platform();
|
|
ClearLoraData(true);
|
|
#if(AT_SUPPORT)
|
|
enableAt();
|
|
#endif
|
|
deviceState = DEVICE_STATE_INIT;
|
|
LoRaWAN.ifskipjoin();
|
|
}
|
|
|
|
/******************************************************************************/
|
|
/* Arduino loop function */
|
|
/******************************************************************************/
|
|
|
|
void loop()
|
|
{
|
|
switch ( deviceState )
|
|
{
|
|
case DEVICE_STATE_INIT:
|
|
{
|
|
#if(LORAWAN_DEVEUI_AUTO)
|
|
LoRaWAN.generateDeveuiByChipID();
|
|
#endif
|
|
#if(AT_SUPPORT)
|
|
getDevParam();
|
|
#endif
|
|
printDevParam();
|
|
LoRaWAN.init(loraWanClass, loraWanRegion);
|
|
deviceState = DEVICE_STATE_JOIN;
|
|
break;
|
|
}
|
|
case DEVICE_STATE_JOIN:
|
|
{
|
|
LoRaWAN.join();
|
|
break;
|
|
}
|
|
case DEVICE_STATE_SEND:
|
|
{
|
|
if (ReadSensors(false)) {
|
|
prepareTxFrame(appPort);
|
|
LoRaWAN.send();
|
|
package_counter++;
|
|
ClearLoraData(false);
|
|
}
|
|
deviceState = DEVICE_STATE_CYCLE;
|
|
break;
|
|
}
|
|
case DEVICE_STATE_CYCLE:
|
|
{
|
|
// Schedule next packet transmission
|
|
txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );
|
|
LoRaWAN.cycle(txDutyCycleTime);
|
|
deviceState = DEVICE_STATE_SLEEP;
|
|
break;
|
|
}
|
|
case DEVICE_STATE_SLEEP:
|
|
{
|
|
LoRaWAN.sleep();
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
deviceState = DEVICE_STATE_INIT;
|
|
break;
|
|
}
|
|
}
|
|
}
|