feat: add session restoration
This commit is contained in:
parent
d569a07655
commit
441f724c60
3 changed files with 56 additions and 15 deletions
62
src/lora.cpp
62
src/lora.cpp
|
|
@ -9,6 +9,7 @@ SX1276 radio = new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1);
|
|||
LoRaWANNode node(&radio, &EU868);
|
||||
|
||||
uint8_t noncesBuffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE];
|
||||
RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];
|
||||
|
||||
void loraReset()
|
||||
{
|
||||
|
|
@ -24,7 +25,7 @@ void loraInit()
|
|||
loraReset();
|
||||
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS);
|
||||
|
||||
Serial.print("[LoRa] Init... ");
|
||||
Serial.print("[LoRaWAN] Init... ");
|
||||
int state = radio.begin();
|
||||
if (state != RADIOLIB_ERR_NONE)
|
||||
{
|
||||
|
|
@ -40,9 +41,7 @@ void loraInit()
|
|||
void loraRestoreDevNonce()
|
||||
{
|
||||
prefs.begin("lorawan");
|
||||
size_t len = prefs.getBytesLength("nonces");
|
||||
|
||||
if (len == RADIOLIB_LORAWAN_NONCES_BUF_SIZE)
|
||||
if (prefs.isKey("nonces"))
|
||||
{
|
||||
prefs.getBytes("nonces", noncesBuffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
||||
}
|
||||
|
|
@ -68,24 +67,62 @@ void loraPersistDevNonce()
|
|||
prefs.end();
|
||||
}
|
||||
|
||||
void loraJoin()
|
||||
int16_t loraRestoreSession()
|
||||
{
|
||||
Serial.print("[LoRaWAN] Tentative de restaurer la session...");
|
||||
|
||||
int16_t state = node.setBufferSession(LWsession);
|
||||
if (state == RADIOLIB_ERR_NONE)
|
||||
{
|
||||
state = node.activateOTAA();
|
||||
}
|
||||
else {
|
||||
Serial.println("Echec");
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
void loraSaveSession()
|
||||
{
|
||||
uint8_t *session = node.getBufferSession();
|
||||
memcpy(LWsession, session, RADIOLIB_LORAWAN_SESSION_BUF_SIZE);
|
||||
Serial.println("[RTC] Session sauvegardée");
|
||||
}
|
||||
|
||||
int16_t loraFullJoin()
|
||||
{
|
||||
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||
loraRestoreDevNonce();
|
||||
|
||||
Serial.print("[LoRaWAN] Join... ");
|
||||
int state = node.activateOTAA();
|
||||
int16_t state = node.activateOTAA();
|
||||
|
||||
loraPersistDevNonce();
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION)
|
||||
return state;
|
||||
}
|
||||
|
||||
void loraJoin()
|
||||
{
|
||||
int16_t state = RADIOLIB_ERR_NETWORK_NOT_JOINED;
|
||||
|
||||
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||
loraRestoreDevNonce();
|
||||
if (loraRestoreSession() != RADIOLIB_LORAWAN_SESSION_RESTORED)
|
||||
{
|
||||
Serial.printf("Echec : %d\n", state);
|
||||
while (true)
|
||||
Serial.print("[LoRaWAN] Join complet...");
|
||||
int16_t state = loraFullJoin();
|
||||
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION)
|
||||
{
|
||||
delay(1000);
|
||||
Serial.printf("Echec : %d\n", state);
|
||||
while (true)
|
||||
{
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("Join OK !");
|
||||
Serial.println("OK");
|
||||
loraSaveSession();
|
||||
}
|
||||
|
||||
void loraSend(uint8_t payload[], size_t size)
|
||||
|
|
@ -99,4 +136,5 @@ void loraSend(uint8_t payload[], size_t size)
|
|||
{
|
||||
Serial.printf("[TX] Erreur : %d\n", state);
|
||||
}
|
||||
loraSaveSession();
|
||||
}
|
||||
|
|
@ -9,5 +9,7 @@ void loraJoin();
|
|||
void loraSend(uint8_t payload[], size_t size);
|
||||
void loraPersistDevNonce();
|
||||
void loraSaveDevNonce();
|
||||
void loraRestoreSession();
|
||||
void loraSaveSession();
|
||||
|
||||
#endif
|
||||
|
|
@ -6,10 +6,11 @@ void setup() {
|
|||
|
||||
loraInit();
|
||||
loraJoin();
|
||||
uint8_t payload[] = { 0x01, 0x02, 0x03 };
|
||||
loraSend(payload, sizeof(payload));
|
||||
Serial.flush();
|
||||
esp_deep_sleep(60000000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
uint8_t payload[] = { 0x01, 0x02, 0x03 };
|
||||
loraSend(payload, sizeof(payload));
|
||||
delay(60000);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue