Initial commit
This commit is contained in:
commit
d6b0b23e8e
|
|
@ -0,0 +1,29 @@
|
|||
# mini-beieli-node - Bienenstock Ueberwachung
|
||||
|
||||
Code fuer den LoraWAN Node (CubeCell)
|
||||
|
||||
Autor: Joerg Lehmann, nbit Informatik GmbH
|
||||
|
||||
| Parameter | Value |
|
||||
| ------------------------ | --------------------------- |
|
||||
| Board | CubeCell-Board (HTTC-AB01) |
|
||||
| LORAWAN_REGION | REGION_EU868 |
|
||||
| LORAWAN_CLASS | CLASS_A |
|
||||
| LORAWAN_DEVEUI | CUSTOM |
|
||||
| LORAWAN_NETMODE | OTAA |
|
||||
| LORAWAN_ADR | ON |
|
||||
| LORAWAN_UPLINKMODE | UNCONFIRMED |
|
||||
| LORAWAN_Net_Reservation | OFF |
|
||||
| LORAWAN_AT_SUPPORT | ON |
|
||||
| LORAWAN_RGB | DEACTIVE |
|
||||
| LoRaWan Debug Level | None |
|
||||
|
||||
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/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git | 688f255 | Fri, 3 Jan 2020 12:35:22 -0700 |
|
||||
|
||||
|
||||
[1]: Commit String: git log --pretty=format:'%h | %cD |' -n 1
|
||||
|
|
@ -0,0 +1,787 @@
|
|||
#include "LoRaWan_APP.h"
|
||||
#include "Arduino.h"
|
||||
#include "Seeed_BME280.h"
|
||||
#include <Wire.h>
|
||||
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
|
||||
|
||||
NAU7802 nau7802;
|
||||
BME280 bme280;
|
||||
|
||||
/******************************************************************************/
|
||||
/* 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 };
|
||||
|
||||
/* 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(500);
|
||||
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 */
|
||||
/******************************************************************************/
|
||||
static const int32_t fwVersion = 20200925;
|
||||
|
||||
// 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 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;
|
||||
|
||||
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
|
||||
} __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
|
||||
int16_t temperature; // Temperatur (Startwert) in 1/10 Grad Celsius
|
||||
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
|
||||
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
|
||||
int16_t temperature; // Temperatur in 1/10 Grad Celsius
|
||||
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 vsupercap; // Spannung Supercap (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
|
||||
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
||||
int16_t temperature; // Temperatur in 1/10 Grad Celsius
|
||||
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.vsupercap = 0;
|
||||
lora_data.temperature = 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 - 1)) {
|
||||
lora_data.temperature_change[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
my_position = 0;
|
||||
|
||||
// We initialize last_sensor_reading
|
||||
if (clearLastValues) {
|
||||
last_sensor_reading.vsupercap = 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;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* 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()
|
||||
{
|
||||
if (!bme280.init()) {
|
||||
Serial.println("Error: cannot initialize BME280");
|
||||
}
|
||||
}
|
||||
|
||||
void ReadBME280()
|
||||
{
|
||||
//get and print temperatures
|
||||
/* warm up the BME280 by discarding a measurement */
|
||||
bme280.getTemperature();
|
||||
|
||||
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
|
||||
|
||||
//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.
|
||||
|
||||
result &= nau7802.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
|
||||
|
||||
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;
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
delay(50);
|
||||
}
|
||||
long reading;
|
||||
if (nau7802.available()) {
|
||||
reading = nau7802.getReading();
|
||||
readings[i] = reading;
|
||||
}
|
||||
if (config_data.debug_level > 0) {
|
||||
Serial.printf("Reading: %d\n", reading);
|
||||
}
|
||||
delay(50);
|
||||
}
|
||||
|
||||
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 ReadScales()
|
||||
{
|
||||
PowerupScale();
|
||||
sensor_data.weight_a = ReadScale('A');
|
||||
sensor_data.weight_b = ReadScale('B');
|
||||
PowerdownScale();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* 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));
|
||||
} else {
|
||||
appDataSize = sizeof(lora_data);
|
||||
memcpy(&appData, &lora_data, 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, "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(" \"vsupercap\": \"%d\",\n", sensor_data.vsupercap);
|
||||
Serial.printf("}\n");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(cmd, "SUPERCAP") == 0)
|
||||
{
|
||||
if (content[0] == '?')
|
||||
{
|
||||
uint16_t BatteryVoltage = getBatteryVoltage();
|
||||
Serial.print("+SUPERCAP=");
|
||||
Serial.print(BatteryVoltage);
|
||||
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();
|
||||
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;
|
||||
|
||||
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.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;
|
||||
} 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;
|
||||
} else {
|
||||
temp_change = sensor_data.temperature - last_sensor_reading.temperature;
|
||||
if (temp_change > 127) {
|
||||
temp_change = 127;
|
||||
}
|
||||
if (temp_change < -128) {
|
||||
temp_change = -128;
|
||||
}
|
||||
lora_data.temperature_change[my_position - 1] = (uint8_t)temp_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 (my_position == 0) {
|
||||
timer_pos0 = millis();
|
||||
}
|
||||
my_position++;
|
||||
}
|
||||
|
||||
// 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();
|
||||
ReadScales();
|
||||
disableVext();
|
||||
|
||||
uint16_t voltage = getBatteryVoltage();
|
||||
sensor_data.vsupercap = voltage;
|
||||
|
||||
if (!read_only) {
|
||||
AddSensorDataToLoraData();
|
||||
send_data = (iteration % MAX_VALUES_TO_SEND == 0) || (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);
|
||||
}
|
||||
|
||||
|
||||
} 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(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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue