diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index 75d8dc5..6f63b50 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -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]; + 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 + 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 - gCatena.SafePrintf("Reading Calibration Config from FRAM...\n"); -#endif + 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)); -#ifdef DEBUG - 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("Size of config_data: %d\n", sizeof(config_data)); -#endif + 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. @@ -381,9 +385,9 @@ bool setup_scales(void) bool res; res = true; -#ifdef DEBUG - gCatena.SafePrintf("Setup Scales...\n"); -#endif + if (config_data.debug_level > 0) { + gCatena.SafePrintf("Setup Scales...\n"); + } // Enable Power digitalWrite(D10, HIGH); @@ -401,9 +405,9 @@ bool setup_scales(void) res = false; } -#ifdef DEBUG - gCatena.SafePrintf("Setup Scale is complete\n"); -#endif + if (config_data.debug_level > 0) { + gCatena.SafePrintf("Setup Scale is complete\n"); + } return res; } @@ -413,13 +417,17 @@ void setup_flash(void) if (gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS)) { fFlash = true; gFlash.powerDown(); - gCatena.SafePrintf("FLASH found, but power down\n"); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("FLASH found, but power down\n"); + } } else { fFlash = false; gFlash.end(); gSPI2.end(); - gCatena.SafePrintf("No FLASH found: check hardware\n"); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("No FLASH found: check hardware\n"); + } } } @@ -427,7 +435,9 @@ void setup_uplink(void) { // Do an unjoin, so every reboot will trigger a join - gCatena.SafePrintf("Do an unjoin...\n"); + 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() { - gCatena.SafePrintf("Now going to deep sleep: %d\n", millis()); + 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(); - gCatena.SafePrintf("Done with deep sleep: %d\n", millis()); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("Done with deep sleep: %d\n", millis()); + } } @@ -600,43 +614,43 @@ void ReadSensors(bool firstTime, bool readOnly) // vBat float vBat = gCatena.ReadVbat(); -#ifdef DEBUG - gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f)); -#endif + if (config_data.debug_level > 0) { + gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f)); + } // vBus float vBus = gCatena.ReadVbus(); -#ifdef DEBUG - gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f)); -#endif + if (config_data.debug_level > 0) { + gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f)); + } fUsbPower = (vBus > 3.0) ? true : false; // Setup Scales - //setup_scales(); // Read Scales - //if (LoadCell.is_ready()) { if (setup_scales()) { -#ifdef DEBUG - gCatena.SafePrintf("HX711 LoadCell is ready.\n"); -#endif + 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; -#ifdef DEBUG - gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1); - gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current); -#endif + 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; -#ifdef DEBUG - gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2); - gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current); -#endif + 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 { - gCatena.SafePrintf("HX711 LoadCell not ready.\n"); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("HX711 LoadCell not ready.\n"); + } } // Disable Power @@ -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 - gCatena.SafePrintf( - "BME280: T: %d P: %d RH: %d\n", - (int)m.Temperature, - (int)m.Pressure, - (int)m.Humidity); -#endif + 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); -#ifdef DEBUG - gCatena.SafePrintf("pressure_current: %d\n", pressure_current); -#endif + if (config_data.debug_level > 0) { + gCatena.SafePrintf("pressure_current: %d\n", pressure_current); + } } if (not(readOnly)) { @@ -708,7 +722,9 @@ void ReadSensors(bool firstTime, bool readOnly) timer_pos0 = millis(); } - ShowLORAData(firstTime); + 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); - gCatena.SafePrintf("startSendingUplink()\n"); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("startSendingUplink()\n"); + } startSendingUplink(firstTime); } else { - gCatena.SafePrintf("now going to sleep for 6 minutes...\n"); - //Serial.flush(); + 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 - gCatena.SafePrintf("requesting confirmed tx\n"); -#endif + if (config_data.debug_level > 0) { + gCatena.SafePrintf("requesting confirmed tx\n"); + } fConfirmed = true; } if (firstTime) { -#ifdef DEBUG - gCatena.SafePrintf("SendBuffer firstTime\n"); -#endif + 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 { -#ifdef DEBUG - gCatena.SafePrintf("SendBuffer not firstTime\n"); -#endif + 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(); @@ -794,7 +819,9 @@ static void sendBufferDoneCb( gLoRaWAN.Shutdown(); } else - gCatena.SafePrintf("send buffer failed\n"); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("send buffer failed\n"); + } } os_setTimedCallback( @@ -808,7 +835,9 @@ static void txNotProvisionedCb( osjob_t *pSendJob ) { - gCatena.SafePrintf("LoRaWAN not provisioned yet. Use the commands to set it up.\n"); + 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 - gCatena.SafePrintf("settleDoneCb\n"); -#endif - // 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 ^^ + 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); } @@ -852,42 +882,48 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage unsigned txCycle; unsigned txCount; -#ifdef DEBUG - gCatena.SafePrintf("receiveMessage was called!!!\n"); -#endif - // Terry vv - if (port == 0) - { - gCatena.SafePrintf("MAC message:"); - for (unsigned i = 0; i < LMIC.dataBeg; ++i) - { - gCatena.SafePrintf(" %02x", LMIC.frame[i]); - } - return; + 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("receiveMessage was called!!!!n"); - gCatena.SafePrintf("Port: %s\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 ^^ + gCatena.SafePrintf("receiveMessage was called!!!!n"); + gCatena.SafePrintf("Port: %s\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 (! (port == 1 && 2 <= nMessage && nMessage <= 3)) { - gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n", - port, nMessage - ); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n", + port, nMessage + ); + } return; } - + txCycle = (pMessage[0] << 8) | pMessage[1]; if (txCycle < CATCFG_T_MIN || txCycle > CATCFG_T_MAX) { - gCatena.SafePrintf("tx cycle time out of range: %u\n", txCycle); + if (config_data.debug_level > 0) { + gCatena.SafePrintf("tx cycle time out of range: %u\n", txCycle); + } return; } @@ -900,27 +936,31 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage } // we print out the received message... - gCatena.SafePrintf("Received Data (Payload): \n"); - for (byte i = 0; i < nMessage; i++) { - gCatena.SafePrintf("%c", pMessage[i]); + 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"); } - gCatena.SafePrintf("\n"); - + setTxCycleTime(txCycle, txCount); } void setTxCycleTime(unsigned txCycle, unsigned txCount) { - if (txCount > 0) - gCatena.SafePrintf( - "message cycle time %u seconds for %u messages\n", - txCycle, txCount - ); - else - gCatena.SafePrintf( - "message cycle time %u seconds indefinitely\n", - txCycle - ); + if (config_data.debug_level > 2) { + if (txCount > 0) + gCatena.SafePrintf( + "message cycle time %u seconds for %u messages\n", + txCycle, txCount + ); + else + gCatena.SafePrintf( + "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; +}