Skip to content

Commit

Permalink
20241227 refactoring (#121)
Browse files Browse the repository at this point in the history
* Modified radio chip selection
* Moved uplinkDelay() from BresserWeatherSensorLWCmd.cpp to BresserWeatherSensorLW.ino
* Disabled m5stack_core2 due to iram linker segment overflow
  • Loading branch information
matthias-bs authored Dec 27, 2024
1 parent 02dd207 commit c25135f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 28 additions & 6 deletions BresserWeatherSensorLW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// -
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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 -
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 2 additions & 15 deletions BresserWeatherSensorLWCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// -
Expand All @@ -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;


/*
Expand Down Expand Up @@ -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 ---");

Expand Down Expand Up @@ -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();
}
3 changes: 2 additions & 1 deletion BresserWeatherSensorLWCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// 20240722 Added CMD_SET_LW_STATUS_INTERVAL, renamed
// CMD_<GET|SET>_STATUS_INTERVAL to CMD_<GET|SET>_APP_STATUS_INTERVAL
// 20240920 Changed sendCfgUplink() to encodeCfgUplink()
// 20241227 Removed delay from encodeCfgUplink()
//
// ToDo:
// -
Expand Down Expand Up @@ -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
55 changes: 33 additions & 22 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// -
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit c25135f

Please sign in to comment.