From c25135f626d876ba6e0313439090f47bcbfcaae3 Mon Sep 17 00:00:00 2001 From: Matthias Prinke <83612361+matthias-bs@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:37:16 +0100 Subject: [PATCH] 20241227 refactoring (#121) * Modified radio chip selection * Moved uplinkDelay() from BresserWeatherSensorLWCmd.cpp to BresserWeatherSensorLW.ino * Disabled m5stack_core2 due to iram linker segment overflow --- .github/workflows/CI.yml | 2 +- BresserWeatherSensorLW.ino | 34 ++++++++++++++++++---- BresserWeatherSensorLWCmd.cpp | 17 ++--------- BresserWeatherSensorLWCmd.h | 3 +- config.h | 55 +++++++++++++++++++++-------------- 5 files changed, 66 insertions(+), 45 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index dbf4eeb..e09ea00 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -23,7 +23,7 @@ jobs: #- esp32:esp32:heltec_wifi_lora_32_V2 - esp32:esp32:heltec_wifi_lora_32_V3 #- esp32:esp32:featheresp32 - - esp32:esp32:m5stack_core2 + #- esp32:esp32:m5stack_core2 - esp32:esp32:esp32s3_powerfeather - esp32:esp32:adafruit_feather_esp32s2 - rp2040:rp2040:adafruit_feather:dbgport=Serial diff --git a/BresserWeatherSensorLW.ino b/BresserWeatherSensorLW.ino index 7662389..559a3a7 100644 --- a/BresserWeatherSensorLW.ino +++ b/BresserWeatherSensorLW.ino @@ -108,6 +108,7 @@ // 20241203 Added supply voltage measurement if PIN_SUPPLY_IN is defined // Moved start of sensor reception after battery voltage check // Modified sleep duration if battery is low but external power is available +// 20241227 Moved uplinkDelay() from BresserWeatherSensorLWCmd.cpp // // ToDo: // - @@ -269,6 +270,22 @@ void print_wakeup_reason() } #endif +void uplinkDelay(uint32_t timeUntilUplink, uint32_t uplinkInterval) +{ + // wait before sending uplink + uint32_t minimumDelay = uplinkInterval * 1000UL; + //uint32_t interval = node.timeUntilUplink(); // calculate minimum duty cycle delay (per FUP & law!) + uint32_t delayMs = max(timeUntilUplink, minimumDelay); // cannot send faster than duty cycle allows + + log_d("Sending uplink in %u s", delayMs / 1000); + #if defined(ESP32) + esp_sleep_enable_timer_wakeup(delayMs * 1000); + esp_light_sleep_start(); + #else + delay(delayMs); + #endif +} + /*! * \brief Compute sleep duration * @@ -399,7 +416,7 @@ void printDateTime(void) * * \return RADIOLIB_LORAWAN_NEW_SESSION or RADIOLIB_LORAWAN_SESSION_RESTORED */ -int16_t lwActivate(void) +int16_t lwActivate(LoRaWANNode &node) { // setup the OTAA session information #if defined(LORAWAN_VERSION_1_1) @@ -508,6 +525,7 @@ void setup() #endif Serial.begin(115200); + Serial.setDebugOutput(true); delay(2000); // give time to switch to the serial monitor log_i("Setup"); @@ -628,14 +646,15 @@ void setup() int16_t state = 0; // return value for calls to RadioLib + // setup the radio based on the pinmap (connections) in config.h log_v("Initalise radio"); - radio.reset(); + state = radio.begin(); debug(state != RADIOLIB_ERR_NONE, "Initalise radio failed", state, true); // activate node by restoring session or otherwise joining the network - state = lwActivate(); + state = lwActivate(node); // state is one of RADIOLIB_LORAWAN_NEW_SESSION or RADIOLIB_LORAWAN_SESSION_RESTORED // Set battery fill level - @@ -738,20 +757,23 @@ void setup() { log_d("Sending response uplink."); fPort = uplinkReq; - encodeCfgUplink(fPort, uplinkPayload, payloadSize, uplinkIntervalSeconds); + encodeCfgUplink(fPort, uplinkPayload, payloadSize); + uplinkDelay(node.timeUntilUplink(), uplinkIntervalSeconds); } else if (fsmStage == E_FSM_STAGE::E_LWSTATUS) { log_d("Sending LoRaWAN status uplink."); fPort = CMD_GET_LW_STATUS; - encodeCfgUplink(fPort, uplinkPayload, payloadSize, uplinkIntervalSeconds); + encodeCfgUplink(fPort, uplinkPayload, payloadSize); + uplinkDelay(node.timeUntilUplink(), uplinkIntervalSeconds); lwStatusUplinkPending = false; } else if (fsmStage == E_FSM_STAGE::E_APPSTATUS) { log_d("Sending application status uplink."); fPort = CMD_GET_SENSORS_STAT; - encodeCfgUplink(fPort, uplinkPayload, payloadSize, uplinkIntervalSeconds); + encodeCfgUplink(fPort, uplinkPayload, payloadSize); + uplinkDelay(node.timeUntilUplink(), uplinkIntervalSeconds); appStatusUplinkPending = false; } diff --git a/BresserWeatherSensorLWCmd.cpp b/BresserWeatherSensorLWCmd.cpp index e3806c0..9eb7432 100644 --- a/BresserWeatherSensorLWCmd.cpp +++ b/BresserWeatherSensorLWCmd.cpp @@ -39,6 +39,7 @@ // 20240818 Replaced delay() with light sleep for ESP32 // 20240829 Added missing implementation of CMD_SET_LW_STATUS_INTERVAL // 20240920 Changed sendCfgUplink() to encodeCfgUplink() +// 20241227 Removed delay from encodeCfgUplink() // // ToDo: // - @@ -61,8 +62,6 @@ using namespace PowerFeather; * From config.h */ void debug(bool isFail, const char* message, int state, bool Freeze); -extern LoRaWANNode node; -extern const uint16_t uplinkIntervalSeconds; /* @@ -164,7 +163,7 @@ uint8_t decodeDownlink(uint8_t port, uint8_t *payload, size_t size) // Encode configuration/status uplink -void encodeCfgUplink(uint8_t port, uint8_t *uplinkPayload, uint8_t &payloadSize, uint32_t uplinkInterval) +void encodeCfgUplink(uint8_t port, uint8_t *uplinkPayload, uint8_t &payloadSize) { log_d("--- Uplink Configuration/Status ---"); @@ -304,17 +303,5 @@ void encodeCfgUplink(uint8_t port, uint8_t *uplinkPayload, uint8_t &payloadSize, } Serial.println(); - // wait before sending uplink - uint32_t minimumDelay = uplinkInterval * 1000UL; - uint32_t interval = node.timeUntilUplink(); // calculate minimum duty cycle delay (per FUP & law!) - uint32_t delayMs = max(interval, minimumDelay); // cannot send faster than duty cycle allows - - log_d("Sending uplink in %u s", delayMs / 1000); - #if defined(ESP32) - esp_sleep_enable_timer_wakeup(delayMs * 1000); - esp_light_sleep_start(); - #else - delay(delayMs); - #endif payloadSize = encoder.getLength(); } \ No newline at end of file diff --git a/BresserWeatherSensorLWCmd.h b/BresserWeatherSensorLWCmd.h index b83750f..b3f376a 100644 --- a/BresserWeatherSensorLWCmd.h +++ b/BresserWeatherSensorLWCmd.h @@ -46,6 +46,7 @@ // 20240722 Added CMD_SET_LW_STATUS_INTERVAL, renamed // CMD__STATUS_INTERVAL to CMD__APP_STATUS_INTERVAL // 20240920 Changed sendCfgUplink() to encodeCfgUplink() +// 20241227 Removed delay from encodeCfgUplink() // // ToDo: // - @@ -477,6 +478,6 @@ uint8_t decodeDownlink(uint8_t port, uint8_t *payload, size_t size); * \param payloadSize uplink payload size in bytes * \param uplinkInterval uplink interval in seconds */ -void encodeCfgUplink(uint8_t port, uint8_t *uplinkPayload, uint8_t &payloadSize, uint32_t uplinkInterval); +void encodeCfgUplink(uint8_t port, uint8_t *uplinkPayload, uint8_t &payloadSize); #endif \ No newline at end of file diff --git a/config.h b/config.h index a651720..a35af77 100644 --- a/config.h +++ b/config.h @@ -43,6 +43,7 @@ // 20240710 Fixed pragma messages fro Firebeetle ESP32 pin config // 20240922 Bumped to RadioLib v7.0.0 // 20240928 Modified for LoRaWAN v1.0.4 (requires no nwkKey) +// 20241227 Modified radio chip selection // // ToDo: // - @@ -107,7 +108,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #pragma message("NOT TESTED!!!") #pragma message("ARDUINO_FEATHER_ESP32/ARDUINO_THINGPULSE_EPULSE_FEATHER defined; assuming RFM95W FeatherWing will be used") #pragma message("Required wiring: A to RST, B to DIO1, D to DIO0, E to CS") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) // Use pinning for Adafruit Feather ESP32S2 with RFM95W "FeatherWing" ADA3232 @@ -119,7 +120,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #pragma message("NOT TESTED!!!") #pragma message("ARDUINO_ADAFRUIT_FEATHER_ESP32S2 defined; assuming RFM95W FeatherWing will be used") #pragma message("Required wiring: A to RST, B to DIO1, D to DIO0, E to CS") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) #define PIN_LORA_NSS 14 @@ -130,7 +131,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #pragma message("NOT TESTED!!!") #pragma message("ARDUINO_ADAFRUIT_FEATHER_ESP32_V2 defined; assuming RFM95W FeatherWing will be used") #pragma message("Required wiring: A to RST, B to DIO1, D to DIO0, E to CS") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_ESP32S3_POWERFEATHER) #define PIN_LORA_NSS 15 @@ -140,7 +141,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_DIO2 RADIOLIB_NC #pragma message("ARDUINO_ESP32S3_POWERFEATHER defined; assuming RFM95W FeatherWing will be used") #pragma message("Required wiring: A to RST, B to DIO1, D to DIO0, E to CS") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) // Use pinning for Adafruit Feather RP2040 with RFM95W "FeatherWing" ADA3232 @@ -150,7 +151,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_GPIO 10 #pragma message("ARDUINO_ADAFRUIT_FEATHER_RP2040 defined; assuming RFM95W FeatherWing will be used") #pragma message("Required wiring: A to RST, B to DIO1, D to DIO0, E to CS") - #define LORA_CHIP SX1276 + #define USE_SX1276 // LilyGo #elif defined(ARDUINO_TTGO_LORA32_V1) || defined(ARDUINO_TTGO_LoRa32_V1) @@ -164,7 +165,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_GPIO 33 #define PIN_LORA_DIO2 RADIOLIB_NC #pragma message ("TTGO LoRa32 v1 - no Display") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_TTGO_LORA32_V2) || defined(ARDUINO_TTGO_LoRa32_V2) // https://github.com/espressif/arduino-esp32/blob/master/variants/ttgo-lora32-v2/pins_arduino.h @@ -173,7 +174,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_IRQ LORA_IRQ #define PIN_LORA_GPIO RADIOLIB_NC #define PIN_LORA_DIO2 RADIOLIB_NC - #define LORA_CHIP SX1276 + #define USE_SX1276 #pragma message ("TTGO_LoRa32_V2 + Display") #elif defined(ARDUINO_TTGO_LoRa32_v21new) @@ -184,26 +185,26 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_GPIO LORA_D1 #define PIN_LORA_DIO2 RADIOLIB_NC #pragma message ("Using TTGO LoRa32 v2.1 marked T3_V1.6.1 + Display") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_TBEAM_USE_RADIO_SX1262) #pragma error ("ARDUINO_TBEAM_USE_RADIO_SX1262 awaiting pin map") - #define LORA_CHIP SX1262 + #define USE_SX1262 #elif defined(ARDUINO_TBEAM_USE_RADIO_SX1276) #pragma error ("ARDUINO_TBEAM_USE_RADIO_SX1276 awaiting pin map") - #define LORA_CHIP SX1276 + #define USE_SX1276 // AZ-Delivery #elif defined(ARDUINO_D1_MINI32) - // ESP32-WROOM-32 + // ESP32-WROOM-32 #define PIN_LORA_NSS 27 #define PIN_LORA_RST 32 #define PIN_LORA_IRQ 21 #define PIN_LORA_GPIO 33 #define PIN_LORA_DIO2 RADIOLIB_NC #pragma message("wemos_d1_mini32 - WEMOS D1 MINI ESP32 defined; assuming RFM95W will be used") - #define LORA_CHIP SX1276 + #define USE_SX1276 // Heltec #elif defined(ARDUINO_HELTEC_WIFI_LORA_32) @@ -217,7 +218,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_GPIO DIO1 #define PIN_LORA_DIO2 DIO2 #pragma message("ARDUINO_HELTEC_WIFI_LORA_32_V2") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_HELTEC_WIFI_LORA_32_V3) || defined(ARDUINO_heltec_wifi_lora_32_V3) // Use pinning for Heltec WiFi LoRa32 V3 @@ -226,7 +227,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_IRQ DIO0 #define PIN_LORA_GPIO BUSY_LoRa #pragma message("ARDUINO_HELTEC_WIFI_LORA_32_V3") - #define LORA_CHIP SX1262 + #define USE_SX1262 #elif defined(ARDUINO_HELTEC_WIRELESS_STICK) // https://github.com/espressif/arduino-esp32/blob/master/variants/heltec_wireless_stick/pins_arduino.h @@ -236,33 +237,33 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_GPIO DIO1 #define PIN_LORA_DIO2 DIO2 #pragma message("ARDUINO_HELTEC_WIRELESS_STICK") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_HELTEC_WIRELESS_STICK_V3) - https://github.com/espressif/arduino-esp32/tree/master/variants/heltec_wireless_stick_v3 + // https://github.com/espressif/arduino-esp32/tree/master/variants/heltec_wireless_stick_v3 #define PIN_LORA_NSS SS #define PIN_LORA_RST RST_LoRa #define PIN_LORA_IRQ DIO0 #define PIN_LORA_GPIO DIO1 #define PIN_LORA_DIO2 DIO2 #pragma message("ARDUINO_HELTEC_WIRELESS_STICK_V3") - #define LORA_CHIP SX1262 + #define USE_SX1262 // #elif defined(ARDUINO_heltec_wifi_kit_32_V2) // Presumably no LoRa chip // #pragma message ("ARDUINO_heltec_wifi_kit_32_V2 awaiting pin map") -// #define LORA_CHIP USE_SX1276 +// #define USE_SX1276 // SX1276 radio = new Module(18, 26, 14, 35); // #elif defined(ARDUINO_heltec_wifi_kit_32_V3) // Presumably no LoRa chip // #pragma message ("Using Heltec WiFi LoRa32 v3 - Display + USB-C") -// #define LORA_CHIP USE_SX1262 +// #define USE_SX1262 // SX1262 radio = new Module(8, 14, 12, 13); #elif defined(ARDUINO_CUBECELL_BOARD) #pragma error ("ARDUINO_CUBECELL_BOARD awaiting pin map") - #define LORA_CHIP SX1262 + #define USE_SX1262 //SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE); #elif defined(ARDUINO_CUBECELL_BOARD_V2) @@ -278,7 +279,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #define PIN_LORA_GPIO RADIOLIB_NC //#define PIN_LORA_GPIO 35 // manual connection - only required for LMIC #pragma message("ARDUINO_M5STACK_CORE2 defined; assuming M5Stack Module LoRa868 will be used") - #define LORA_CHIP SX1276 + #define USE_SX1276 #elif defined(ARDUINO_DFROBOT_FIREBEETLE_ESP32) @@ -304,7 +305,7 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #pragma message("Either LORAWAN_NODE or DFROBOT_COVER_LORA must be defined") #endif - #define LORA_CHIP SX1276 + #define USE_SX1276 #else #pragma message ("Unknown board - no automagic pinmap available") @@ -326,6 +327,16 @@ const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0 #endif +#if defined(USE_SX1276) + #define LORA_CHIP SX1276 +#elif defined(USE_SX1262) + #define LORA_CHIP SX1262 +#elif defined(USE_LR1121) + #define LORA_CHIP LR1121 +#else + #pragma message ("No radio chip selected") +#endif + LORA_CHIP radio = new Module(PIN_LORA_NSS, PIN_LORA_IRQ, PIN_LORA_RST, PIN_LORA_GPIO); // Copy over the EUI's & keys in to the something that will not compile if incorrectly formatted