Skip to content

Commit

Permalink
20240922 bump radiolib7.0.1 (#110)
Browse files Browse the repository at this point in the history
* Updated to RadioLib v7.0.1

* Modified for LoRaWAN v1.0.4
  • Loading branch information
matthias-bs authored Sep 30, 2024
1 parent ca8722a commit fbf9b74
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
#declare -a required_libs=("https://github.com/matthias-bs/BresserWeatherSensorReceiver.git"
declare -a required_libs=(
"BresserWeatherSensorReceiver@0.28.9"
"RadioLib@6.6.0"
"RadioLib@7.0.1"
"LoRa Serialization@3.2.1"
"ESP32Time@2.0.6"
"OneWireNg@0.13.3"
Expand Down
58 changes: 38 additions & 20 deletions BresserWeatherSensorLW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
// 20240804 PowerFeather: Added configuration of maximum battery charging current
// 20240818 Fixed bootCount
// 20240920 Fixed handling of downlink after any kind of uplink
// 20240912 Bumped to RadioLib v7.0.0
// 20240928 Modified for LoRaWAN v1.0.4 (requires no nwkKey)
//
// ToDo:
// -
Expand Down Expand Up @@ -386,10 +388,16 @@ void printDateTime(void)
*/
int16_t lwActivate(void)
{
int16_t state = RADIOLIB_ERR_UNKNOWN;

// setup the OTAA session information
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
#if defined(LORAWAN_VERSION_1_1)
int16_t state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
#elif defined(LORAWAN_VERSION_1_0_4)
int16_t state = node.beginOTAA(joinEUI, devEUI, nullptr, appKey);
#else
#error "LoRaWAN version not defined"
#endif

debug(state != RADIOLIB_ERR_NONE, "Initialise node failed", state, true);

log_d("Recalling LoRaWAN nonces & session");
// ##### setup the flash storage
Expand Down Expand Up @@ -561,7 +569,12 @@ void setup()
printDateTime();

// Try to load LoRaWAN secrets from LittleFS file, if available
loadSecrets(joinEUI, devEUI, nwkKey, appKey);
#ifdef LORAWAN_VERSION_1_1
bool requireNwkKey = true;
#else
bool requireNwkKey = false;
#endif
loadSecrets(requireNwkKey, joinEUI, devEUI, nwkKey, appKey);

// Initialize Application Layer
appLayer.begin();
Expand All @@ -587,8 +600,8 @@ void setup()

LoraEncoder encoder(uplinkPayload);

uint8_t port = 1;
appLayer.getPayloadStage1(port, encoder);
uint8_t fPort = 1;
appLayer.getPayloadStage1(fPort, encoder);

int16_t state = 0; // return value for calls to RadioLib

Expand Down Expand Up @@ -635,17 +648,19 @@ void setup()
}

// get payload immediately before uplink - not used here
appLayer.getPayloadStage2(port, encoder);
appLayer.getPayloadStage2(fPort, encoder);

uint8_t downlinkPayload[MAX_DOWNLINK_SIZE]; // Make sure this fits your plans!
size_t downlinkSize; // To hold the actual payload size rec'd
LoRaWANEvent_t downlinkDetails;

uint8_t payloadSize = encoder.getLength();
if (payloadSize > PAYLOAD_SIZE)
uint8_t maxPayloadLen = node.getMaxPayloadLen();
log_d("Max payload length: %u", maxPayloadLen);
if (payloadSize > maxPayloadLen)
{
log_w("Payload size exceeds maximum of %u bytes - truncating", PAYLOAD_SIZE);
payloadSize = PAYLOAD_SIZE;
log_w("Payload size exceeds maximum of %u bytes - truncating", maxPayloadLen);
payloadSize = maxPayloadLen;
}

// ----- and now for the main event -----
Expand Down Expand Up @@ -699,41 +714,42 @@ void setup()
if (fsmStage == E_FSM_STAGE::E_RESPONSE)
{
log_d("Sending response uplink.");
port = uplinkReq;
encodeCfgUplink(port, uplinkPayload, payloadSize, uplinkIntervalSeconds);
fPort = uplinkReq;
encodeCfgUplink(fPort, uplinkPayload, payloadSize, uplinkIntervalSeconds);
}
else if (fsmStage == E_FSM_STAGE::E_LWSTATUS)
{
log_d("Sending LoRaWAN status uplink.");
port = CMD_GET_LW_STATUS;
encodeCfgUplink(port, uplinkPayload, payloadSize, uplinkIntervalSeconds);
fPort = CMD_GET_LW_STATUS;
encodeCfgUplink(fPort, uplinkPayload, payloadSize, uplinkIntervalSeconds);
lwStatusUplinkPending = false;
}
else if (fsmStage == E_FSM_STAGE::E_APPSTATUS)
{
log_d("Sending application status uplink.");
port = CMD_GET_SENSORS_STAT;
encodeCfgUplink(port, uplinkPayload, payloadSize, uplinkIntervalSeconds);
fPort = CMD_GET_SENSORS_STAT;
encodeCfgUplink(fPort, uplinkPayload, payloadSize, uplinkIntervalSeconds);
appStatusUplinkPending = false;
}

log_i("Sending uplink; port %u, size %u", port, payloadSize);
log_i("Sending uplink; port %u, size %u", fPort, payloadSize);

state = node.sendReceive(
uplinkPayload,
payloadSize,
port,
fPort,
downlinkPayload,
&downlinkSize,
isConfirmed,
nullptr,
&downlinkDetails);
debug((state != RADIOLIB_LORAWAN_NO_DOWNLINK) && (state != RADIOLIB_ERR_NONE), "Error in sendReceive", state, false);
debug(state < RADIOLIB_ERR_NONE, "Error in sendReceive", state, false);

uplinkReq = 0;

// Check if downlink was received
if (state != RADIOLIB_LORAWAN_NO_DOWNLINK)
// (state 0 = no downlink, state 1/2 = downlink in window Rx1/Rx2)
if (state > 0)
{
// Did we get a downlink with data for us
if (downlinkSize > 0)
Expand Down Expand Up @@ -769,6 +785,8 @@ void setup()
log_d("[LoRaWAN] Output power:\t%d dBm", downlinkDetails.power);
log_d("[LoRaWAN] Frame count:\t%u", downlinkDetails.fCnt);
log_d("[LoRaWAN] fPort:\t\t%u", downlinkDetails.fPort);
log_d("[LoRaWAN] Time-on-air: \t%u ms", node.getLastToA());
log_d("[LoRaWAN] Rx window: %d", state);
}

uint32_t networkTime = 0;
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ This was originally a remake of [BresserWeatherSensorTTN](https://github.com/mat

## Important Notes

* RadioLib's LoRaWAN implementation is currently in beta stage.
* Helium Network is not supported (requires LoRaWAN v1.0.x)
* This should not be the first Arduino sketch you are ever trying to flash to your board - try something simple first (e.g. `blink.ino`) to get familiar with the tools and workflow.
* If you are new to LoRaWAN
* Check out [The Things Fundamentals on LoRaWAN](https://www.thethingsnetwork.org/docs/lorawan/)
* Read the excellent article [RadioLib LoRaWAN on TTN starter script](https://github.com/jgromes/RadioLib/blob/master/examples/LoRaWAN/LoRaWAN_Starter/notes.md)
* You currently need [RadioLib v6.6.0](https://github.com/jgromes/RadioLib/releases/tag/6.6.0)
* You currently need [RadioLib v7.0.1](https://github.com/jgromes/RadioLib/releases/tag/7.0.1)
* You need [espressif/arduino-esp32 v3.0.X](https://github.com/espressif/arduino-esp32)
* Try and configure [BresserWeatherSensorReceiver](https://github.com/matthias-bs/BresserWeatherSensorReceiver) ([examples/BresserWeatherSensorBasic](https://github.com/matthias-bs/BresserWeatherSensorReceiver/tree/main/examples/BresserWeatherSensorBasic)) stand-alone before using it with BresserWeatherSensorLW
* If you previously used [BresserWeatherSensorTTN](https://github.com/matthias-bs/BresserWeatherSensorTTN)
Expand Down
25 changes: 18 additions & 7 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
// (examples/LoRaWAN/LoRaWAN_Reference/config.h)
// 20240613 Added LORAWAN_NODE (DFRobot FireBeetle ESP32 wiring variant)
// 20240704 Moved MAX_DOWNLINK_SIZE to BresserWeatherSensorLWCfg.h
// 20260710 Fixed pragma messages fro Firebeetle ESP32 pin config
// 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)
//
// ToDo:
// -
Expand All @@ -57,6 +59,9 @@
// How often to send an uplink - consider legal & FUP constraints - see notes
const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds

#define LORAWAN_VERSION_1_1
//#define LORAWAN_VERSION_1_0_4

// JoinEUI - previous versions of LoRaWAN called this AppEUI
// for development purposes you can use all zeros - see wiki for details
#define RADIOLIB_LORAWAN_JOIN_EUI 0x0000000000000000
Expand All @@ -68,9 +73,11 @@ const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds
#ifndef RADIOLIB_LORAWAN_APP_KEY // Replace with your App Key
#define RADIOLIB_LORAWAN_APP_KEY 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--
#endif
#ifdef LORAWAN_VERSION_1_1
#ifndef RADIOLIB_LORAWAN_NWK_KEY // Put your Nwk Key here
#define RADIOLIB_LORAWAN_NWK_KEY 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--
#endif
#endif

// For the curious, the #ifndef blocks allow for automated testing &/or you can
// put your EUI & keys in to your platformio.ini - see wiki for more tips
Expand Down Expand Up @@ -325,7 +332,11 @@ LORA_CHIP radio = new Module(PIN_LORA_NSS, PIN_LORA_IRQ, PIN_LORA_RST, PIN_LORA_
uint64_t joinEUI = RADIOLIB_LORAWAN_JOIN_EUI;
uint64_t devEUI = RADIOLIB_LORAWAN_DEV_EUI;
uint8_t appKey[] = { RADIOLIB_LORAWAN_APP_KEY };
#ifdef LORAWAN_VERSION_1_1
uint8_t nwkKey[] = { RADIOLIB_LORAWAN_NWK_KEY };
#else
uint8_t nwkKey[] = { 0 };
#endif

// Create the LoRaWAN node
LoRaWANNode node(&radio, &Region, subBand);
Expand Down Expand Up @@ -382,16 +393,16 @@ String stateDecode(const int16_t result) {
return "RADIOLIB_ERR_DWELL_TIME_EXCEEDED";
case RADIOLIB_ERR_CHECKSUM_MISMATCH:
return "RADIOLIB_ERR_CHECKSUM_MISMATCH";
case RADIOLIB_LORAWAN_NO_DOWNLINK:
return "RADIOLIB_LORAWAN_NO_DOWNLINK";
case RADIOLIB_ERR_NO_JOIN_ACCEPT:
return "RADIOLIB_ERR_NO_JOIN_ACCEPT";
case RADIOLIB_LORAWAN_SESSION_RESTORED:
return "RADIOLIB_LORAWAN_SESSION_RESTORED";
case RADIOLIB_LORAWAN_NEW_SESSION:
return "RADIOLIB_LORAWAN_NEW_SESSION";
case RADIOLIB_LORAWAN_NONCES_DISCARDED:
return "RADIOLIB_LORAWAN_NONCES_DISCARDED";
case RADIOLIB_LORAWAN_SESSION_DISCARDED:
return "RADIOLIB_LORAWAN_SESSION_DISCARDED";
case RADIOLIB_ERR_NONCES_DISCARDED:
return "RADIOLIB_ERR_NONCES_DISCARDED";
case RADIOLIB_ERR_SESSION_DISCARDED:
return "RADIOLIB_ERR_SESSION_DISCARDED";
}
return "See TypeDef.h";
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"homepage": "https://github.com/matthias-bs/BresserWeatherSensorLW#README",
"dependencies": {
"BresserWeatherSensorReceiver": "matthias-bs/BresserWeatherSensorReceiver#semver:^0.28.9",
"RadioLib": "jgromes/RadioLib#semver:^6.6.0",
"RadioLib": "jgromes/RadioLib#semver:^7.0.1",
"lora-serialization": "thesolarnomad/lora-serialization#semver:^3.2.1",
"ESP32Time": "fbiego/ESP32Time#semver:^2.0.6",
"OneWireNg": "https://github.com/pstolarz/OneWireNg#semver:^0.13.3",
Expand Down
Loading

0 comments on commit fbf9b74

Please sign in to comment.