Skip to content

Commit

Permalink
Bump to RadioLib v7.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed Sep 23, 2024
1 parent ca8722a commit 5461a6f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
43 changes: 24 additions & 19 deletions BresserWeatherSensorLW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
// 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
//
// ToDo:
// -
Expand Down Expand Up @@ -386,10 +387,9 @@ void printDateTime(void)
*/
int16_t lwActivate(void)
{
int16_t state = RADIOLIB_ERR_UNKNOWN;

// setup the OTAA session information
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
int16_t state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
debug(state != RADIOLIB_ERR_NONE, "Initialise node failed", state, true);

log_d("Recalling LoRaWAN nonces & session");
// ##### setup the flash storage
Expand Down Expand Up @@ -587,8 +587,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 +635,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 +701,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 +772,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
15 changes: 8 additions & 7 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
// (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
//
// ToDo:
// -
Expand Down Expand Up @@ -382,16 +383,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

0 comments on commit 5461a6f

Please sign in to comment.