From 244d1eea21653806be296a7b72d363532b3f55d7 Mon Sep 17 00:00:00 2001 From: Alexis Fourmaux Date: Wed, 6 May 2026 17:25:17 +0200 Subject: [PATCH] refacto: change lora to class with ITransmitter interface --- lib/Transmitter/ITransmitter.h | 11 +++ lib/Transmitter/LoRaTransmitter.cpp | 147 ++++++++++++++++++++++++++++ lib/Transmitter/LoRaTransmitter.h | 47 +++++++++ src/config.h | 26 ++--- src/lora.cpp | 140 -------------------------- src/lora.h | 15 --- src/main.cpp | 18 +++- 7 files changed, 225 insertions(+), 179 deletions(-) create mode 100644 lib/Transmitter/ITransmitter.h create mode 100644 lib/Transmitter/LoRaTransmitter.cpp create mode 100644 lib/Transmitter/LoRaTransmitter.h delete mode 100644 src/lora.cpp delete mode 100644 src/lora.h diff --git a/lib/Transmitter/ITransmitter.h b/lib/Transmitter/ITransmitter.h new file mode 100644 index 0000000..30191b9 --- /dev/null +++ b/lib/Transmitter/ITransmitter.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include + +class ITransmitter { +public: + virtual ~ITransmitter() = default; + virtual void init() = 0; + virtual void join() = 0; + virtual void send(uint8_t* payload, size_t size) = 0; +}; \ No newline at end of file diff --git a/lib/Transmitter/LoRaTransmitter.cpp b/lib/Transmitter/LoRaTransmitter.cpp new file mode 100644 index 0000000..aeb4454 --- /dev/null +++ b/lib/Transmitter/LoRaTransmitter.cpp @@ -0,0 +1,147 @@ +#include + +RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE]; + +LoRaTransmitter::LoRaTransmitter( + uint64_t joinEUI, + uint64_t devEUI, + uint8_t* nwkKey, + uint8_t* appKey +) + : _joinEUI(joinEUI), + _devEUI(devEUI), + _radio(new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1)), + _node(&_radio, &EU868) +{ + memcpy(_nwkKey, nwkKey, 16); + memcpy(_appKey, appKey, 16); + memset(_noncesBuffer, 0, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); +} + +void LoRaTransmitter::init() +{ + _reset(); + SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS); + + Serial.print("[LoRaWAN] Init... "); + int state = _radio.begin(); + if (state != RADIOLIB_ERR_NONE) + { + Serial.printf("ERREUR %d\n", state); + while (true) + { + delay(1000); + } + } + Serial.println("OK"); +} + +void LoRaTransmitter::join() +{ + int16_t state = RADIOLIB_ERR_NETWORK_NOT_JOINED; + + _node.beginOTAA(_joinEUI, _devEUI, _nwkKey, _appKey); + _restoreDevNonce(); + if (_restoreSession() != RADIOLIB_LORAWAN_SESSION_RESTORED) + { + Serial.print("[LoRaWAN] Join complet..."); + int16_t state = _fullJoin(); + + if (state != RADIOLIB_LORAWAN_NEW_SESSION) + { + Serial.printf("Echec : %d\n", state); + while (true) + { + delay(1000); + } + } + } + + Serial.println("OK"); + _saveSession(); +} + +void LoRaTransmitter::send(uint8_t payload[], size_t size) +{ + int state = _node.sendReceive(payload, size, 1); + if (state == RADIOLIB_ERR_NONE || state == RADIOLIB_LORAWAN_NO_DOWNLINK) + { + Serial.println("[TX] OK"); + } + else + { + Serial.printf("[TX] Erreur : %d\n", state); + } + _saveSession(); +} + +void LoRaTransmitter::_reset() +{ + pinMode(LORA_RST, OUTPUT); + digitalWrite(LORA_RST, LOW); + delay(10); + digitalWrite(LORA_RST, HIGH); + delay(10); +} + +void LoRaTransmitter::_restoreDevNonce() +{ + _prefs.begin("lorawan"); + if (_prefs.isKey("nonces")) + { + _prefs.getBytes("nonces", _noncesBuffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); + } + else + { + Serial.println("[NVS] Aucune donnée trouvée, buffer initialisé à zéro"); + memset(_noncesBuffer, 0, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); + } + _prefs.end(); + + _node.setBufferNonces(_noncesBuffer); +} + +void LoRaTransmitter::_persistDevNonce() +{ + _prefs.begin("lorawan"); + uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; + uint8_t *persist = _node.getBufferNonces(); + memcpy(buffer, persist, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); + + size_t result = _prefs.putBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); + + _prefs.end(); +} + +int16_t LoRaTransmitter::_restoreSession() +{ + 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 LoRaTransmitter::_saveSession() +{ + uint8_t *session = _node.getBufferSession(); + memcpy(LWsession, session, RADIOLIB_LORAWAN_SESSION_BUF_SIZE); + Serial.println("[RTC] Session sauvegardée"); +} + +int16_t LoRaTransmitter::_fullJoin() +{ + _node.beginOTAA(_joinEUI, _devEUI, _nwkKey, _appKey); + _restoreDevNonce(); + + int16_t state = _node.activateOTAA(); + + _persistDevNonce(); + return state; +} \ No newline at end of file diff --git a/lib/Transmitter/LoRaTransmitter.h b/lib/Transmitter/LoRaTransmitter.h new file mode 100644 index 0000000..a152c53 --- /dev/null +++ b/lib/Transmitter/LoRaTransmitter.h @@ -0,0 +1,47 @@ +#pragma once +#include +#include +#include +#include +#include + +#define LORA_SCK 5 +#define LORA_MISO 19 +#define LORA_MOSI 27 +#define LORA_NSS 18 +#define LORA_RST 23 +#define LORA_DIO0 26 +#define LORA_DIO1 33 + +class LoRaTransmitter : public ITransmitter { +public: + LoRaTransmitter( + uint64_t joinEUI, + uint64_t devEUI, + uint8_t* nwkKey, + uint8_t* appKey + ); + + void init() override; + void join() override; + void send(uint8_t* payload, size_t size) override; + +private: + uint64_t _joinEUI; + uint64_t _devEUI; + uint8_t _nwkKey[16]; + uint8_t _appKey[16]; + + SX1276 _radio; + LoRaWANNode _node; + Preferences _prefs; + + uint8_t _noncesBuffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; + + void _reset(); + void _restoreDevNonce(); + void _persistDevNonce(); + int16_t _restoreSession(); + void _saveSession(); + int16_t _fullJoin(); +}; \ No newline at end of file diff --git a/src/config.h b/src/config.h index 49f82a5..7161b59 100644 --- a/src/config.h +++ b/src/config.h @@ -1,22 +1,8 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#include - -#define LORA_SCK 5 -#define LORA_MISO 19 -#define LORA_MOSI 27 -#define LORA_NSS 18 -#define LORA_RST 23 -#define LORA_DIO0 26 -#define LORA_DIO1 33 +#pragma once uint64_t joinEUI = 0x35f48318e1324e2e; -uint64_t devEUI = 0x0586fe41112d83d9; -uint8_t appKey[] = { - 0x0b, 0x84, 0xb4, 0x04, 0x0f, 0xd5, 0x56, 0x1b, - 0x00, 0x53, 0x94, 0x22, 0xc1, 0xf1, 0x4f, 0xd6 -}; -uint8_t *nwkKey = appKey; - -#endif \ No newline at end of file +uint64_t devEUI = 0x0586fe41112d83d9; +uint8_t appKey[] = { + 0x0b, 0x84, 0xb4, 0x04, 0x0f, 0xd5, 0x56, 0x1b, + 0x00, 0x53, 0x94, 0x22, 0xc1, 0xf1, 0x4f, 0xd6}; +uint8_t *nwkKey = appKey; \ No newline at end of file diff --git a/src/lora.cpp b/src/lora.cpp deleted file mode 100644 index 27aa902..0000000 --- a/src/lora.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include - -#include -#include -#include - -Preferences prefs; -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() -{ - pinMode(LORA_RST, OUTPUT); - digitalWrite(LORA_RST, LOW); - delay(10); - digitalWrite(LORA_RST, HIGH); - delay(10); -} - -void loraInit() -{ - loraReset(); - SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS); - - Serial.print("[LoRaWAN] Init... "); - int state = radio.begin(); - if (state != RADIOLIB_ERR_NONE) - { - Serial.printf("ERREUR %d\n", state); - while (true) - { - delay(1000); - } - } - Serial.println("OK"); -} - -void loraRestoreDevNonce() -{ - prefs.begin("lorawan"); - if (prefs.isKey("nonces")) - { - prefs.getBytes("nonces", noncesBuffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); - } - else - { - Serial.println("[NVS] Aucune donnée trouvée, buffer initialisé à zéro"); - memset(noncesBuffer, 0, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); - } - prefs.end(); - - node.setBufferNonces(noncesBuffer); -} - -void loraPersistDevNonce() -{ - prefs.begin("lorawan"); - uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; - uint8_t *persist = node.getBufferNonces(); - memcpy(buffer, persist, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); - - size_t result = prefs.putBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); - - prefs.end(); -} - -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(); - - int16_t state = node.activateOTAA(); - - loraPersistDevNonce(); - 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.print("[LoRaWAN] Join complet..."); - int16_t state = loraFullJoin(); - - if (state != RADIOLIB_LORAWAN_NEW_SESSION) - { - Serial.printf("Echec : %d\n", state); - while (true) - { - delay(1000); - } - } - } - - Serial.println("OK"); - loraSaveSession(); -} - -void loraSend(uint8_t payload[], size_t size) -{ - int state = node.sendReceive(payload, size, 1); - if (state == RADIOLIB_ERR_NONE || state == RADIOLIB_LORAWAN_NO_DOWNLINK) - { - Serial.println("[TX] OK"); - } - else - { - Serial.printf("[TX] Erreur : %d\n", state); - } - loraSaveSession(); -} \ No newline at end of file diff --git a/src/lora.h b/src/lora.h deleted file mode 100644 index dc97ea3..0000000 --- a/src/lora.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef LORA_H -#define LORA_H - -#include - -void loraReset(); -void loraInit(); -void loraJoin(); -void loraSend(uint8_t payload[], size_t size); -void loraPersistDevNonce(); -void loraSaveDevNonce(); -void loraRestoreSession(); -void loraSaveSession(); - -#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c0eced4..31df6ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,26 @@ -#include +#include +#include "LoRaTransmitter.h" +#include "config.h" + +ITransmitter* transmitter = new LoRaTransmitter( + joinEUI, + devEUI, + nwkKey, + appKey +); void setup() { Serial.begin(115200); delay(2000); - loraInit(); + transmitter->init(); + transmitter->join(); } void loop() { - loraJoin(); uint8_t payload[] = { 0x01, 0x02, 0x03 }; - loraSend(payload, sizeof(payload)); + transmitter->send(payload, sizeof(payload)); + Serial.flush(); esp_deep_sleep(60000000); } \ No newline at end of file