make debug level configurable in FRAM

This commit is contained in:
Joerg Lehmann 2019-09-13 14:38:16 +02:00
parent eb419b016a
commit 754404a771
1 changed files with 186 additions and 126 deletions

View File

@ -29,8 +29,6 @@
using namespace McciCatena;
#define DEBUG
/****************************************************************************\
|
| MANIFEST CONSTANTS & TYPEDEFS
@ -103,6 +101,8 @@ 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[] =
@ -114,6 +114,8 @@ static const cCommandStream::cEntry sMyExtraCommmands[] =
{ "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....
};
@ -146,17 +148,18 @@ static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 10; // when weight value dr
|
\****************************************************************************/
// must be 65 bytes long...
// 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,
float cal_w2_factor;
byte fill[123];
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
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
@ -212,7 +215,6 @@ Catena::LoRaWAN gLoRaWAN;
//
// the LED
//
StatusLed gLed(Catena::PIN_STATUS_LED);
// The temperature/humidity sensor
@ -267,18 +269,20 @@ void setup_platform(void)
);
// read config_data from fram...
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Reading Calibration Config from FRAM...\n");
#endif
}
gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data));
#ifdef DEBUG
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));
#endif
}
#ifdef USBCON
// if running unattended, don't wait for USB connect.
@ -381,9 +385,9 @@ bool setup_scales(void)
bool res;
res = true;
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Setup Scales...\n");
#endif
}
// Enable Power
digitalWrite(D10, HIGH);
@ -401,9 +405,9 @@ bool setup_scales(void)
res = false;
}
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Setup Scale is complete\n");
#endif
}
return res;
}
@ -413,21 +417,27 @@ 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 */
@ -563,7 +573,9 @@ uint8_t GetVBatValue(int millivolts)
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);
@ -583,7 +595,9 @@ void DoDeepSleep()
SPI.begin();
if (fFlash)
gSPI2.begin();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Done with deep sleep: %d\n", millis());
}
}
@ -600,44 +614,44 @@ void ReadSensors(bool firstTime, bool readOnly)
// vBat
float vBat = gCatena.ReadVbat();
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f));
#endif
}
// vBus
float vBus = gCatena.ReadVbus();
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
#endif
}
fUsbPower = (vBus > 3.0) ? true : false;
// Setup Scales
//setup_scales();
// Read Scales
//if (LoadCell.is_ready()) {
if (setup_scales()) {
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("HX711 LoadCell is ready.\n");
#endif
}
LoadCell.set_gain(128);
long w1 = LoadCell.read_average(5);
weight1_current = (int32_t)w1;
#ifdef DEBUG
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);
#endif
}
LoadCell.set_gain(32);
long w2 = LoadCell.read_average(5);
weight2_current = (int32_t)w2;
#ifdef DEBUG
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);
#endif
}
}
else {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("HX711 LoadCell not ready.\n");
}
}
// Disable Power
digitalWrite(D10, LOW);
@ -659,19 +673,19 @@ void ReadSensors(bool firstTime, bool readOnly)
// 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.
#ifdef DEBUG
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);
#endif
}
temp_current = (int16_t)((m.Temperature) * 10);
humidity_current = (uint8_t)m.Humidity;
pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET);
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("pressure_current: %d\n", pressure_current);
#endif
}
}
if (not(readOnly)) {
@ -708,7 +722,9 @@ void ReadSensors(bool firstTime, bool readOnly)
timer_pos0 = millis();
}
if (config_data.debug_level > 0) {
ShowLORAData(firstTime);
}
my_position++;
// Should we send the Data?
@ -717,11 +733,20 @@ void ReadSensors(bool firstTime, bool readOnly)
// 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);
} 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);
os_setTimedCallback(
&sensorJob,
@ -754,21 +779,21 @@ void startSendingUplink(bool firstTime)
bool fConfirmed = false;
if (gCatena.GetOperatingFlags() & (1 << 16)) {
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("requesting confirmed tx\n");
#endif
}
fConfirmed = true;
}
if (firstTime) {
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer firstTime\n");
#endif
}
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
} else {
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer not firstTime\n");
#endif
}
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
}
ClearLoraData();
@ -794,8 +819,10 @@ static void sendBufferDoneCb(
gLoRaWAN.Shutdown();
}
else
if (config_data.debug_level > 0) {
gCatena.SafePrintf("send buffer failed\n");
}
}
os_setTimedCallback(
&sensorJob,
@ -808,7 +835,9 @@ 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);
}
@ -817,17 +846,18 @@ static void txNotProvisionedCb(
static void settleDoneCb(
osjob_t* pSendJob)
{
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("settleDoneCb\n");
#endif
}
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);
}
@ -852,9 +882,11 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
unsigned txCycle;
unsigned txCount;
#ifdef DEBUG
if (config_data.debug_level > 0) {
gCatena.SafePrintf("receiveMessage was called!!!\n");
#endif
}
if (config_data.debug_level > 2) {
// Terry vv
if (port == 0)
{
@ -872,14 +904,16 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
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 (! (port == 1 && 2 <= nMessage && nMessage <= 3))
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n",
port, nMessage
);
}
return;
}
@ -887,7 +921,9 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
if (txCycle < CATCFG_T_MIN || txCycle > CATCFG_T_MAX)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("tx cycle time out of range: %u\n", txCycle);
}
return;
}
@ -900,17 +936,20 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
}
// we print out the received message...
if (config_data.debug_level > 2) {
gCatena.SafePrintf("Received Data (Payload): \n");
for (byte i = 0; i < nMessage; i++) {
gCatena.SafePrintf("%c", pMessage[i]);
}
gCatena.SafePrintf("\n");
}
setTxCycleTime(txCycle, txCount);
}
void setTxCycleTime(unsigned txCycle, unsigned txCount)
{
if (config_data.debug_level > 2) {
if (txCount > 0)
gCatena.SafePrintf(
"message cycle time %u seconds for %u messages\n",
@ -921,6 +960,7 @@ void setTxCycleTime(unsigned txCycle, unsigned txCount)
"message cycle time %u seconds indefinitely\n",
txCycle
);
}
gTxCycle = txCycle;
gTxCycleCount = txCount;
@ -1035,3 +1075,23 @@ cCommandStream::CommandStatus cmdCalibrateScaleB(cCommandStream *pThis, void *pC
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;
}