From 01289ec357685deffaa8bbc85725c52d2672e879 Mon Sep 17 00:00:00 2001 From: Alexis Fourmaux Date: Sun, 3 May 2026 16:21:50 +0200 Subject: [PATCH] Reorganize project in multiple files for readability --- src/config.h | 22 ++++++++++++++++++ src/lora.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ src/lora.h | 11 +++++++++ src/main.cpp | 63 +++++----------------------------------------------- 4 files changed, 87 insertions(+), 58 deletions(-) create mode 100644 src/config.h create mode 100644 src/lora.cpp create mode 100644 src/lora.h diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..49f82a5 --- /dev/null +++ b/src/config.h @@ -0,0 +1,22 @@ +#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 + +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 diff --git a/src/lora.cpp b/src/lora.cpp new file mode 100644 index 0000000..d206762 --- /dev/null +++ b/src/lora.cpp @@ -0,0 +1,49 @@ +#include + +#include +#include + +SX1276 radio = new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1); +LoRaWANNode node(&radio, &EU868); + +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("[LoRa] Init... "); + int state = radio.begin(); + if (state != RADIOLIB_ERR_NONE) { + Serial.printf("ERREUR %d\n", state); + while (true) { delay(1000); } + } + Serial.println("OK"); +} + +void loraJoin(){ + node.beginOTAA(joinEUI, devEUI, nwkKey, appKey); + + Serial.print("[LoRaWAN] Join... "); + int state = node.activateOTAA(); + if (state != RADIOLIB_LORAWAN_NEW_SESSION) { + Serial.printf("Echec : %d\n", state); + while (true) { delay(1000); } + } + Serial.println("Join OK !"); +} + +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); + } +} \ No newline at end of file diff --git a/src/lora.h b/src/lora.h new file mode 100644 index 0000000..a90127d --- /dev/null +++ b/src/lora.h @@ -0,0 +1,11 @@ +#ifndef LORA_H +#define LORA_H + +#include + +void loraReset(); +void loraInit(); +void loraJoin(); +void loraSend(uint8_t payload[], size_t size); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index afc115a..20310b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,68 +1,15 @@ -#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 - -void loraReset() { - pinMode(LORA_RST, OUTPUT); - digitalWrite(LORA_RST, LOW); - delay(10); - digitalWrite(LORA_RST, HIGH); - delay(10); -} - -SX1276 radio = new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1); - -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; - -LoRaWANNode node(&radio, &EU868); +#include void setup() { Serial.begin(115200); delay(2000); - - loraReset(); - SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS); - - Serial.print("[LoRa] Init... "); - int state = radio.begin(); - if (state != RADIOLIB_ERR_NONE) { - Serial.printf("ERREUR %d\n", state); - while (true) { delay(1000); } - } - Serial.println("OK"); - - node.beginOTAA(joinEUI, devEUI, nwkKey, appKey); - - Serial.print("[LoRaWAN] Join... "); - state = node.activateOTAA(); - if (state != RADIOLIB_LORAWAN_NEW_SESSION) { - Serial.printf("Echec : %d\n", state); - while (true) { delay(1000); } - } - Serial.println("Join OK !"); + + loraInit(); + loraJoin(); } void loop() { uint8_t payload[] = { 0x01, 0x02, 0x03 }; - int state = node.sendReceive(payload, sizeof(payload), 1); - if (state == RADIOLIB_ERR_NONE || state == RADIOLIB_LORAWAN_NO_DOWNLINK) { - Serial.println("[TX] OK"); - } else { - Serial.printf("[TX] Erreur : %d\n", state); - } + loraSend(payload, sizeof(payload)); delay(60000); } \ No newline at end of file