diff --git a/.gitignore b/.gitignore index 89cc49c..1fcbe2c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch + +secrets.csv +nvs_secrets.bin \ No newline at end of file diff --git a/flash_secrets.sh b/flash_secrets.sh new file mode 100755 index 0000000..e896f7e --- /dev/null +++ b/flash_secrets.sh @@ -0,0 +1,4 @@ +# Si nécessaire, activer le venv de platformio : source ~/.platformio/penv/bin/activate + +pio pkg exec -p "tool-esptoolpy" -- python -m esp_idf_nvs_partition_gen generate secrets.csv nvs_secrets.bin 0x6000 +pio pkg exec -p "tool-esptoolpy" -- esptool.py write_flash 0x9000 nvs_secrets.bin \ No newline at end of file diff --git a/lib/Transmitter/LoRaTransmitter.cpp b/lib/Transmitter/LoRaTransmitter.cpp index bf1870d..5bb662b 100644 --- a/lib/Transmitter/LoRaTransmitter.cpp +++ b/lib/Transmitter/LoRaTransmitter.cpp @@ -2,19 +2,19 @@ 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)), +LoRaTransmitter::LoRaTransmitter() + : _radio(new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1)), _node(&_radio, &EU868) { - memcpy(_nwkKey, nwkKey, 16); - memcpy(_appKey, appKey, 16); + _prefs.begin("lorawan", true); + + _joinEUI = _prefs.getULong64("join_eui", 0); + _devEUI = _prefs.getULong64("dev_eui", 0); + _prefs.getBytes("nwk_key", _nwkKey, 16); + _prefs.getBytes("app_key", _appKey, 16); + + _prefs.end(); + memset(_noncesBuffer, 0, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); } diff --git a/lib/Transmitter/LoRaTransmitter.h b/lib/Transmitter/LoRaTransmitter.h index 0dbd777..1c5fbbf 100644 --- a/lib/Transmitter/LoRaTransmitter.h +++ b/lib/Transmitter/LoRaTransmitter.h @@ -15,12 +15,7 @@ class LoRaTransmitter : public ITransmitter { public: - LoRaTransmitter( - uint64_t joinEUI, - uint64_t devEUI, - uint8_t* nwkKey, - uint8_t* appKey - ); + LoRaTransmitter(); TransmitError init() override; TransmitError join() override; diff --git a/secrets.csv.example b/secrets.csv.example new file mode 100644 index 0000000..971fc5c --- /dev/null +++ b/secrets.csv.example @@ -0,0 +1,6 @@ +key,type,encoding,value +lorawan,namespace,, +join_eui,data,hex2bin,0011223344556677 +dev_eui,data,hex2bin,0011223344556677 +app_key,data,hex2bin,AABBCCDDEEFF00112233445566778899 +nwk_key,data,hex2bin,AABBCCDDEEFF00112233445566778899 \ No newline at end of file diff --git a/src/config.h b/src/config.h deleted file mode 100644 index f2b60a7..0000000 --- a/src/config.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#define MAX_RESTART 1 -#define MAX_FATAL_RESTART 3 - -uint64_t joinEUI = 0x911b5c62b4dc2079; -uint64_t devEUI = 0x0586fe41112d83d9; -uint8_t appKey[] = { - 0x81, 0x24, 0xbe, 0x86, 0x59, 0x06, 0xa4, 0x4f, - 0xe7, 0x39, 0xd6, 0x0f, 0xd6, 0x88, 0x91, 0xcc}; -uint8_t nwkKey[] = { - 0xb8, 0x30, 0x20, 0xf2, 0x52, 0x3b, 0xd2, 0xb0, - 0x1d, 0x12, 0xe1, 0xb9, 0xe7, 0xe1, 0xb0, 0xde}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5870002..7120d3c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,15 +4,13 @@ #include #include "config.h" +#define MAX_RESTART 1 +#define MAX_FATAL_RESTART 3 + RTC_DATA_ATTR uint8_t fatalRestartCount = 0; RTC_DATA_ATTR uint8_t restartCount = 0; -ITransmitter* transmitter = new LoRaTransmitter( - joinEUI, - devEUI, - nwkKey, - appKey -); +ITransmitter* transmitter; void fatalError(const char* msg){ Serial.printf("[FATAL] %s\n", msg); @@ -57,6 +55,8 @@ void setup() { Serial.begin(115200); delay(2000); + transmitter = new LoRaTransmitter(); + if (displayInit() != DisplayError::OK){ nonCriticalError("Initialisation du périphérique d'affichage échouée"); }