mini-beieli-node/mini-beieli-node.ino

1102 lines
34 KiB
C++

/*
mini-beieli-node.ino
BeieliScale, see https://mini-beieli.ch
Joerg Lehmann, nbit Informatik GmbH
*/
#include <Catena.h>
#include <Catena_Led.h>
#include <Catena_CommandStream.h>
#include <Catena_Mx25v8035f.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Arduino_LoRaWAN.h>
#include <lmic.h>
#include <hal/hal.h>
#include <mcciadk_baselib.h>
#include <cmath>
#include <type_traits>
#include <HX711.h>
using namespace McciCatena;
/****************************************************************************\
|
| MANIFEST CONSTANTS & TYPEDEFS
|
\****************************************************************************/
constexpr uint8_t kUplinkPort = 2;
/* how long do we wait between transmissions? (in seconds) */
enum {
// set this to interval between transmissions, in seconds
// Actual time will be a little longer because have to
// add measurement and broadcast time, but we attempt
// to compensate for the gross effects below.
CATCFG_T_CYCLE = 6 * 60, // every 6 minutes
CATCFG_T_CYCLE_TEST = 30, // every 10 seconds
CATCFG_T_CYCLE_INITIAL = 30, // every 30 seconds initially
CATCFG_INTERVAL_COUNT_INITIAL = 30, // repeat for 15 minutes
};
/* additional timing parameters; ususually you don't change these. */
enum {
CATCFG_T_WARMUP = 1,
CATCFG_T_SETTLE = 5,
CATCFG_T_OVERHEAD = (CATCFG_T_WARMUP + CATCFG_T_SETTLE),
CATCFG_T_MIN = CATCFG_T_OVERHEAD,
CATCFG_T_MAX = CATCFG_T_CYCLE < 60 * 60 ? 60 * 60 : CATCFG_T_CYCLE, // normally one hour max.
CATCFG_INTERVAL_COUNT = 30,
};
constexpr uint32_t CATCFG_GetInterval(uint32_t tCycle)
{
return (tCycle < CATCFG_T_OVERHEAD)
? CATCFG_T_OVERHEAD
: tCycle - CATCFG_T_OVERHEAD;
}
enum {
CATCFG_T_INTERVAL = CATCFG_GetInterval(CATCFG_T_CYCLE),
};
enum {
PIN_ONE_WIRE = A2, // XSDA1 == A2
PIN_SHT10_CLK = 8, // XSCL0 == D8
PIN_SHT10_DATA = 12, // XSDA0 == D12
};
// forwards
static void settleDoneCb(osjob_t* pSendJob);
static void warmupDoneCb(osjob_t* pSendJob);
static void txNotProvisionedCb(osjob_t *pSendJob);
static void sleepDoneCb(osjob_t* pSendJob);
static Arduino_LoRaWAN::SendBufferCbFn sendBufferDoneCb;
static Arduino_LoRaWAN::ReceivePortBufferCbFn receiveMessage;
// Additional Commands
// forward reference to the command function
cCommandStream::CommandFn cmdHello;
cCommandStream::CommandFn cmdGetCalibrationSettings;
cCommandStream::CommandFn cmdGetSensorReadings;
cCommandStream::CommandFn cmdGetScaleA;
cCommandStream::CommandFn cmdGetScaleB;
cCommandStream::CommandFn cmdCalibrateZeroScaleA;
cCommandStream::CommandFn cmdCalibrateZeroScaleB;
cCommandStream::CommandFn cmdCalibrateScaleA;
cCommandStream::CommandFn cmdCalibrateScaleB;
cCommandStream::CommandFn cmdSetDebugLevel;
cCommandStream::CommandFn cmdGetDebugLevel;
// the individual commmands are put in this table
static const cCommandStream::cEntry sMyExtraCommmands[] =
{
{ "hello", cmdHello },
{ "get_calibration_settings", cmdGetCalibrationSettings },
{ "get_sensor_readings", cmdGetSensorReadings },
{ "calibrate_zero_scale_a", cmdCalibrateZeroScaleA },
{ "calibrate_zero_scale_b", cmdCalibrateZeroScaleB },
{ "calibrate_scale_a", cmdCalibrateScaleA },
{ "calibrate_scale_b", cmdCalibrateScaleB },
{ "set_debug_level", cmdSetDebugLevel },
{ "get_debug_level", cmdGetDebugLevel },
// other commands go here....
};
/* a top-level structure wraps the above and connects to the system table */
/* it optionally includes a "first word" so you can for sure avoid name clashes */
static cCommandStream::cDispatch
sMyExtraCommands_top(
sMyExtraCommmands, /* this is the pointer to the table */
sizeof(sMyExtraCommmands), /* this is the size of the table */
"application" /* this is the "first word" for all the commands in this table*/
);
/****************************************************************************\
|
| READ-ONLY DATA
|
\****************************************************************************/
static const int32_t fwVersion = 20191004;
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 = 129;
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
/****************************************************************************\
|
| VARIABLES
|
\****************************************************************************/
// must be 139 bytes long (size of kBme680Cal)
typedef struct {
long cal_w1_0; // 4 Bytes, Wert Waegezelle 1 ohne Gewicht
long cal_w2_0; // 4 Bytes, Wert Waegezelle 2 ohne Gewicht
float cal_w1_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
float cal_w2_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
byte debug_level; // 0 => no debugging, 1 => infos, 2 => error, 3 => highest level
byte fill[122];
} __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])
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 vbat; // Batteriespannung (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 weight1; // Waegezelle 1, Raw Value
int32_t weight2; // Waegezelle 2, Raw Value
uint16_t weight; // Waegezelle Gesamtgewicht, in 5g
} __attribute__((packed)) LORA_data_first;
typedef struct {
uint8_t vbat; // Batteriespannung (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 weight1; // Waegezelle 1, Raw Value
int32_t weight2; // Waegezelle 2, Raw Value
uint16_t weight; // Waegezelle Gesamtgewicht, in 5g
} SENSOR_data;
byte my_position = 0; // what is our actual measurement, starts with 0
long timer_pos0;
// Global Variables
LORA_data lora_data;
LORA_data_first lora_data_first;
CONFIG_data config_data;
SENSOR_data last_sensor_reading;
// generic timer
long t_cur;
// the primary object
Catena gCatena;
//
// the LoRaWAN backhaul. Note that we use the
// Catena version so it can provide hardware-specific
// information to the base class.
//
Catena::LoRaWAN gLoRaWAN;
//
// the LED
StatusLed gLed(Catena::PIN_STATUS_LED);
// The temperature/humidity sensor
Adafruit_BME280 gBME280; // The default initalizer creates an I2C connection
bool fBme;
SPIClass gSPI2(
Catena::PIN_SPI2_MOSI,
Catena::PIN_SPI2_MISO,
Catena::PIN_SPI2_SCK);
// The flash
Catena_Mx25v8035f gFlash;
bool fFlash;
// Scales
HX711 LoadCell;
// USB power
bool fUsbPower;
// have we printed the sleep info?
bool g_fPrintedSleeping = false;
// tFhe job that's used to synchronize us with the LMIC code
static osjob_t sensorJob;
void sensorJob_cb(osjob_t* pJob);
void setup(void)
{
gCatena.begin();
ClearLoraData();
// Use D10 to regulate power
pinMode(D10, OUTPUT);
setup_platform();
setup_bme280();
//setup_scales();
setup_flash();
setup_uplink();
}
void setup_platform(void)
{
/* add our application-specific commands */
gCatena.addCommands(
sMyExtraCommands_top,
nullptr
);
// read config_data from fram...
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Reading Calibration Config from FRAM...\n");
}
gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data));
if (config_data.debug_level > 0) {
gCatena.SafePrintf("cal_w1_0: %d\n", config_data.cal_w1_0);
gCatena.SafePrintf("cal_w2_0: %d\n", config_data.cal_w2_0);
gCatena.SafePrintf("cal_w1_factor: %d.%03d\n", (int)config_data.cal_w1_factor, (int)(config_data.cal_w1_factor * 1000) % 1000);
gCatena.SafePrintf("cal_w2_factor: %d.%03d\n", (int)config_data.cal_w2_factor, (int)(config_data.cal_w2_factor * 1000) % 1000);
gCatena.SafePrintf("debug_level: %d\n", (int)config_data.debug_level);
gCatena.SafePrintf("Size of config_data: %d\n", sizeof(config_data));
}
#ifdef USBCON
// if running unattended, don't wait for USB connect.
if (!(gCatena.GetOperatingFlags() & static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fUnattended))) {
while (!Serial)
/* wait for USB attach */
yield();
}
#endif
gCatena.SafePrintf("\n");
gCatena.SafePrintf("-------------------------------------------------------------------------------\n");
gCatena.SafePrintf("mini-beieli.ch - BeieliScale Version %d.\n", fwVersion);
{
char sRegion[16];
gCatena.SafePrintf("Target network: %s / %s\n",
gLoRaWAN.GetNetworkName(),
gLoRaWAN.GetRegionString(sRegion, sizeof(sRegion)));
}
gCatena.SafePrintf("Enter 'help' for a list of commands.\n");
#ifdef CATENA_CFG_SYSCLK
gCatena.SafePrintf("SYSCLK: %d MHz\n", CATENA_CFG_SYSCLK);
#endif
#ifdef USBCON
gCatena.SafePrintf("USB enabled\n");
#else
gCatena.SafePrintf("USB disabled\n");
#endif
Catena::UniqueID_string_t CpuIDstring;
gCatena.SafePrintf(
"CPU Unique ID: %s\n",
gCatena.GetUniqueIDstring(&CpuIDstring));
gCatena.SafePrintf("--------------------------------------------------------------------------------\n");
gCatena.SafePrintf("\n");
// set up the LED
gLed.begin();
gCatena.registerObject(&gLed);
gLed.Set(LedPattern::FastFlash);
// set up LoRaWAN
gCatena.SafePrintf("LoRaWAN init: ");
if (!gLoRaWAN.begin(&gCatena)) {
gCatena.SafePrintf("failed\n");
}
else {
gCatena.SafePrintf("succeeded\n");
}
gLoRaWAN.SetReceiveBufferBufferCb(receiveMessage);
gCatena.registerObject(&gLoRaWAN);
/* find the platform */
const Catena::EUI64_buffer_t* pSysEUI = gCatena.GetSysEUI();
uint32_t flags;
const CATENA_PLATFORM* const pPlatform = gCatena.GetPlatform();
if (pPlatform) {
gCatena.SafePrintf("EUI64: ");
for (unsigned i = 0; i < sizeof(pSysEUI->b); ++i) {
gCatena.SafePrintf("%s%02x", i == 0 ? "" : "-", pSysEUI->b[i]);
}
gCatena.SafePrintf("\n");
flags = gCatena.GetPlatformFlags();
gCatena.SafePrintf(
"Platform Flags: %#010x\n",
flags);
gCatena.SafePrintf(
"Operating Flags: %#010x\n",
gCatena.GetOperatingFlags());
}
else {
gCatena.SafePrintf("**** no platform, check provisioning ****\n");
flags = 0;
}
}
void setup_bme280(void)
{
if (gBME280.begin(BME280_ADDRESS, Adafruit_BME280::OPERATING_MODE::Sleep)) {
fBme = true;
}
else {
fBme = false;
gCatena.SafePrintf("No BME280 found: check wiring\n");
}
}
bool setup_scales(void)
{
bool res;
res = true;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Setup Scales...\n");
}
// Enable Power
digitalWrite(D10, HIGH);
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
LoadCell.begin(A1, A0);
if (!(LoadCell.wait_ready_timeout(2000))) {
gCatena.SafePrintf("Scale not ready after Init.\n");
res = false;
}
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Setup Scale is complete\n");
}
return res;
}
void setup_flash(void)
{
if (gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS)) {
fFlash = true;
gFlash.powerDown();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("FLASH found, but power down\n");
}
}
else {
fFlash = false;
gFlash.end();
gSPI2.end();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("No FLASH found: check hardware\n");
}
}
}
void setup_uplink(void)
{
// Do an unjoin, so every reboot will trigger a join
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Do an unjoin...\n");
}
LMIC_unjoin();
/* trigger a join by sending the first packet */
if (!(gCatena.GetOperatingFlags() & static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fManufacturingTest))) {
if (!gLoRaWAN.IsProvisioned())
gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n");
else {
gLed.Set(LedPattern::Joining);
/* trigger a join by sending the first packet */
ReadSensors(true, false);
}
}
}
// The Arduino loop routine -- in our case, we just drive the other loops.
// If we try to do too much, we can break the LMIC radio. So the work is
// done by outcalls scheduled from the LMIC os loop.
void loop()
{
gCatena.poll();
}
void ClearLoraData(void)
{
lora_data.version = LORA_DATA_VERSION;
lora_data.vbat = 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.vbat = 0;
lora_data_first.humidity = 0;
lora_data_first.pressure = 0;
lora_data_first.weight1 = 0;
lora_data_first.weight2 = 0;
lora_data_first.weight = 0;
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)
{
if (firstTime) {
gCatena.SafePrintf("{\n");
gCatena.SafePrintf(" \"version\": \"%d\",\n", lora_data_first.version);
gCatena.SafePrintf(" \"fw_version\": \"%d\",\n", lora_data_first.fw_version);
gCatena.SafePrintf(" \"vbat\": \"%u\",\n", lora_data_first.vbat);
gCatena.SafePrintf(" \"humidity\": \"%u\",\n", lora_data_first.humidity);
gCatena.SafePrintf(" \"pressure\": \"%u\",\n", lora_data_first.pressure);
gCatena.SafePrintf(" \"weight1\": \"%ld\",\n", lora_data_first.weight1);
gCatena.SafePrintf(" \"weight2\": \"%ld\",\n", lora_data_first.weight2);
gCatena.SafePrintf(" \"weight\": \"%u\",\n", lora_data_first.weight);
gCatena.SafePrintf(" \"temperature\": \"%u\",\n", lora_data_first.temperature);
gCatena.SafePrintf("}\n");
} else {
gCatena.SafePrintf("{\n");
gCatena.SafePrintf(" \"version\": \"%d\",\n", lora_data.version);
gCatena.SafePrintf(" \"vbat\": \"%u\",\n", lora_data.vbat);
gCatena.SafePrintf(" \"temperature\": \"%u\",\n", lora_data.temperature);
gCatena.SafePrintf(" \"humidity\": [");
for (int i = 0; i < MAX_VALUES_TO_SEND; i++) {
gCatena.SafePrintf("%d", lora_data.humidity[i]);
if (i < (MAX_VALUES_TO_SEND - 1)) {
gCatena.SafePrintf(",");
}
}
gCatena.SafePrintf("],\n");
gCatena.SafePrintf(" \"pressure\": [");
for (int i = 0; i < MAX_VALUES_TO_SEND; i++) {
gCatena.SafePrintf("%d", lora_data.pressure[i]);
if (i < (MAX_VALUES_TO_SEND - 1)) {
gCatena.SafePrintf(",");
}
}
gCatena.SafePrintf("],\n");
gCatena.SafePrintf(" \"weight\": [");
for (int i = 0; i < MAX_VALUES_TO_SEND; i++) {
gCatena.SafePrintf("%ld", lora_data.weight[i]);
if (i < (MAX_VALUES_TO_SEND - 1)) {
gCatena.SafePrintf(",");
}
}
gCatena.SafePrintf("],\n");
gCatena.SafePrintf(" \"temperature_change\": [");
for (int i = 0; i < MAX_VALUES_TO_SEND - 1; i++) {
gCatena.SafePrintf("%d", lora_data.temperature_change[i]);
if (i < (MAX_VALUES_TO_SEND - 2)) {
gCatena.SafePrintf(",");
}
}
gCatena.SafePrintf("]\n");
gCatena.SafePrintf("}\n");
}
}
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;
}
void DoDeepSleep()
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Now going to deep sleep: %d\n", millis());
}
// Prepare Deep Sleep
gLed.Set(LedPattern::Off);
Serial.end();
Wire.end();
SPI.end();
if (fFlash)
gSPI2.end();
// Now sleeping...
gCatena.Sleep(CATCFG_T_INTERVAL);
// Recover from wakeup...
Serial.begin();
Wire.begin();
SPI.begin();
if (fFlash)
gSPI2.begin();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Done with deep sleep: %d\n", millis());
}
}
void ReadSensors(bool firstTime, bool readOnly)
{
int16_t temp_current;
uint8_t humidity_current;
uint8_t pressure_current;
int32_t weight1_current;
int32_t weight2_current;
int16_t temp_change;
int32_t weight_current32;
uint16_t weight_current;
// vBat
float vBat = gCatena.ReadVbat();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f));
}
// vBus
float vBus = gCatena.ReadVbus();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
}
fUsbPower = (vBus > 4.3) ? true : false;
// Setup Scales
// Read Scales
if (setup_scales()) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("HX711 LoadCell is ready.\n");
}
LoadCell.set_gain(128);
long w1 = LoadCell.read_average(5);
weight1_current = (int32_t)w1;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1);
gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current);
}
LoadCell.set_gain(32);
long w2 = LoadCell.read_average(5);
weight2_current = (int32_t)w2;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2);
gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current);
}
}
else {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("HX711 LoadCell not ready.\n");
}
}
// Disable Power
digitalWrite(D10, LOW);
// Gewicht berechnen
weight_current32 = (int32_t)((((weight1_current - config_data.cal_w1_0) / config_data.cal_w1_factor) + ((weight2_current - config_data.cal_w2_0) / config_data.cal_w2_factor)) / 5.0);
if (weight_current32 < 0) {
weight_current32 = 0;
} else if (weight_current32 > UINT16_MAX) {
weight_current32 = UINT16_MAX;
}
weight_current = (uint16_t)weight_current32;
if (fBme) {
/* warm up the BME280 by discarding a measurement */
(void)gBME280.readTemperature();
Adafruit_BME280::Measurements m = gBME280.readTemperaturePressureHumidity();
// temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C
// pressure is 2 bytes, hPa * 10.
// humidity is one byte, where 0 == 0/256 and 0xFF == 255/256.
if (config_data.debug_level > 0) {
gCatena.SafePrintf(
"BME280: T: %d P: %d RH: %d\n",
(int)m.Temperature,
(int)m.Pressure,
(int)m.Humidity);
}
temp_current = (int16_t)((m.Temperature) * 10);
humidity_current = (uint8_t)m.Humidity;
pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET);
if (config_data.debug_level > 0) {
gCatena.SafePrintf("pressure_current: %d\n", pressure_current);
}
}
if (not(readOnly)) {
if (firstTime) {
lora_data_first.vbat = GetVBatValue((int)(vBat * 1000.0f));
lora_data_first.weight1 = weight1_current;
lora_data_first.weight2 = weight2_current;
lora_data_first.weight = weight_current;
lora_data_first.temperature = temp_current;
lora_data_first.humidity = humidity_current;
lora_data_first.pressure = pressure_current;
} else {
lora_data.vbat = GetVBatValue((int)(vBat * 1000.0f));
lora_data.weight[my_position] = weight_current;
if (my_position == 0) {
lora_data.temperature = temp_current;
} else {
temp_change = temp_current - 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] = humidity_current;
lora_data.pressure[my_position] = pressure_current;
}
if (my_position == 0) {
timer_pos0 = millis();
}
if (config_data.debug_level > 0) {
ShowLORAData(firstTime);
}
my_position++;
// 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 threshold or the first measurement is
// more than one hour old (which should not happen :-) )
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);
if (config_data.debug_level > 0) {
gCatena.SafePrintf("startSendingUplink()\n");
}
startSendingUplink(firstTime);
gLed.Set(LedPattern::TwoShort);
uint32_t deepSleepDelay = 10;
for (auto n = deepSleepDelay; n > 0; --n)
{
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 1000)
{
gCatena.poll();
yield();
}
}
} else {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("now going to sleep for 6 minutes...\n");
if (fUsbPower) {
gCatena.SafePrintf("USB Power is on\n");
} else {
gCatena.SafePrintf("USB Power is off\n");
}
//Serial.flush();
}
gLed.Set(LedPattern::Sleeping);
if (!fUsbPower) {
DoDeepSleep();
sleepDoneCb(&sensorJob);
} else {
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb);
}
}
}
last_sensor_reading.vbat = GetVBatValue((int)(vBat * 1000.0f));
last_sensor_reading.weight1 = weight1_current;
last_sensor_reading.weight2 = weight2_current;
last_sensor_reading.weight = weight_current;
last_sensor_reading.temperature = temp_current;
last_sensor_reading.humidity = humidity_current;
last_sensor_reading.pressure = pressure_current;
}
void startSendingUplink(bool firstTime)
{
LedPattern savedLed = gLed.Set(LedPattern::Measuring);
if (savedLed != LedPattern::Joining)
gLed.Set(LedPattern::Sending);
else
gLed.Set(LedPattern::Joining);
bool fConfirmed = false;
if (gCatena.GetOperatingFlags() & (1 << 16)) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("requesting confirmed tx\n");
}
fConfirmed = true;
}
if (firstTime) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer firstTime\n");
}
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
} else {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer not firstTime\n");
}
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
}
ClearLoraData();
}
static void sendBufferDoneCb(
void* pContext,
bool fStatus)
{
osjobcb_t pFn;
gLed.Set(LedPattern::Settling);
pFn = settleDoneCb;
if (! fStatus)
{
if (!gLoRaWAN.IsProvisioned())
{
// we'll talk about it at the callback.
pFn = txNotProvisionedCb;
// but prevent join attempts now.
gLoRaWAN.Shutdown();
}
else if (config_data.debug_level > 0) {
gCatena.SafePrintf("send buffer failed\n");
}
}
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_SETTLE),
pFn
);
}
static void txNotProvisionedCb(
osjob_t *pSendJob
)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n");
}
gLoRaWAN.Shutdown();
gLed.Set(LedPattern::NotProvisioned);
}
static void settleDoneCb(
osjob_t* pSendJob)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("settleDoneCb\n");
}
if (config_data.debug_level > 2) {
// Terry vv
gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay);
gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr);
gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq);
gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset);
// Terry ^^
}
sleepDoneCb(pSendJob);
}
static void sleepDoneCb(osjob_t* pJob)
{
gLed.Set(LedPattern::WarmingUp);
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_WARMUP),
warmupDoneCb);
}
static void warmupDoneCb(osjob_t* pJob)
{
ReadSensors(false, false);
}
static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage, size_t nMessage)
{
union {
byte buf[9];
struct {
byte dummy;
uint32_t gram_A;
uint32_t gram_B;
} value;
} bytearray;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("receiveMessage was called!!!\n");
}
if (config_data.debug_level > 2) {
// Terry vv
if (port == 0)
{
gCatena.SafePrintf("MAC message:");
for (unsigned i = 0; i < LMIC.dataBeg; ++i)
{
gCatena.SafePrintf(" %02x", LMIC.frame[i]);
}
return;
}
gCatena.SafePrintf("Port: %i\n", port);
gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay);
gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr);
gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq);
gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset);
// Terry ^^
}
if (config_data.debug_level > 0) {
gCatena.SafePrintf("message received port(%02x)/length(%x)\n",
port, nMessage
);
// we print out the received message...
gCatena.SafePrintf("Current LMIC.seqnoUp: %d\n",LMIC.seqnoUp);
gCatena.SafePrintf("Received Data (Payload): \n");
for (byte i = 0; i < nMessage; i++) {
gCatena.SafePrintf("%02x", pMessage[i]);
}
gCatena.SafePrintf("\n");
}
if (LMIC.seqnoUp > 2) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("tare with downlink is only possible within first two uplink packets!\n");
return;
}
}
if (port == 1 && nMessage == 1) {
if (pMessage[0] == 0) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("reset both scales to zero\n");
}
ReadSensors(false, true);
config_data.cal_w1_0 = last_sensor_reading.weight1;
config_data.cal_w2_0 = last_sensor_reading.weight2;
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
}
}
if (port == 1 && nMessage == 9) {
if (pMessage[0] == 1) {
memcpy(bytearray.buf, pMessage, 9);
if (config_data.debug_level > 0) {
gCatena.SafePrintf("tare scales, A: %d, B: %d\n", bytearray.value.gram_A, bytearray.value.gram_B);
}
ReadSensors(false, true);
config_data.cal_w1_factor = (((float)last_sensor_reading.weight1 - config_data.cal_w1_0) / bytearray.value.gram_A);
config_data.cal_w2_factor = (((float)last_sensor_reading.weight2 - config_data.cal_w2_0) / bytearray.value.gram_B);
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
}
}
}
/* process "application hello" -- args are ignored */
// argv[0] is "hello"
// argv[1..argc-1] are the (ignored) arguments
cCommandStream::CommandStatus cmdHello(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
pThis->printf("Hello, world!\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdGetCalibrationSettings(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
pThis->printf("{\n");
pThis->printf(" \"cal_w1_0\": \"%d\",\n", config_data.cal_w1_0);
pThis->printf(" \"cal_w2_0\": \"%d\",\n", config_data.cal_w2_0);
pThis->printf(" \"cal_w1_factor\": \"%d.%03d\n", (int)config_data.cal_w1_factor, (int)abs(config_data.cal_w1_factor * 1000) % 1000);
pThis->printf(" \"cal_w2_factor\": \"%d.%03d\n", (int)config_data.cal_w2_factor, (int)abs(config_data.cal_w2_factor * 1000) % 1000);
pThis->printf("}\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdGetSensorReadings(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
ReadSensors(false, true);
pThis->printf("{\n");
pThis->printf(" \"weight\": \"%d\",\n", last_sensor_reading.weight);
pThis->printf(" \"weight1_raw\": \"%d\",\n", last_sensor_reading.weight1);
pThis->printf(" \"weight2_raw\": \"%d\",\n", last_sensor_reading.weight2);
pThis->printf(" \"temperature\": \"%d\",\n", last_sensor_reading.temperature);
pThis->printf(" \"humidity\": \"%d\",\n", last_sensor_reading.humidity);
pThis->printf(" \"pressure\": \"%d\",\n", last_sensor_reading.pressure);
pThis->printf(" \"batt\": \"%d\",\n", last_sensor_reading.vbat);
pThis->printf("}\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdGetScale1(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
pThis->printf("getscale1\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdGetScale2(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
pThis->printf("getscale2\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdCalibrateZeroScaleA(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
ReadSensors(false, true);
config_data.cal_w1_0 = last_sensor_reading.weight1;
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
pThis->printf("{ \"msg\": \"calibrate_zero_scale_a was successful\" }\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdCalibrateZeroScaleB(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
ReadSensors(false, true);
config_data.cal_w2_0 = last_sensor_reading.weight2;
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
pThis->printf("{ \"msg\": \"calibrate_zero_scale_b was successful\" }\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdCalibrateScaleA(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
ReadSensors(false, true);
String w1_gramm(argv[1]);
config_data.cal_w1_factor = (((float)last_sensor_reading.weight1 - config_data.cal_w1_0) / w1_gramm.toFloat());
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
gCatena.SafePrintf("last_sensor_reading.weight1: %ld\n", last_sensor_reading.weight1);
gCatena.SafePrintf("config_data.cal_w1_0: %ld\n", config_data.cal_w1_0);
gCatena.SafePrintf("w1_gramm: %s\n", w1_gramm);
gCatena.SafePrintf("w1_gramm (float): %d\n", (int)w1_gramm.toFloat());
gCatena.SafePrintf("config_data.cal_w1_factor: %d\n", (int)config_data.cal_w1_factor);
pThis->printf("{ \"msg\": \"calibrate_scale_a was successful\" }\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdCalibrateScaleB(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
ReadSensors(false, true);
String w2_gramm(argv[1]);
config_data.cal_w2_factor = (((float)last_sensor_reading.weight2 - config_data.cal_w2_0) / w2_gramm.toFloat());
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
pThis->printf("{ \"msg\": \"calibrate_scale_b was successful\" }\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdSetDebugLevel(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
String s_debug_level(argv[1]);
config_data.debug_level = s_debug_level.toInt();
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
pThis->printf("{ \"msg\": \"set_debug_level was successful\" }\n");
return cCommandStream::CommandStatus::kSuccess;
}
cCommandStream::CommandStatus cmdGetDebugLevel(cCommandStream *pThis, void *pContext, int argc, char **argv)
{
gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
pThis->printf("{ \"msg\": \"debug_level is %d\" }\n", config_data.debug_level);
return cCommandStream::CommandStatus::kSuccess;
}