From 34d8c922817392b70e6d79570f8c751b7fac70ef Mon Sep 17 00:00:00 2001 From: seiichiro Date: Mon, 3 May 2021 16:35:37 +0000 Subject: [PATCH] Rework Debugging Macros to also Work in Libraries (#6) Co-authored-by: Stefan Brand Reviewed-on: https://www.seiichiro0185.org/git/attnode/v3_firmware/pulls/6 Co-authored-by: seiichiro Co-committed-by: seiichiro --- README.md | 9 +++++++++ include/debug.h | 2 -- lib/BME280/BME280.cpp | 3 +++ lib/BME280/BME280.h | 1 + lib/DS18B20/DS18B20.cpp | 3 +++ lib/DS18B20/DS18B20.h | 1 + lib/HM330x/HM330x.cpp | 4 ++++ lib/HM330x/HM330x.h | 1 + lib/SCD30/SCD30.cpp | 5 +++++ lib/SCD30/SCD30.h | 1 + lib/SHT21/SHT21.cpp | 2 ++ lib/SHT21/SHT21.h | 1 + platformio.ini | 9 ++++++++- src/config.h.example | 9 --------- src/main.cpp | 9 ++++++--- 15 files changed, 45 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8850007..44f95e9 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,15 @@ Before programming a node, copy src/config.h.example to src/config.h and set the Programming is done using a [MicroUPDI Programmer](https://github.com/MCUdude/microUPDI), settings in platformio.ini are set to use it. For other pogrammer options see the PlatformIO Documentation +## Debugging + +The firmware has the ability to produce some debug output via the Serial UART as long as there is no sensor using the UART. To enable it, uncomment the line -D DEBUG in platformio.ini. This will produce some debug output showing the state of the node. The following macros are available as a replacement for the normal Serial.print and Serial.println functions: + + DEBUG_PRINT("Debug Output"); + DEBUG_PRINTLN("Debug Output with Linebreak"); + +These will only produce additional code in the firmware when the DEBUG-Flag is enabled, and will be entirely removed in the ouput binary if not. The macros work like the normal Serial.print* statements form the standard arduino functions. + ## Payload Decoder You need to specify a Payload Decoder fitting for your configured Sensors for a Node. See payload/index.html in this repository. Open it in your Browser of Choice and select the enabled sensors. It will generate the Payload Decoder fitting for the choosen Sensors. You can also use the Online Version at [attno.de](https://www.attno.de/payload-generator) diff --git a/include/debug.h b/include/debug.h index f7fbaad..593deb3 100644 --- a/include/debug.h +++ b/include/debug.h @@ -5,8 +5,6 @@ DebugUtils.h - Simple debugging utilities. #ifndef DEBUGUTILS_H #define DEBUGUTILS_H -#include "config.h" - #ifdef DEBUG #define DEBUG_PRINT(...) Serial.print(__VA_ARGS__); Serial.flush(); #define DEBUG_PRINTLN(...) Serial.println(__VA_ARGS__); Serial.flush(); diff --git a/lib/BME280/BME280.cpp b/lib/BME280/BME280.cpp index b2485dd..ba7d564 100644 --- a/lib/BME280/BME280.cpp +++ b/lib/BME280/BME280.cpp @@ -5,6 +5,7 @@ BME280::BME280() {} void BME280::getCalData() { + DEBUG_PRINTLN("BME280::getCalData"); dig_T1 = read16_LE(0x88); dig_T2 = readS16_LE(0x8A); dig_T3 = readS16_LE(0x8C); @@ -82,6 +83,8 @@ uint8_t BME280::getSensorData(char *payload, uint8_t startbyte) { int32_t UP, UT, UH; int32_t rawP, rawT; + DEBUG_PRINTLN("BME280::geSensorData"); + // Trigger Measurement // Set Sensor Config write8(0xF2, 0b00000001); // 1x Oversampling for Humidity diff --git a/lib/BME280/BME280.h b/lib/BME280/BME280.h index e20fcdc..391147d 100644 --- a/lib/BME280/BME280.h +++ b/lib/BME280/BME280.h @@ -2,6 +2,7 @@ #define BME280_H #include "../../include/attsensor.h" +#include "../../include/debug.h" #define BME280_I2CADDR 0x76 diff --git a/lib/DS18B20/DS18B20.cpp b/lib/DS18B20/DS18B20.cpp index c413afd..098d5be 100644 --- a/lib/DS18B20/DS18B20.cpp +++ b/lib/DS18B20/DS18B20.cpp @@ -50,6 +50,7 @@ DS18B20::DS18B20(uint8_t owpin, uint8_t resbits = 12, bool para = false) { } void DS18B20::initialize(void){ + DEBUG_PRINTLN("DS18B20::initialize"); onewire->reset(); onewire->reset_search(); sensorcount = 0; @@ -84,6 +85,8 @@ uint8_t DS18B20::getSensorData(char *payload, uint8_t startbyte){ uint8_t data[9]; int16_t value; + DEBUG_PRINTLN("DS18B20::getSensorData"); + // Start Conversation on all Sensors onewire->reset(); onewire->skip(); diff --git a/lib/DS18B20/DS18B20.h b/lib/DS18B20/DS18B20.h index 3d5d58f..eff70ac 100644 --- a/lib/DS18B20/DS18B20.h +++ b/lib/DS18B20/DS18B20.h @@ -28,6 +28,7 @@ #define DS18B20_H #include "../../include/attsensor.h" +#include "../../include/debug.h" #include // Sensor Resolutions diff --git a/lib/HM330x/HM330x.cpp b/lib/HM330x/HM330x.cpp index 0dadf6d..7e6e0ef 100644 --- a/lib/HM330x/HM330x.cpp +++ b/lib/HM330x/HM330x.cpp @@ -35,6 +35,8 @@ HM330x::HM330x() {}; // Initialize the Sensor void HM330x::initialize(void) { uint8_t retryCount = 0; + + DEBUG_PRINTLN("HM330x::initialize"); // Send select command while (!sendCmd(HM330x_SELECT)) { if (retryCount++ >= 0) { @@ -50,6 +52,8 @@ uint8_t HM330x::getSensorData(char *payload, uint8_t startbyte) { uint8_t data[HM330x_DATA_LEN] = {0}; uint16_t value = 0; + DEBUG_PRINTLN("HM330x::getSensorData"); + // Initialize Payload with 0s for (uint8_t i=startbyte; i < startbyte+6; i++) payload[i] = 0; diff --git a/lib/HM330x/HM330x.h b/lib/HM330x/HM330x.h index 19aad3c..28b89d1 100644 --- a/lib/HM330x/HM330x.h +++ b/lib/HM330x/HM330x.h @@ -28,6 +28,7 @@ #define HM330x_H #include "../../include/attsensor.h" +#include "../../include/debug.h" // Sensor I2C Address #define HM330x_ADDR 0x40 diff --git a/lib/SCD30/SCD30.cpp b/lib/SCD30/SCD30.cpp index be2612d..e0d84bc 100644 --- a/lib/SCD30/SCD30.cpp +++ b/lib/SCD30/SCD30.cpp @@ -41,6 +41,8 @@ SCD30::SCD30(uint8_t interval, bool selfcalib) { // Initialize the Sensor, Enable Constant Measurement, Set Autocalibration and Interval void SCD30::initialize(void) { uint8_t fw[3]; + + DEBUG_PRINTLN("SCD30::initialize"); // Soft Reset reset(); // Send Get Firmware, Workaround to Avoid First Real Command to be Ignored @@ -63,6 +65,9 @@ void SCD30::initialize(void) { uint8_t SCD30::getSensorData(char *payload, uint8_t startbyte) { uint8_t ready[2] = {0}; uint8_t data[18] = {0}; + + DEBUG_PRINTLN("SCD30::getSensorData"); + // Check if Sensor Data is Available getBytes(SCD30_DATA_READY, ready, 2); if (ready[1] == 1){ diff --git a/lib/SCD30/SCD30.h b/lib/SCD30/SCD30.h index 3cfea3c..3c0d359 100644 --- a/lib/SCD30/SCD30.h +++ b/lib/SCD30/SCD30.h @@ -28,6 +28,7 @@ #define SCD30_H #include "../../include/attsensor.h" +#include "../../include/debug.h" // Sensor I2C Address #define SCD30_ADDR 0x61 diff --git a/lib/SHT21/SHT21.cpp b/lib/SHT21/SHT21.cpp index 1ba31e0..b913289 100644 --- a/lib/SHT21/SHT21.cpp +++ b/lib/SHT21/SHT21.cpp @@ -53,6 +53,8 @@ uint16_t SHT21::sensorRead(uint8_t command) { } uint8_t SHT21::getSensorData(char *payload, uint8_t startbyte) { + DEBUG_PRINTLN("SHT21::getSensorData"); + // Temperature int32ToPayload((int32_t)((-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)))*100), payload, startbyte); // Humidity diff --git a/lib/SHT21/SHT21.h b/lib/SHT21/SHT21.h index 8755898..70ffe4e 100644 --- a/lib/SHT21/SHT21.h +++ b/lib/SHT21/SHT21.h @@ -28,6 +28,7 @@ #define SHT21_H #include "../../include/attsensor.h" +#include "../../include/debug.h" #define SHT21_I2CADDR 0x40 diff --git a/platformio.ini b/platformio.ini index 841bb45..4f5942a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,13 +24,20 @@ board_hardware.bod = disabled monitor_speed = 115200 monitor_port = /dev/ttyACM1 -## LMIC Config via Build Flags ## +## LMIC Config and Debug Mode via Build Flags ## +#************************************************************************************************************************** +# Uncomment -D DEBUG to enable Serial Debugging. Parameters for the Serial Port are 115200 Baud 8n1 +# By default the Firmware will output some messages about LoRa-Status and indicate sending +# The Macros DEBUG_PRINT() and DEBUG_PRINTLN() can be used to produce debug output depending on this define +# Please be aware that this will not work when a Serial Sensor like the SG112A/MH-Z19C or Sensair S8 ist used +#*************************************************************************************************************************/ build_flags = -D CFG_eu868 -D CFG_sx1276_radio -D DISABLE_PING -D DISABLE_BEACONS -D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS +# -D DEBUG ## Programmer Config (MicroUPDI) ## upload_port = usb diff --git a/src/config.h.example b/src/config.h.example index 09ff36d..a939ade 100644 --- a/src/config.h.example +++ b/src/config.h.example @@ -26,15 +26,6 @@ *************************************************************************************************************************/ // #define BTN_PIN PIN_PC2 -/************************************************************************************************************************** - * Enable Serial Debugging. Parameters for the Serial Port are 115200 Baud 8n1 - * By default the Firmware will output some messages about LoRa-Status and indicate sending - * The Macros DEBUG_PRINT() and DEBUG_PRINTLN() can be used to produce debug output depending on this define - * Please be aware that this will not work when a Serial Sensor like the SG112A/MH-Z19C or Sensair S8 ist used - *************************************************************************************************************************/ -// #define DEBUG - - /************************************************************************************************************************** * Number of active Sensors (used as long as HAS_NO_SENSOR is not enabled) * Set to the correct number of enabled sensors below diff --git a/src/main.cpp b/src/main.cpp index 6aadad0..c209ab2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,8 +13,8 @@ // Include Config and Helpers #include "config.h" -#include "debug.h" -#include "attsensor.h" +#include +#include // Include All Sensors Activated in config.h #ifndef HAS_NO_SENSOR @@ -189,11 +189,14 @@ void onEvent(ev_t ev) { } // Got to sleep for specified Time - DEBUG_PRINTLN("Going to Sleep"); + DEBUG_PRINT("Going to Sleep for "); + DEBUG_PRINT(sleep_time*64); + DEBUG_PRINTLN(" Seconds"); for (uint16_t i = 0; i < sleep_time*2; i++) { // Cancel sleep Cycle if Button was Pressed #ifdef BTN_PIN if (btn_pressed && digitalRead(BTN_PIN) == HIGH) { + DEBUG_PRINTLN("Button Pressed, waking up"); i = sleep_time*2; btn_pressed = 0; } else {