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);
|
LoRaWANNode node(&radio, &EU868);
|
||||||
|
|
||||||
uint8_t noncesBuffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE];
|
uint8_t noncesBuffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE];
|
||||||
|
RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];
|
||||||
|
|
||||||
void loraReset()
|
void loraReset()
|
||||||
{
|
{
|
||||||
|
|
@ -24,7 +25,7 @@ void loraInit()
|
||||||
loraReset();
|
loraReset();
|
||||||
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS);
|
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS);
|
||||||
|
|
||||||
Serial.print("[LoRa] Init... ");
|
Serial.print("[LoRaWAN] Init... ");
|
||||||
int state = radio.begin();
|
int state = radio.begin();
|
||||||
if (state != RADIOLIB_ERR_NONE)
|
if (state != RADIOLIB_ERR_NONE)
|
||||||
{
|
{
|
||||||
|
|
@ -40,9 +41,7 @@ void loraInit()
|
||||||
void loraRestoreDevNonce()
|
void loraRestoreDevNonce()
|
||||||
{
|
{
|
||||||
prefs.begin("lorawan");
|
prefs.begin("lorawan");
|
||||||
size_t len = prefs.getBytesLength("nonces");
|
if (prefs.isKey("nonces"))
|
||||||
|
|
||||||
if (len == RADIOLIB_LORAWAN_NONCES_BUF_SIZE)
|
|
||||||
{
|
{
|
||||||
prefs.getBytes("nonces", noncesBuffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
prefs.getBytes("nonces", noncesBuffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
||||||
}
|
}
|
||||||
|
|
@ -68,24 +67,62 @@ void loraPersistDevNonce()
|
||||||
prefs.end();
|
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);
|
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||||
loraRestoreDevNonce();
|
loraRestoreDevNonce();
|
||||||
|
|
||||||
Serial.print("[LoRaWAN] Join... ");
|
int16_t state = node.activateOTAA();
|
||||||
int state = node.activateOTAA();
|
|
||||||
loraPersistDevNonce();
|
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);
|
Serial.print("[LoRaWAN] Join complet...");
|
||||||
while (true)
|
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)
|
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);
|
Serial.printf("[TX] Erreur : %d\n", state);
|
||||||
}
|
}
|
||||||
|
loraSaveSession();
|
||||||
}
|
}
|
||||||
|
|
@ -9,5 +9,7 @@ void loraJoin();
|
||||||
void loraSend(uint8_t payload[], size_t size);
|
void loraSend(uint8_t payload[], size_t size);
|
||||||
void loraPersistDevNonce();
|
void loraPersistDevNonce();
|
||||||
void loraSaveDevNonce();
|
void loraSaveDevNonce();
|
||||||
|
void loraRestoreSession();
|
||||||
|
void loraSaveSession();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -6,10 +6,11 @@ void setup() {
|
||||||
|
|
||||||
loraInit();
|
loraInit();
|
||||||
loraJoin();
|
loraJoin();
|
||||||
|
uint8_t payload[] = { 0x01, 0x02, 0x03 };
|
||||||
|
loraSend(payload, sizeof(payload));
|
||||||
|
Serial.flush();
|
||||||
|
esp_deep_sleep(60000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
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